Skip to content

Commit

Permalink
Swallow "remote not found" error on git before 2.30 (#514)
Browse files Browse the repository at this point in the history
Git 2.30.0 introduced a dedicated exit code (2) for when `git remote rm`
failed because the remote does not exist. Earlier versions of git
always returned code 128, and only parsing stderr could tell the error
conditions apart.

Support all versions of git by checking for both exit code 2, and exit
code 128 with the specific error message. The exit code 2 check remains
more robust against localized output, hence it's kept in addition to the
output parsing path that was removed in #505.

Fixes #509
  • Loading branch information
imphil authored Sep 26, 2024
1 parent 79246b2 commit cd06965
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/lib/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ describe('deleteRemote', () => {
repoName: 'kibana',
} as ValidConfigOptions;

it('should swallow "no such remote" error', async () => {
it('should swallow "no such remote" error on git before 2.30.0', async () => {
const err = new childProcess.SpawnError({
code: 2,
code: 128,
cmdArgs: [],
stdout: '',
stderr: "fatal: No such remote: 'my-remote'\n",
Expand All @@ -376,11 +376,23 @@ describe('deleteRemote', () => {
await expect(await deleteRemote(options, remoteName)).toBe(undefined);
});

it('should swallow "no such remote" error, even if it is not in English', async () => {
it('should swallow "no such remote" error on git 2.30.0 or later', async () => {
const err = new childProcess.SpawnError({
code: 2,
cmdArgs: [],
stdout: '',
stderr: "fatal: No such remote: 'my-remote'\n",
});

jest.spyOn(childProcess, 'spawnPromise').mockRejectedValueOnce(err);
await expect(await deleteRemote(options, remoteName)).toBe(undefined);
});

it('should swallow "no such remote" error on git 2.30.0+, even if it is not in English', async () => {
const err = new childProcess.SpawnError({
code: 2, // returned only by git 2.30.0 or later, earlier versions returned 128
cmdArgs: [],
stdout: '',
stderr: "Fehler: Remote-Repository nicht gefunden: 'my-remote'\n",
});

Expand Down
13 changes: 9 additions & 4 deletions src/lib/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,15 @@ export async function deleteRemote(
} catch (e) {
const isSpawnError = e instanceof SpawnError;

// Swallow the "remote does not exist" failure, indicated by a return
// code of 2. From `git help remote`: "When subcommands such as add, rename,
// and remove can’t find the remote in question, the exit status is 2."
if (isSpawnError && e.context.code == 2) {
// Swallow the "remote does not exist" failure.
// Since git 2.30.0, this failure is indicated by the specific exit code 2.
// In earlier versions, the exit code is 128 and only the error message can
// tell the problems apart.
if (
isSpawnError &&
(e.context.code == 2 ||
(e.context.code == 128 && e.context.stderr.includes('No such remote')))
) {
return;
}

Expand Down

0 comments on commit cd06965

Please sign in to comment.