Skip to content

Commit 87f73b4

Browse files
committed
refactor: use commitFrom and commitTo in checkHiddenCOmmits
1 parent 9c1449f commit 87f73b4

File tree

4 files changed

+43
-55
lines changed

4 files changed

+43
-55
lines changed

src/proxy/actions/Action.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ export interface Commit {
1414
commitTS?: string; // TODO: Normalize this to commitTimestamp
1515
commitTimestamp?: string;
1616
}
17-
/**
18-
* Represents an UpdatedRef in the action.
19-
*/
20-
export interface UpdatedRef {
21-
ref: string;
22-
oldOid: string;
23-
newOid: string;
24-
}
2517

2618
/**
2719
* Class representing a Push.
@@ -56,7 +48,6 @@ class Action {
5648
attestation?: string;
5749
lastStep?: Step;
5850
proxyGitPath?: string;
59-
updatedRefs?: UpdatedRef[];
6051
newIdxFiles?: string[];
6152

6253
/**

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ const exec = async (req: any, action: Action): Promise<Action> => {
77

88
try {
99
const repoPath = `${action.proxyGitPath}/${action.repoName}`;
10-
console.log(`repoPath: ${repoPath}`);
1110

1211
const introducedCommits = new Set<string>();
1312

14-
action.updatedRefs?.forEach(({ ref, oldOid, newOid }) => {
15-
const revRange =
16-
oldOid === '0000000000000000000000000000000000000000' ? newOid : `${oldOid}..${newOid}`;
17-
18-
const result = spawnSync('git', ['rev-list', revRange], {
19-
cwd: repoPath,
20-
encoding: 'utf-8',
21-
});
22-
23-
result.stdout
24-
.trim()
25-
.split('\n')
26-
.forEach((c) => {
27-
if (c) introducedCommits.add(c);
28-
});
29-
});
13+
// Retrieve the single ref update
14+
const oldOid = action.commitFrom;
15+
const newOid = action.commitTo;
16+
if (!oldOid || !newOid) {
17+
throw new Error('Both action.commitFrom and action.commitTo must be defined');
18+
}
3019

20+
const revisionRange: string =
21+
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()
29+
.split('\n')
30+
.filter(Boolean)
31+
.forEach((sha) => introducedCommits.add(sha));
3132
step.log(`Total introduced commits: ${introducedCommits.size}`);
32-
step.log(`Introduced commits: ${[...introducedCommits].join(', ')}`);
33-
3433
const packPath = path.join('.git', 'objects', 'pack');
3534

3635
const packCommits = new Set<string>();
@@ -52,7 +51,6 @@ const exec = async (req: any, action: Action): Promise<Action> => {
5251
});
5352
step.log(`Commits nel pack: ${packCommits.size}`);
5453
console.log('Pack commits:', packCommits);
55-
console.log('Introduced commits:', introducedCommits);
5654

5755
const referenced: string[] = [];
5856
const unreferenced: string[] = [];

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@ async function exec(req: any, action: Action): Promise<Action> {
2626
if (refUpdates.length !== 1) {
2727
step.log('Invalid number of branch updates.');
2828
step.log(`Expected 1, but got ${refUpdates.length}`);
29-
step.setError('Your push has been blocked. Please make sure you are pushing to a single branch.');
29+
step.setError(
30+
'Your push has been blocked. Please make sure you are pushing to a single branch.',
31+
);
3032
action.addStep(step);
3133
return action;
3234
}
3335

34-
const cleanedUpdates = refUpdates.map((line) => {
35-
const [oldOid, newOid, refWithNull] = line.split(' ');
36-
const ref = refWithNull.replace(/\0.*/, '').trim();
37-
return { oldOid, newOid, ref };
38-
});
36+
const parts = refUpdates[0].split(' ');
37+
const [oldOid, newOid, rawRef] = parts;
3938

40-
action.updatedRefs = cleanedUpdates;
39+
const branch = rawRef.replace(/\0.*/, '').trim();
4140

42-
const { oldOid, newOid, ref } = cleanedUpdates[0];
43-
action.branch = ref;
41+
action.branch = branch;
4442
action.setCommit(oldOid, newOid);
4543

4644
// Check if the offset is valid and if there's data after it
@@ -66,13 +64,13 @@ async function exec(req: any, action: Action): Promise<Action> {
6664

6765
action.commitData = getCommitData(contents as any);
6866
if (action.commitData.length === 0) {
69-
step.log('No commit data found when parsing push.')
67+
step.log('No commit data found when parsing push.');
7068
} else {
7169
if (action.commitFrom === '0000000000000000000000000000000000000000') {
7270
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
7371
}
7472
const user = action.commitData[action.commitData.length - 1].committer;
75-
action.user = user;
73+
action.user = user;
7674
}
7775

7876
step.content = {
@@ -86,20 +84,24 @@ async function exec(req: any, action: Action): Promise<Action> {
8684
action.addStep(step);
8785
}
8886
return action;
89-
};
87+
}
9088

9189
/**
9290
* Parses the name, email, and timestamp from an author or committer line.
93-
*
91+
*
9492
* Timestamp including timezone offset is required.
9593
* @param {string} line - The line to parse.
9694
* @return {Object} An object containing the name, email, and timestamp.
9795
*/
98-
const parsePersonLine = (line: string): { name: string; email: string; timestamp: string } | null => {
96+
const parsePersonLine = (
97+
line: string,
98+
): { name: string; email: string; timestamp: string } | null => {
9999
const personRegex = /^(.*?) <(.*?)> (\d+) ([+-]\d+)$/;
100100
const match = line.match(personRegex);
101101
if (!match) {
102-
throw new Error(`Failed to parse person line: ${line}. Make sure to include a name, email, timestamp and timezone offset.`);
102+
throw new Error(
103+
`Failed to parse person line: ${line}. Make sure to include a name, email, timestamp and timezone offset.`,
104+
);
103105
}
104106
return { name: match[1].trim(), email: match[2], timestamp: match[3] };
105107
};
@@ -173,7 +175,10 @@ const getCommitData = (contents: CommitContent[]) => {
173175
}
174176

175177
const headerLines = allLines.slice(0, headerEndIndex);
176-
const message = allLines.slice(headerEndIndex + 1).join('\n').trim();
178+
const message = allLines
179+
.slice(headerEndIndex + 1)
180+
.join('\n')
181+
.trim();
177182
console.log({ headerLines, message });
178183

179184
const { tree, parents, authorInfo, committerInfo } = getParsedData(headerLines);
@@ -374,22 +379,16 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
374379

375380
// Make sure we don't read past the end of the buffer
376381
if (offset + length > buffer.length) {
377-
throw new Error(`Invalid packet line length ${lengthHex} at offset ${offset}`);
382+
throw new Error(`Invalid packet line length ${lengthHex} at offset ${offset}`);
378383
}
379384

380385
const line = buffer.toString('utf8', offset + 4, offset + length);
381386
lines.push(line);
382387
offset += length; // Move offset to the start of the next line's length prefix
383388
}
384389
return [lines, offset];
385-
}
390+
};
386391

387392
exec.displayName = 'parsePush.exec';
388393

389-
export {
390-
exec,
391-
getCommitData,
392-
getPackMeta,
393-
parsePacketLines,
394-
unpack
395-
};
394+
export { exec, getCommitData, getPackMeta, parsePacketLines, unpack };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const exec = async (req: any, action: Action) => {
2828

2929
const newIdxFiles = [...after].filter((f) => !before.has(f));
3030
action.newIdxFiles = newIdxFiles;
31-
step.log(`.idx generati da questo push: ${newIdxFiles.join(', ') || '(nessuno)'}`);
31+
step.log(`new idx files: ${newIdxFiles}`);
3232

3333
step.setContent(content);
3434
} catch (e: any) {

0 commit comments

Comments
 (0)