Skip to content

Commit fb7f087

Browse files
t3dotggfixfonclaude
authored
fix(server): pushes the remote refuses say why instead of a generic failure (pingdotgg#22904)
Co-authored-by: fixfon <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 22b89ef commit fb7f087

3 files changed

Lines changed: 119 additions & 4 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ const makeSuccessfulHandle = (stdout: string) =>
8080
getOutputFd: () => Stream.empty,
8181
});
8282

83+
const makeFailedHandle = (stderr: string) =>
84+
ChildProcessSpawner.makeHandle({
85+
...makeNonRepositoryHandle(),
86+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
87+
stderr: Stream.encodeText(Stream.make(stderr)),
88+
});
89+
8390
const makeTmpDir = (
8491
prefix = "git-vcs-driver-test-",
8592
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
@@ -803,6 +810,88 @@ it.effect("retries a fetch that lost a ref update to another Git process", () =>
803810
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
804811
);
805812

813+
describe("rejected git refs", () => {
814+
const remoteUrl = "https://user:[email protected]/org/secret-repo.git";
815+
const failedPush = `error: failed to push some refs to '${remoteUrl}'`;
816+
const leakedText = [
817+
"token-123",
818+
"secret-repo",
819+
"git.example.invalid",
820+
"private-branch",
821+
"workflow scope",
822+
];
823+
824+
const failWith = (stderr: string) => {
825+
const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeFailedHandle(stderr)));
826+
const layer = GitVcsDriver.layer.pipe(
827+
Layer.provide(layerServerConfig),
828+
Layer.provideMerge(
829+
Layer.merge(
830+
NodeServices.layer,
831+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
832+
),
833+
),
834+
);
835+
return Effect.gen(function* () {
836+
const driver = yield* GitVcsDriver.GitVcsDriver;
837+
return yield* driver
838+
.execute({
839+
operation: "GitVcsDriver.test.rejectedRef",
840+
cwd: "/repo",
841+
args: ["push", "origin", "HEAD"],
842+
})
843+
.pipe(Effect.flip);
844+
}).pipe(Effect.provide(layer));
845+
};
846+
847+
it.effect.each([
848+
{
849+
label: "a ref the remote refused",
850+
stderr: [
851+
"remote: Unable to determine if workflow can be created; workflow scope may be required.",
852+
`To ${remoteUrl}`,
853+
" ! [remote rejected] private-branch -> private-branch (workflow scope may be required.)",
854+
failedPush,
855+
],
856+
reason: "push_rejected_by_remote" as const,
857+
detail: "The remote rejected the push.",
858+
},
859+
{
860+
label: "a non-fast-forward push",
861+
stderr: [
862+
`To ${remoteUrl}`,
863+
" ! [rejected] private-branch -> private-branch (non-fast-forward)",
864+
failedPush,
865+
],
866+
reason: "push_rejected_non_fast_forward" as const,
867+
detail: "Pull or rebase",
868+
},
869+
])("names $label without quoting stderr", ({ stderr, reason, detail }) =>
870+
Effect.gen(function* () {
871+
const error = yield* failWith(stderr.join("\n"));
872+
873+
assert.deepInclude(error, { reason });
874+
assert.include(error.detail, detail);
875+
for (const text of leakedText) assert.notInclude(error.message, text);
876+
}),
877+
);
878+
879+
// A fetch into a diverged local branch prints the same rejected line, but
880+
// never "failed to push some refs".
881+
it.effect("leaves a rejected fetch unclassified", () =>
882+
Effect.gen(function* () {
883+
const error = yield* failWith(
884+
[`From ${remoteUrl}`, " ! [rejected] main -> side (non-fast-forward)"].join(
885+
"\n",
886+
),
887+
);
888+
889+
assert.equal(error.reason, undefined);
890+
assert.equal(error.detail, "Git command exited with a non-zero status.");
891+
}),
892+
);
893+
});
894+
806895
it.layer(layerTest)("GitVcsDriver core integration", (it) => {
807896
describe("structured errors", () => {
808897
it.effect("does not retain git arguments or stderr in command failures", () =>
@@ -1071,7 +1160,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
10711160
}),
10721161
);
10731162

1074-
it.effect("does not read remote hook output as a git failure reason", () =>
1163+
it.effect("reads a hook-declined push from git's rejection line, not the hook's output", () =>
10751164
Effect.gen(function* () {
10761165
const parent = yield* makeTmpDir();
10771166
const pathService = yield* Path.Path;
@@ -1100,7 +1189,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
11001189
})
11011190
.pipe(Effect.flip);
11021191

1103-
assert.notProperty(error, "reason");
1192+
assert.deepInclude(error, { reason: "push_rejected_by_remote" });
11041193
assert.notInclude(error.message, "(authentication_failed)");
11051194
}),
11061195
);

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,32 @@ const GIT_FAILURE_REASON_PATTERNS: ReadonlyArray<readonly [RegExp, GitCommandFai
456456
/^fatal: (?:invalid reference: |not a valid object name|needed a single revision|bad revision |'.+' is not a commit and a branch '.+' cannot be created from it)/im,
457457
"ref_not_found",
458458
],
459+
// `[remote rejected]` is only printed for a push. The remote's explanation
460+
// follows in parentheses; it is server text, so it is never quoted.
461+
[/^[ \t]*![ \t]+\[remote rejected\]/im, "push_rejected_by_remote"],
462+
// A fetch into a diverged local branch prints the same `[rejected]` line, but
463+
// only a push goes on to report "failed to push some refs".
464+
[
465+
/^[ \t]*![ \t]+\[rejected\][^\n]*\((?:non-fast-forward|fetch first)\)[\s\S]*^error: failed to push some refs/im,
466+
"push_rejected_non_fast_forward",
467+
],
459468
];
460469

470+
// Reasons the user can act on get a sentence in place of the generic detail.
471+
const GIT_FAILURE_REASON_DETAILS: Partial<Record<GitCommandFailureReason, string>> = {
472+
push_rejected_by_remote:
473+
"The remote rejected the push. Push this branch from a terminal to see the server's message.",
474+
push_rejected_non_fast_forward:
475+
"The remote has commits this branch doesn't have. Pull or rebase, then push again.",
476+
};
477+
478+
function gitFailureDetail(reason: GitCommandFailureReason | null): string {
479+
return (
480+
(reason === null ? undefined : GIT_FAILURE_REASON_DETAILS[reason]) ??
481+
"Git command exited with a non-zero status."
482+
);
483+
}
484+
461485
// Existing non-commit objects share the missing-ref fatal diagnostic; Git
462486
// distinguishes them with a separate error line.
463487
const GIT_NOT_A_COMMIT_PATTERN = /^error: .+(?:, not a commit|expected commit type)/im;
@@ -1031,7 +1055,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10311055
return yield* new GitCommandError({
10321056
...gitCommandContext(commandInput),
10331057
...(reason === null ? {} : { reason }),
1034-
detail: "Git command exited with a non-zero status.",
1058+
detail: gitFailureDetail(reason),
10351059
exitCode,
10361060
stdoutLength: stdout.text.length,
10371061
stderrLength: stderr.text.length,
@@ -1123,7 +1147,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11231147
new GitCommandError({
11241148
...gitCommandContext({ operation, cwd, args }),
11251149
...(reason === null ? {} : { reason }),
1126-
detail: options.fallbackErrorDetail ?? "Git command exited with a non-zero status.",
1150+
detail: options.fallbackErrorDetail ?? gitFailureDetail(reason),
11271151
...(result.exitCode === null ? {} : { exitCode: result.exitCode }),
11281152
stdoutLength: result.stdout.length,
11291153
stderrLength: result.stderr.length,

‎packages/contracts/src/git.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ export const GitCommandFailureReason = Schema.Literals([
361361
"host_key_unverified",
362362
"not_a_repository",
363363
"path_already_exists",
364+
"push_rejected_by_remote",
365+
"push_rejected_non_fast_forward",
364366
"ref_not_found",
365367
"remote_unreachable",
366368
"tag_would_be_clobbered",

0 commit comments

Comments
 (0)