From cd06965fd73490f8b769281858c496d262aecc35 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Thu, 26 Sep 2024 17:18:51 +0200 Subject: [PATCH] Swallow "remote not found" error on git before 2.30 (#514) 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 --- src/lib/git.test.ts | 18 +++++++++++++++--- src/lib/git.ts | 13 +++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 7acafd2e..4db7a7b5 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -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", @@ -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", }); diff --git a/src/lib/git.ts b/src/lib/git.ts index 93932a60..4dc1688b 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -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; }