Skip to content

Commit 088ffc4

Browse files
committed
refactor: create list only if there's an error and update tests
1 parent ca86b71 commit 088ffc4

File tree

3 files changed

+67
-51
lines changed

3 files changed

+67
-51
lines changed

src/proxy/processors/push-action/checkHiddenCommits.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,61 @@ const exec = async (req: any, action: Action): Promise<Action> => {
88
try {
99
const repoPath = `${action.proxyGitPath}/${action.repoName}`;
1010

11-
const introducedCommits = new Set<string>();
12-
13-
// Retrieve the single ref update
1411
const oldOid = action.commitFrom;
1512
const newOid = action.commitTo;
1613
if (!oldOid || !newOid) {
1714
throw new Error('Both action.commitFrom and action.commitTo must be defined');
1815
}
1916

20-
const revisionRange: string =
17+
// build introducedCommits set
18+
const introducedCommits = new Set<string>();
19+
const revRange =
2120
oldOid === '0000000000000000000000000000000000000000' ? newOid : `${oldOid}..${newOid}`;
22-
23-
const revListOutput = spawnSync('git', ['rev-list', revisionRange], {
24-
cwd: repoPath,
25-
encoding: 'utf-8',
26-
}).stdout;
27-
revListOutput
28-
.trim()
21+
const revList = spawnSync('git', ['rev-list', revRange], { cwd: repoPath, encoding: 'utf-8' })
22+
.stdout.trim()
2923
.split('\n')
30-
.filter(Boolean)
31-
.forEach((sha) => introducedCommits.add(sha));
24+
.filter(Boolean);
25+
revList.forEach((sha) => introducedCommits.add(sha));
3226
step.log(`Total introduced commits: ${introducedCommits.size}`);
33-
const packPath = path.join('.git', 'objects', 'pack');
3427

28+
// build packCommits set
29+
const packPath = path.join('.git', 'objects', 'pack');
3530
const packCommits = new Set<string>();
36-
3731
(action.newIdxFiles || []).forEach((idxFile) => {
3832
const idxPath = path.join(packPath, idxFile);
3933
const out = spawnSync('git', ['verify-pack', '-v', idxPath], {
4034
cwd: repoPath,
4135
encoding: 'utf-8',
42-
}).stdout;
43-
44-
out
45-
.trim()
46-
.split('\n')
47-
.forEach((line) => {
48-
const [sha, type] = line.split(/\s+/);
49-
if (type === 'commit') packCommits.add(sha);
50-
});
36+
})
37+
.stdout.trim()
38+
.split('\n');
39+
out.forEach((line) => {
40+
const [sha, type] = line.split(/\s+/);
41+
if (type === 'commit') packCommits.add(sha);
42+
});
5143
});
52-
step.log(`Commits nel pack: ${packCommits.size}`);
53-
console.log('Pack commits:', packCommits);
44+
step.log(`Total commits in the pack: ${packCommits.size}`);
5445

55-
const referenced: string[] = [];
56-
const unreferenced: string[] = [];
57-
[...packCommits].forEach((sha) => {
58-
if (introducedCommits.has(sha)) referenced.push(sha);
59-
else unreferenced.push(sha);
60-
});
46+
// subset check
47+
const isSubset = [...packCommits].every((sha) => introducedCommits.has(sha));
48+
if (!isSubset) {
49+
// build detailed lists
50+
const unreferenced = [...packCommits].filter((sha) => !introducedCommits.has(sha));
51+
const referenced = [...packCommits].filter((sha) => introducedCommits.has(sha));
6152

62-
step.log(`✅ Referenced commits: ${referenced.length}`);
63-
step.log(`❌ Unreferenced commits: ${unreferenced.length}`);
53+
step.log(`✅ Referenced commits: ${referenced.length}`);
54+
step.log(`❌ Unreferenced commits: ${unreferenced.length}`);
6455

65-
if (unreferenced.length > 0) {
6656
step.setError(
6757
`Unreferenced commits in pack (${unreferenced.length}): ${unreferenced.join(', ')}`,
6858
);
6959
action.error = true;
60+
step.setContent(`Referenced: ${referenced.length}, Unreferenced: ${unreferenced.length}`);
61+
} else {
62+
// all good, no logging of individual SHAs needed
63+
step.log('All pack commits are referenced in the introduced range.');
64+
step.setContent(`All ${packCommits.size} pack commits are within introduced commits.`);
7065
}
71-
step.setContent(`Referenced: ${referenced.length}, Unreferenced: ${unreferenced.length}`);
7266
} catch (e: any) {
7367
step.setError(e.message);
7468
throw e;

src/proxy/processors/push-action/writePack.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import fs from 'fs';
66
const exec = async (req: any, action: Action) => {
77
const step = new Step('writePack');
88
try {
9-
const cmd = `git receive-pack ${action.repoName}`;
10-
step.log(`executing ${cmd}`);
119
if (!action.proxyGitPath || !action.repoName) {
1210
throw new Error('proxyGitPath and repoName must be defined');
1311
}

test/checkHiddenCommit.test.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,46 @@ describe('checkHiddenCommits.exec', () => {
3232
sandbox.restore();
3333
});
3434

35-
it('reports all commits unreferenced and sets error=true', async () => {
35+
it.only('reports all commits unreferenced and sets error=true', async () => {
36+
const COMMIT_1 = 'deadbeef';
37+
const COMMIT_2 = 'cafebabe';
38+
3639
// 1) rev-list → no introduced commits
3740
// 2) verify-pack → two commits in pack
38-
spawnSyncStub.onFirstCall().returns({ stdout: '' }).onSecondCall().returns({
39-
stdout: 'deadbeef commit 100 1\ncafebabe commit 100 2\n',
40-
});
41+
spawnSyncStub
42+
.onFirstCall()
43+
.returns({ stdout: '' })
44+
.onSecondCall()
45+
.returns({
46+
stdout: `${COMMIT_1} commit 100 1\n${COMMIT_2} commit 100 2\n`,
47+
});
4148

4249
readdirSyncStub.returns(['pack-test.idx']);
4350

4451
await checkHidden({ body: '' }, action);
4552

4653
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
47-
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 2');
54+
expect(step.logs).to.include(`checkHiddenCommits - ✅ Referenced commits: 0`);
55+
expect(step.logs).to.include(`checkHiddenCommits - ❌ Unreferenced commits: 2`);
56+
expect(step.logs).to.include(
57+
`checkHiddenCommits - Unreferenced commits in pack (2): ${COMMIT_1}, ${COMMIT_2}`,
58+
);
4859
expect(action.error).to.be.true;
4960
});
5061

51-
it('mixes referenced & unreferenced correctly', async () => {
62+
it.only('mixes referenced & unreferenced correctly', async () => {
63+
const COMMIT_1 = 'deadbeef';
64+
const COMMIT_2 = 'cafebabe';
65+
5266
// 1) git rev-list → introduces one commit “deadbeef”
5367
// 2) git verify-pack → the pack contains two commits
54-
spawnSyncStub.onFirstCall().returns({ stdout: 'deadbeef\n' }).onSecondCall().returns({
55-
stdout: 'deadbeef commit 100 1\ncafebabe commit 100 2\n',
56-
});
68+
spawnSyncStub
69+
.onFirstCall()
70+
.returns({ stdout: `${COMMIT_1}\n` })
71+
.onSecondCall()
72+
.returns({
73+
stdout: `${COMMIT_1} commit 100 1\n${COMMIT_2} commit 100 2\n`,
74+
});
5775

5876
readdirSyncStub.returns(['pack-test.idx']);
5977

@@ -62,6 +80,9 @@ describe('checkHiddenCommits.exec', () => {
6280
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
6381
expect(step.logs).to.include('checkHiddenCommits - ✅ Referenced commits: 1');
6482
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 1');
83+
expect(step.logs).to.include(
84+
`checkHiddenCommits - Unreferenced commits in pack (1): ${COMMIT_2}`,
85+
);
6586
expect(action.error).to.be.true;
6687
});
6788

@@ -75,14 +96,17 @@ describe('checkHiddenCommits.exec', () => {
7596
readdirSyncStub.returns(['pack-test.idx']);
7697

7798
await checkHidden({ body: '' }, action);
78-
7999
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
80-
expect(step.logs).to.include('checkHiddenCommits - ✅ Referenced commits: 2');
81-
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 0');
100+
101+
expect(step.logs).to.include('checkHiddenCommits - Total introduced commits: 2');
102+
expect(step.logs).to.include('checkHiddenCommits - Total commits in the pack: 2');
103+
expect(step.logs).to.include(
104+
'checkHiddenCommits - All pack commits are referenced in the introduced range.',
105+
);
82106
expect(action.error).to.be.false;
83107
});
84108

85-
it('throws if commitFrom or commitTo is missing', async () => {
109+
it.only('throws if commitFrom or commitTo is missing', async () => {
86110
delete action.commitFrom;
87111

88112
try {

0 commit comments

Comments
 (0)