Skip to content

Commit a0ec1b4

Browse files
committed
fix(github): guard pushFiles against empty diff from diffCommitTree
GitHub's POST /git/trees rejects an empty tree array with 422 "Invalid tree info". While prepareCommit already prevents no-op commits from reaching pushFiles, add a defensive early return so the failure mode is a clear log message instead of a swallowed 422. Made-with: Cursor
1 parent bb4ba2d commit a0ec1b4

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/modules/platform/github/index.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5090,7 +5090,14 @@ describe('modules/platform/github/index', () => {
50905090
git.getCommitTreeSha.mockResolvedValue(
50915091
'0000000000000000000000000000000000000000' as LongCommitSha,
50925092
);
5093-
git.diffCommitTree.mockResolvedValue([]);
5093+
git.diffCommitTree.mockResolvedValue([
5094+
{
5095+
path: 'foo.bar',
5096+
mode: '100644',
5097+
type: 'blob',
5098+
sha: 'abc0000000000000000000000000000000000000',
5099+
},
5100+
]);
50945101
});
50955102

50965103
it('returns null if pre-commit phase has failed', async () => {
@@ -5128,6 +5135,21 @@ describe('modules/platform/github/index', () => {
51285135
expect(res).toBeNull();
51295136
});
51305137

5138+
it('returns null when diff is empty', async () => {
5139+
const scope = httpMock.scope(githubApiHost);
5140+
initRepoMock(scope, 'some/repo');
5141+
await github.initRepo({ repository: 'some/repo' });
5142+
git.diffCommitTree.mockResolvedValueOnce([]);
5143+
5144+
const res = await github.commitFiles({
5145+
branchName: 'foo/bar',
5146+
files: [{ type: 'addition', path: 'foo.bar', contents: 'foobar' }],
5147+
message: 'Foobar',
5148+
});
5149+
5150+
expect(res).toBeNull();
5151+
});
5152+
51315153
it('commits and returns SHA string', async () => {
51325154
const scope = httpMock.scope(githubApiHost);
51335155

lib/modules/platform/github/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,14 @@ async function pushFiles(
21712171
const baseTreeSha = await getCommitTreeSha(parentCommitSha);
21722172
const treeItems = await diffCommitTree(parentCommitSha, commitSha);
21732173

2174+
if (treeItems.length === 0) {
2175+
logger.debug(
2176+
{ branchName },
2177+
'Platform-native commit: no changed files between commits',
2178+
);
2179+
return null;
2180+
}
2181+
21742182
const treeRes = await githubApi.postJson<{ sha: string }>(
21752183
`/repos/${config.repository}/git/trees`,
21762184
{ body: { base_tree: baseTreeSha, tree: treeItems } },

0 commit comments

Comments
 (0)