From 19001ab0f54356273c57e063461bcb0bedaf25ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 2 May 2021 11:18:01 +0200 Subject: [PATCH 1/4] Avoid an infinite loop when git commands fail to execute when Changesets try to retrieve commits that added files --- .changeset/thirty-jobs-teach.md | 6 ++++++ packages/git/src/index.ts | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .changeset/thirty-jobs-teach.md diff --git a/.changeset/thirty-jobs-teach.md b/.changeset/thirty-jobs-teach.md new file mode 100644 index 000000000..554f40587 --- /dev/null +++ b/.changeset/thirty-jobs-teach.md @@ -0,0 +1,6 @@ +--- +"@changesets/git": patch +"@changesets/cli": patch +--- + +Avoid an infinite loop when git commands fail to execute when Changesets try to retrieve commits that added files. diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts index 92c5bbd11..176efe7a7 100644 --- a/packages/git/src/index.ts +++ b/packages/git/src/index.ts @@ -126,6 +126,9 @@ async function getCommitsThatAddFiles( ], { cwd } ); + if (logResult.code !== 0) { + throw new Error(logResult.stderr.toString()); + } const [commitSha, parentSha] = logResult.stdout.toString().split(":"); return { path: gitPath, commitSha, parentSha }; } @@ -136,6 +139,9 @@ async function getCommitsThatAddFiles( ["rev-parse", "--is-shallow-repository"], { cwd } ); + if (gitCmd.code !== 0) { + throw new Error(gitCmd.stderr.toString()); + } const isShallowRepoOutput = gitCmd.stdout.toString().trim(); @@ -144,11 +150,13 @@ async function getCommitsThatAddFiles( // In that case, we'll test for the existence of .git/shallow. // Firstly, find the .git folder for the repo; note that this will be relative to the repo dir - const gitDir = ( - await spawn("git", ["rev-parse", "--git-dir"], { cwd }) - ).stdout - .toString() - .trim(); + const gitDirCmd = await spawn("git", ["rev-parse", "--git-dir"], { cwd }); + + if (gitDirCmd.code !== 0) { + throw new Error(gitDirCmd.stderr.toString()); + } + + const gitDir = gitDirCmd.stdout.toString().trim(); const fullGitDir = path.resolve(cwd, gitDir); @@ -162,7 +170,10 @@ async function getCommitsThatAddFiles( } async function deepenCloneBy(by: number) { - await spawn("git", ["fetch", `--deepen=${by}`], { cwd }); + const cmd = await spawn("git", ["fetch", `--deepen=${by}`], { cwd }); + if (cmd.code !== 0) { + throw new Error(cmd.stderr.toString()); + } } } From 42d4e3830e06c2a4873e1329bc455005df52533d Mon Sep 17 00:00:00 2001 From: Leonardo Metzger Date: Tue, 18 Nov 2025 13:13:25 -0300 Subject: [PATCH 2/4] Remove a invalid error handler from git command (#611) --- packages/git/src/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts index 176efe7a7..753a8196f 100644 --- a/packages/git/src/index.ts +++ b/packages/git/src/index.ts @@ -126,9 +126,7 @@ async function getCommitsThatAddFiles( ], { cwd } ); - if (logResult.code !== 0) { - throw new Error(logResult.stderr.toString()); - } + const [commitSha, parentSha] = logResult.stdout.toString().split(":"); return { path: gitPath, commitSha, parentSha }; } From 5c6a3a556e5d643df15cfb5d11195820854aed4d Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 26 Jun 2026 10:48:54 +0800 Subject: [PATCH 3/4] Revert impossibly non-0 cases --- packages/git/src/index.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts index e5094ab44..8066a43d4 100644 --- a/packages/git/src/index.ts +++ b/packages/git/src/index.ts @@ -142,28 +142,26 @@ export async function getCommitsThatAddFiles( } export async function isRepoShallow({ cwd }: { cwd: string }) { - const gitCmd = await exec("git", ["rev-parse", "--is-shallow-repository"], { - nodeOptions: { cwd }, - }); - - if (gitCmd.exitCode !== 0) { - throw new Error(gitCmd.stderr.toString()); - } - - const isShallowRepoOutput = gitCmd.stdout.toString().trim(); + const isShallowRepoOutput = ( + await exec("git", ["rev-parse", "--is-shallow-repository"], { + nodeOptions: { cwd }, + }) + ).stdout + .toString() + .trim(); if (isShallowRepoOutput === "--is-shallow-repository") { // We have an old version of Git (<2.15) which doesn't support `rev-parse --is-shallow-repository` // In that case, we'll test for the existence of .git/shallow. // Firstly, find the .git folder for the repo; note that this will be relative to the repo dir - const gitDirCmd = await exec("git", ["rev-parse", "--git-dir"], { nodeOptions: { cwd } }); - - if (gitDirCmd.exitCode !== 0) { - throw new Error(gitDirCmd.stderr.toString()); - } - - const gitDir = gitDirCmd.stdout.toString().trim(); + const gitDir = ( + await exec("git", ["rev-parse", "--git-dir"], { + nodeOptions: { cwd }, + }) + ).stdout + .toString() + .trim(); const fullGitDir = path.resolve(cwd, gitDir); @@ -182,7 +180,9 @@ export async function isRepoShallow({ cwd }: { cwd: string }) { } export async function deepenCloneBy({ by, cwd }: { by: number; cwd: string }) { - const cmd = await exec("git", ["fetch", `--deepen=${by}`], { nodeOptions: { cwd } }); + const cmd = await exec("git", ["fetch", `--deepen=${by}`], { + nodeOptions: { cwd }, + }); if (cmd.exitCode !== 0) { throw new Error(cmd.stderr.toString()); } From 8ac0acd27e605255ee4b3c98dc5a2751cb5987da Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 26 Jun 2026 10:49:34 +0800 Subject: [PATCH 4/4] Update --- packages/git/src/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/git/src/index.ts b/packages/git/src/index.ts index 8066a43d4..bb632760c 100644 --- a/packages/git/src/index.ts +++ b/packages/git/src/index.ts @@ -156,9 +156,7 @@ export async function isRepoShallow({ cwd }: { cwd: string }) { // Firstly, find the .git folder for the repo; note that this will be relative to the repo dir const gitDir = ( - await exec("git", ["rev-parse", "--git-dir"], { - nodeOptions: { cwd }, - }) + await exec("git", ["rev-parse", "--git-dir"], { nodeOptions: { cwd } }) ).stdout .toString() .trim();