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

Function _bundleWithFallback

src/utils/teleport/gitBundle.ts:51–147  ·  view source on GitHub ↗
(
  gitRoot: string,
  bundlePath: string,
  maxBytes: number,
  hasStash: boolean,
  signal: AbortSignal | undefined,
)

Source from the content-addressed store, hash-verified

49// commit of HEAD's tree (or the stash tree if WIP exists) — no history,
50// just the snapshot. Receiver needs refs/seed/root handling for that tier.
51async function _bundleWithFallback(
52 gitRoot: string,
53 bundlePath: string,
54 maxBytes: number,
55 hasStash: boolean,
56 signal: AbortSignal | undefined,
57): Promise<BundleCreateResult> {
58 // --all picks up refs/seed/stash; HEAD needs it explicit.
59 const extra = hasStash ? ['refs/seed/stash'] : []
60 const mkBundle = (base: string) =>
61 execFileNoThrowWithCwd(
62 gitExe(),
63 ['bundle', 'create', bundlePath, base, ...extra],
64 { cwd: gitRoot, abortSignal: signal },
65 )
66
67 const allResult = await mkBundle('--all')
68 if (allResult.code !== 0) {
69 return {
70 ok: false,
71 error: `git bundle create --all failed (${allResult.code}): ${allResult.stderr.slice(0, 200)}`,
72 failReason: 'git_error',
73 }
74 }
75
76 const { size: allSize } = await stat(bundlePath)
77 if (allSize <= maxBytes) {
78 return { ok: true, size: allSize, scope: 'all' }
79 }
80
81 // bundle create overwrites in place.
82 logForDebugging(
83 `[gitBundle] --all bundle is ${(allSize / 1024 / 1024).toFixed(1)}MB (> ${(maxBytes / 1024 / 1024).toFixed(0)}MB), retrying HEAD-only`,
84 )
85 const headResult = await mkBundle('HEAD')
86 if (headResult.code !== 0) {
87 return {
88 ok: false,
89 error: `git bundle create HEAD failed (${headResult.code}): ${headResult.stderr.slice(0, 200)}`,
90 failReason: 'git_error',
91 }
92 }
93
94 const { size: headSize } = await stat(bundlePath)
95 if (headSize <= maxBytes) {
96 return { ok: true, size: headSize, scope: 'head' }
97 }
98
99 // Last resort: squash to a single parentless commit. Uses the stash tree
100 // when WIP exists (bakes uncommitted changes in — can't bundle the stash
101 // ref separately since its parents would drag history back).
102 logForDebugging(
103 `[gitBundle] HEAD bundle is ${(headSize / 1024 / 1024).toFixed(1)}MB, retrying squashed-root`,
104 )
105 const treeRef = hasStash ? 'refs/seed/stash^{tree}' : 'HEAD^{tree}'
106 const commitTree = await execFileNoThrowWithCwd(
107 gitExe(),
108 ['commit-tree', treeRef, '-m', 'seed'],

Callers 1

createAndUploadGitBundleFunction · 0.85

Calls 5

mkBundleFunction · 0.85
statFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
getCodeWebBaseUrlFunction · 0.85
logForDebuggingFunction · 0.50

Tested by

no test coverage detected