fix(server): automatic pull no longer fast-forwards a branch switched in mid-pull #48158
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -4940,0 +4982,21 @@4982+ return;4983+ }4984+ throw cause;4985+ }4986+ held = true;4987+ valid = true;4988+ // A fresh index mtime would let git trust racily clean entries and overwrite an edit.4989+ const { atimeNs, mtimeNs } = NodeFS.statSync(indexPath, { bigint: true });4990+ const seconds = (ns: bigint) => Number(ns / 1000n) / 1e6;4991+ NodeFS.utimesSync(lockPath, seconds(atimeNs), seconds(mtimeNs));4992+ },4993+ catch: (cause) => {4994+ if (held) NodeFS.rmSync(lockPath, { force: true });4995+ held = false;4996+ return lockFailed(cause);4997+ },4998+ }),4999+ () =>5000+ unlock.pipe(5001+ Effect.catch(() => Effect.sync(() => NodeFS.rmSync(lockPath, { force: true }))),5002+ ),@@ -5016,3 +5098,6 @@50165098 if (!claimed) yield* Fiber.await(pending.fiber);50175099 }50185100 }5101+ // `git pull` fast-forwards whatever is checked out when it starts, so the lock keeps a5102+ // concurrent switch from swapping in another branch after the check below.5103+ const { lockPath } = yield* lockIndex(cwd, "GitVcsDriver.pullCurrentBranch.lockIndex");@@ -5016,3 +5098,12 @@50165098 if (!claimed) yield* Fiber.await(pending.fiber);50175099 }50185100 }5101+ // `git pull` fast-forwards whatever is checked out when it starts, so the lock keeps a5102+ // concurrent switch from swapping in another branch after the check below.5103+ const { lockPath } = yield* lockIndex(cwd, "GitVcsDriver.pullCurrentBranch.lockIndex");5104+ const headRef = yield* runGitStdout(5105+ "GitVcsDriver.pullCurrentBranch.headRef",5106+ cwd,5107+ ["symbolic-ref", "--quiet", "HEAD"],5108+ true,5109+ ).pipe(Effect.map((stdout) => stdout.trim()));
Pushed71716e4
Pusheddcea708
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
- @@ -5089,0 +5159,21 @@5159+ if (5160+ mergeRef &&5161+ mergeRefs.length === 1 &&5162+ currentBranch === refName &&5163+ currentSha === fetchedSha5164+ ) {5165+ args = ["fetch", "--", remote, mergeRef];5166+ fetchedUpstreamOnly = true;5167+ result = yield* executeGitWithStableDiagnostics(5168+ "GitVcsDriver.pullCurrentBranch.fetch",5169+ cwd,5170+ args,5171+ fetchOptions,5172+ );5173+ }5174+ }5175+ if (result.exitCode !== 0) return yield* pullFailed(args, result);5176+ }5177+ // `git merge` fast-forwards whatever is checked out when it starts, so the lock keeps a5178+ // concurrent switch from swapping in another branch after the check below.5179+ const { lockPath, unlock } = yield* lockIndex(cwd, "GitVcsDriver.pullCurrentBranch.lockIndex");
Pushedd53e53f
Pushed699da78
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
- @@ -5096,0 +5198,21 @@5198+ const readCommit = (operation: string, revision: string) =>5199+ runGitStdout(operation, cwd, ["rev-parse", "--verify", "--quiet", revision], true).pipe(5200+ Effect.map(trimAsciiWhitespace),5201+ );5202+ const beforeSha = yield* readCommit("GitVcsDriver.pullCurrentBranch.lockedSha", branchRef);5203+ const upstreamArgs = ["rev-parse", "--verify", `${refName}@{upstream}^{commit}`];5204+ const upstreamSha = yield* runGitStdout(5205+ "GitVcsDriver.pullCurrentBranch.upstreamSha",5206+ cwd,5207+ upstreamArgs,5208+ ).pipe(Effect.map(trimAsciiWhitespace));5209+ if (beforeSha === upstreamSha) {5210+ yield* unlock;5211+ return { status: "skipped_up_to_date" as const, refName, upstreamRef };5212+ }5213+ if (beforeSha.length > 0) {5214+ const ancestorArgs = ["merge-base", "--is-ancestor", beforeSha, upstreamSha];5215+ const ancestor = yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ancestorArgs, {5216+ allowNonZeroExit: true,5217+ });5218+ if (ancestor.exitCode !== 0) {@@ -5096,0 +5213,21 @@5213+ if (beforeSha.length > 0) {5214+ const ancestorArgs = ["merge-base", "--is-ancestor", beforeSha, upstreamSha];5215+ const ancestor = yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ancestorArgs, {5216+ allowNonZeroExit: true,5217+ });5218+ if (ancestor.exitCode !== 0) {5219+ return yield* new GitCommandError({5220+ ...gitCommandContext({5221+ operation: "GitVcsDriver.pullCurrentBranch.pull",5222+ cwd,5223+ args: ancestorArgs,5224+ }),5225+ detail: "Not possible to fast-forward, aborting.",5226+ exitCode: ancestor.exitCode,5227+ });5228+ }5229+ }5230+ // A deadline would kill git mid-checkout (e.g. a slow LFS smudge) and strand a half-updated tree.5231+ // A two-way merge refuses to overwrite local edits, as `git merge --ff-only` would.5232+ const checkout = (trees: ReadonlyArray<string>) =>5233+ executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["read-tree", "-m", "-u", ...trees], {@@ -5096,0 +5222,21 @@5222+ cwd,5223+ args: ancestorArgs,5224+ }),5225+ detail: "Not possible to fast-forward, aborting.",5226+ exitCode: ancestor.exitCode,5227+ });5228+ }5229+ }5230+ // A deadline would kill git mid-checkout (e.g. a slow LFS smudge) and strand a half-updated tree.5231+ // A two-way merge refuses to overwrite local edits, as `git merge --ff-only` would.5232+ const checkout = (trees: ReadonlyArray<string>) =>5233+ executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["read-tree", "-m", "-u", ...trees], {5234+ env: { GIT_INDEX_FILE: lockPath },5235+ timeoutMs: null,5236+ appendTruncationMarker: true,5237+ fallbackErrorDetail: "git pull failed",5238+ });5239+ // Uninterruptible so a cancelled pull cannot leave the files, branch and index apart.5240+ yield* Effect.uninterruptible(5241+ Effect.gen(function* () {5242+ yield* checkout(beforeSha.length > 0 ? [beforeSha, upstreamSha] : [upstreamSha]);
Pushedba1288b
- @@ -5096,0 +5205,21 @@5205+ cwd,5206+ upstreamArgs,5207+ ).pipe(Effect.map(trimAsciiWhitespace));5208+ const isAncestor = (ancestor: string, descendant: string) =>5209+ executeGit(5210+ "GitVcsDriver.pullCurrentBranch.isAncestor",5211+ cwd,5212+ ["merge-base", "--is-ancestor", ancestor, descendant],5213+ { allowNonZeroExit: true },5214+ ).pipe(Effect.map((result) => result.exitCode === 0));5215+ if (5216+ beforeSha === upstreamSha ||5217+ (beforeSha.length > 0 && (yield* isAncestor(upstreamSha, beforeSha)))5218+ ) {5219+ yield* unlock;5220+ return { status: "skipped_up_to_date" as const, refName, upstreamRef };5221+ }5222+ // `git merge` refuses to start while a merge or cherry-pick is unfinished; plumbing does not.5223+ for (const operationHead of ["MERGE_HEAD", "CHERRY_PICK_HEAD"]) {5224+ if (5225+ (yield* readCommit("GitVcsDriver.pullCurrentBranch.operationHead", operationHead)) !== ""@@ -5096,0 +5260,21 @@5260+ appendTruncationMarker: true,5261+ fallbackErrorDetail: "git pull failed",5262+ });5263+ // Uninterruptible so a cancelled pull cannot leave the files, branch and index apart.5264+ yield* Effect.uninterruptible(5265+ Effect.gen(function* () {5266+ yield* checkout(beforeSha.length > 0 ? [beforeSha, upstreamSha] : [upstreamSha]);5267+ const revert =5268+ beforeSha.length > 05269+ ? checkout([upstreamSha, beforeSha]).pipe(Effect.ignore)5270+ : Effect.void;5271+ // A switch already past its index write can repoint HEAD until the branch moves.5272+ const headRefAfterCheckout = yield* runGitStdout(5273+ "GitVcsDriver.pullCurrentBranch.headRef",5274+ cwd,5275+ ["symbolic-ref", "--quiet", "HEAD"],5276+ true,5277+ ).pipe(Effect.map(trimAsciiWhitespace));5278+ if (headRefAfterCheckout !== branchRef) {5279+ yield* revert;5280+ return yield* branchChanged;
Pushed5fd8b6b
- @@ -5096,0 +5247,21 @@5247+ args: ancestorArgs,5248+ }),5249+ detail: "Not possible to fast-forward, aborting.",5250+ exitCode: ancestor.exitCode,5251+ });5252+ }5253+ }5254+ // A deadline would kill git mid-checkout (e.g. a slow LFS smudge) and strand a half-updated tree.5255+ // A two-way merge refuses to overwrite local edits, as `git merge --ff-only` would.5256+ const checkout = (trees: ReadonlyArray<string>) =>5257+ executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["read-tree", "-m", "-u", ...trees], {5258+ env: { GIT_INDEX_FILE: lockPath },5259+ timeoutMs: null,5260+ appendTruncationMarker: true,5261+ fallbackErrorDetail: "git pull failed",5262+ });5263+ // Uninterruptible so a cancelled pull cannot leave the files, branch and index apart.5264+ yield* Effect.uninterruptible(5265+ Effect.gen(function* () {5266+ yield* checkout(beforeSha.length > 0 ? [beforeSha, upstreamSha] : [upstreamSha]);5267+ const revert =
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes