Skip to content

Commit cff39a2

Browse files
committed
chore: add constants for parsePush files
1 parent 484bbc0 commit cff39a2

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

src/proxy/processors/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const BRANCH_PREFIX = 'refs/heads/';
2+
export const EMPTY_COMMIT_HASH = '0000000000000000000000000000000000000000';
3+
export const FLUSH_PACKET = '0000';
4+
export const PACK_SIGNATURE = 'PACK';
5+
export const PACKET_SIZE = 4;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Action, Step } from '../../actions';
22
import simpleGit from 'simple-git';
33

4+
import { EMPTY_COMMIT_HASH } from '../constants';
5+
46
const exec = async (req: any, action: Action): Promise<Action> => {
57
const step = new Step('diff');
68

@@ -18,8 +20,8 @@ const exec = async (req: any, action: Action): Promise<Action> => {
1820
return action;
1921
}
2022

21-
if (action.commitFrom === '0000000000000000000000000000000000000000') {
22-
if (action.commitData[0].parent !== '0000000000000000000000000000000000000000') {
23+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
24+
if (action.commitData[0].parent !== EMPTY_COMMIT_HASH) {
2325
commitFrom = `${action.commitData[action.commitData.length - 1].parent}`;
2426
}
2527
} else {

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import fs from 'fs';
44
import path from 'path';
55
import lod from 'lodash';
66
import { CommitContent } from '../types';
7+
import {
8+
BRANCH_PREFIX,
9+
EMPTY_COMMIT_HASH,
10+
PACK_SIGNATURE,
11+
PACKET_SIZE,
12+
} from '../constants';
13+
714
const BitMask = require('bit-mask') as any;
815

916
const dir = path.resolve(__dirname, './.tmp');
@@ -29,8 +36,7 @@ async function exec(req: any, action: Action): Promise<Action> {
2936
return action;
3037
}
3138
const [packetLines, packDataOffset] = parsePacketLines(req.body);
32-
33-
const refUpdates = packetLines.filter((line) => line.includes('refs/heads/'));
39+
const refUpdates = packetLines.filter((line) => line.includes(BRANCH_PREFIX));
3440

3541
if (refUpdates.length !== 1) {
3642
step.log('Invalid number of branch updates.');
@@ -57,7 +63,7 @@ async function exec(req: any, action: Action): Promise<Action> {
5763
const buf = req.body.slice(packDataOffset);
5864

5965
// Verify that data actually starts with PACK signature
60-
if (buf.length < 4 || buf.toString('utf8', 0, 4) !== 'PACK') {
66+
if (buf.length < PACKET_SIZE || buf.toString('utf8', 0, PACKET_SIZE) !== PACK_SIGNATURE) {
6167
step.log(`Expected PACK signature at offset ${packDataOffset}, but found something else.`);
6268
step.setError('Your push has been blocked. Invalid PACK data structure.');
6369
action.addStep(step);
@@ -68,9 +74,14 @@ async function exec(req: any, action: Action): Promise<Action> {
6874
const contents = getContents(contentBuff as any, meta.entries as number);
6975

7076
action.commitData = getCommitData(contents as any);
71-
72-
if (action.commitFrom === '0000000000000000000000000000000000000000') {
73-
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
77+
if (action.commitData.length === 0) {
78+
step.log('No commit data found when parsing push.')
79+
} else {
80+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
81+
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
82+
}
83+
const user = action.commitData[action.commitData.length - 1].committer;
84+
action.user = user;
7485
}
7586

7687
const user = action.commitData[action.commitData.length - 1].committer;
@@ -180,7 +191,7 @@ const getCommitData = (contents: CommitContent[]) => {
180191

181192
const { tree, parents, authorInfo, committerInfo } = getParsedData(headerLines);
182193
// No parent headers -> zero hash
183-
const parent = parents.length > 0 ? parents[0] : '0000000000000000000000000000000000000000';
194+
const parent = parents.length > 0 ? parents[0] : EMPTY_COMMIT_HASH;
184195

185196
// Validation for required attributes
186197
if (!tree || !authorInfo || !committerInfo) {
@@ -210,17 +221,17 @@ const getCommitData = (contents: CommitContent[]) => {
210221
* @return {Array} An array containing the metadata and the remaining buffer.
211222
*/
212223
const getPackMeta = (buffer: Buffer) => {
213-
const sig = buffer.slice(0, 4).toString('utf-8');
214-
const version = buffer.readUIntBE(4, 4);
215-
const entries = buffer.readUIntBE(8, 4);
224+
const sig = buffer.slice(0, PACKET_SIZE).toString('utf-8');
225+
const version = buffer.readUIntBE(PACKET_SIZE, PACKET_SIZE);
226+
const entries = buffer.readUIntBE(PACKET_SIZE * 2, PACKET_SIZE);
216227

217228
const meta = {
218229
sig: sig,
219230
version: version,
220231
entries: entries,
221232
};
222233

223-
return [meta, buffer.slice(12)];
234+
return [meta, buffer.slice(PACKET_SIZE * 3)];
224235
};
225236

226237
/**
@@ -358,8 +369,8 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
358369
const lines: string[] = [];
359370
let offset = 0;
360371

361-
while (offset + 4 <= buffer.length) {
362-
const lengthHex = buffer.toString('utf8', offset, offset + 4);
372+
while (offset + PACKET_SIZE <= buffer.length) {
373+
const lengthHex = buffer.toString('utf8', offset, offset + PACKET_SIZE);
363374
const length = Number(`0x${lengthHex}`);
364375

365376
// Prevent non-hex characters from causing issues
@@ -369,7 +380,7 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
369380

370381
// length of 0 indicates flush packet (0000)
371382
if (length === 0) {
372-
offset += 4; // Include length of the flush packet
383+
offset += PACKET_SIZE; // Include length of the flush packet
373384
break;
374385
}
375386

@@ -378,7 +389,7 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
378389
throw new Error(`Invalid packet line length ${lengthHex} at offset ${offset}`);
379390
}
380391

381-
const line = buffer.toString('utf8', offset + 4, offset + length);
392+
const line = buffer.toString('utf8', offset + PACKET_SIZE, offset + length);
382393
lines.push(line);
383394
offset += length; // Move offset to the start of the next line's length prefix
384395
}

test/testParsePush.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
unpack
1111
} = require('../src/proxy/processors/push-action/parsePush');
1212

13+
import { FLUSH_PACKET, PACK_SIGNATURE } from '../src/proxy/processors/constants';
14+
1315
/**
1416
* Creates a simplified sample PACK buffer for testing.
1517
* @param {number} numEntries - Number of entries in the PACK file.
@@ -23,7 +25,7 @@ function createSamplePackBuffer(
2325
type = 1,
2426
) {
2527
const header = Buffer.alloc(12);
26-
header.write('PACK', 0, 4, 'utf-8'); // Signature
28+
header.write(PACK_SIGNATURE, 0, 4, 'utf-8'); // Signature
2729
header.writeUInt32BE(2, 4); // Version
2830
header.writeUInt32BE(numEntries, 8); // Number of entries
2931

@@ -57,7 +59,7 @@ function createPacketLineBuffer(lines) {
5759
const lengthInHex = (line.length + 4).toString(16).padStart(4, '0');
5860
buffer = Buffer.concat([buffer, Buffer.from(lengthInHex, 'ascii'), Buffer.from(line, 'ascii')]);
5961
});
60-
buffer = Buffer.concat([buffer, Buffer.from('0000', 'ascii')]);
62+
buffer = Buffer.concat([buffer, Buffer.from(FLUSH_PACKET, 'ascii')]);
6163

6264
return buffer;
6365
}
@@ -222,7 +224,7 @@ describe('parsePackFile', () => {
222224
expect(parsedCommit.authorEmail).to.equal('[email protected]');
223225

224226
expect(step.content.meta).to.deep.equal({
225-
sig: 'PACK',
227+
sig: PACK_SIGNATURE,
226228
version: 2,
227229
entries: numEntries,
228230
});
@@ -448,7 +450,7 @@ describe('parsePackFile', () => {
448450
const [meta, contentBuff] = getPackMeta(buffer);
449451

450452
expect(meta).to.deep.equal({
451-
sig: 'PACK',
453+
sig: PACK_SIGNATURE,
452454
version: 2,
453455
entries: 5,
454456
});
@@ -461,7 +463,7 @@ describe('parsePackFile', () => {
461463
const [meta, contentBuff] = getPackMeta(buffer);
462464

463465
expect(meta).to.deep.equal({
464-
sig: 'PACK',
466+
sig: PACK_SIGNATURE,
465467
version: 2,
466468
entries: 1,
467469
});
@@ -682,7 +684,7 @@ describe('parsePackFile', () => {
682684
});
683685

684686
it('should handle a buffer only with a flush packet', () => {
685-
const buffer = Buffer.from('0000');
687+
const buffer = Buffer.from(FLUSH_PACKET);
686688
const [parsedLines, offset] = parsePacketLines(buffer);
687689

688690
expect(parsedLines).to.deep.equal([]);

0 commit comments

Comments
 (0)