MCPcopy Create free account
hub / github.com/Noumena-Network/code / createAndUploadGitBundle

Function createAndUploadGitBundle

src/utils/teleport/gitBundle.ts:153–293  ·  view source on GitHub ↗
(
  config: FilesApiConfig,
  opts?: { cwd?: string; signal?: AbortSignal },
)

Source from the content-addressed store, hash-verified

151// Tracked WIP via stash create → refs/seed/stash (or baked into the
152// squashed tree); untracked not captured.
153export async function createAndUploadGitBundle(
154 config: FilesApiConfig,
155 opts?: { cwd?: string; signal?: AbortSignal },
156): Promise<BundleUploadResult> {
157 const workdir = opts?.cwd ?? getCwd()
158 const gitRoot = findGitRoot(workdir)
159 if (!gitRoot) {
160 return { success: false, error: 'Not in a git repository' }
161 }
162
163 // Sweep stale refs from a crashed prior run before --all bundles them.
164 // Runs before the empty-repo check so it's never skipped by an early return.
165 for (const ref of ['refs/seed/stash', 'refs/seed/root']) {
166 await execFileNoThrowWithCwd(gitExe(), ['update-ref', '-d', ref], {
167 cwd: gitRoot,
168 })
169 }
170
171 // `git bundle create` refuses to create an empty bundle (exit 128), and
172 // `stash create` fails with "You do not have the initial commit yet".
173 // Check for any refs (not just HEAD) so orphan branches with commits
174 // elsewhere still bundle — `--all` packs those refs regardless of HEAD.
175 const refCheck = await execFileNoThrowWithCwd(
176 gitExe(),
177 ['for-each-ref', '--count=1', 'refs/'],
178 { cwd: gitRoot },
179 )
180 if (refCheck.code === 0 && refCheck.stdout.trim() === '') {
181 logEvent('ncode_ccr_bundle_upload', {
182 outcome:
183 'empty_repo' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
184 })
185 return {
186 success: false,
187 error: 'Repository has no commits yet',
188 failReason: 'empty_repo',
189 }
190 }
191
192 // stash create writes a dangling commit — doesn't touch refs/stash or
193 // the working tree. Untracked files intentionally excluded.
194 const stashResult = await execFileNoThrowWithCwd(
195 gitExe(),
196 ['stash', 'create'],
197 { cwd: gitRoot, abortSignal: opts?.signal },
198 )
199 // exit 0 + empty stdout = nothing to stash. Nonzero is rare; non-fatal.
200 const wipStashSha = stashResult.code === 0 ? stashResult.stdout.trim() : ''
201 const hasWip = wipStashSha !== ''
202 if (stashResult.code !== 0) {
203 logForDebugging(
204 `[gitBundle] git stash create failed (${stashResult.code}), proceeding without WIP: ${stashResult.stderr.slice(0, 200)}`,
205 )
206 } else if (hasWip) {
207 logForDebugging(`[gitBundle] Captured WIP as stash ${wipStashSha}`)
208 // env-runner reads the SHA via bundle list-heads refs/seed/stash.
209 await execFileNoThrowWithCwd(
210 gitExe(),

Callers 1

teleportToRemoteFunction · 0.85

Calls 9

getCwdFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
generateTempFilePathFunction · 0.85
_bundleWithFallbackFunction · 0.85
uploadFileFunction · 0.85
unlinkFunction · 0.85
logEventFunction · 0.50
logForDebuggingFunction · 0.50

Tested by

no test coverage detected