Skip to content

Commit c9a94a7

Browse files
committed
refactor: commit header parsing and validation
1 parent 59642df commit c9a94a7

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import zlib from 'zlib';
33
import fs from 'fs';
44
import path from 'path';
55
import lod from 'lodash';
6-
import { CommitContent, PersonLine } from '../types';
6+
import { CommitContent, CommitHeader, PersonLine } from '../types';
77
import {
88
BRANCH_PREFIX,
99
EMPTY_COMMIT_HASH,
@@ -120,15 +120,15 @@ const parsePersonLine = (line: string): PersonLine => {
120120
/**
121121
* Parses the header lines of a commit.
122122
* @param {string[]} headerLines - The header lines of a commit.
123-
* @return {Object} An object containing the parsed data.
123+
* @return {CommitHeader} An object containing the parsed commit header.
124124
*/
125125
const getParsedData = (headerLines: string[]) => {
126-
const parsedData: {
127-
tree?: string;
128-
parents: string[];
129-
authorInfo?: PersonLine;
130-
committerInfo?: PersonLine;
131-
} = { parents: [] };
126+
const parsedData: CommitHeader = {
127+
parents: [],
128+
tree: '',
129+
author: { name: '', email: '', timestamp: '' },
130+
committer: { name: '', email: '', timestamp: '' },
131+
};
132132

133133
for (const line of headerLines) {
134134
const firstSpaceIndex = line.indexOf(' ');
@@ -142,22 +142,54 @@ const getParsedData = (headerLines: string[]) => {
142142

143143
switch (key) {
144144
case 'tree':
145+
if (parsedData.tree) {
146+
throw new Error('Multiple tree lines found in commit.');
147+
}
145148
parsedData.tree = value.trim();
146149
break;
147150
case 'parent':
148151
parsedData.parents.push(value.trim());
149152
break;
150153
case 'author':
151-
parsedData.authorInfo = parsePersonLine(value);
154+
if (parsedData.author) {
155+
throw new Error('Multiple author lines found in commit.');
156+
}
157+
parsedData.author = parsePersonLine(value);
152158
break;
153159
case 'committer':
154-
parsedData.committerInfo = parsePersonLine(value);
160+
if (parsedData.committer) {
161+
throw new Error('Multiple committer lines found in commit.');
162+
}
163+
parsedData.committer = parsePersonLine(value);
155164
break;
156165
}
157166
}
167+
validateParsedData(parsedData);
158168
return parsedData;
159169
};
160170

171+
/**
172+
* Validates the parsed commit header.
173+
* @param {CommitHeader} parsedData - The parsed commit header.
174+
* @return {void}
175+
* @throws {Error} If the commit header is invalid.
176+
*/
177+
const validateParsedData = (parsedData: CommitHeader) => {
178+
const missing = [];
179+
if (!parsedData.tree) {
180+
missing.push('tree');
181+
}
182+
if (!parsedData.author) {
183+
missing.push('author');
184+
}
185+
if (!parsedData.committer) {
186+
missing.push('committer');
187+
}
188+
if (missing.length > 0) {
189+
throw new Error(`Invalid commit data: Missing ${missing.join(', ')}`);
190+
}
191+
}
192+
161193
/**
162194
* Parses the commit data from the contents of a pack file.
163195
* @param {CommitContent[]} contents - The contents of the pack file.
@@ -192,26 +224,17 @@ const getCommitData = (contents: CommitContent[]) => {
192224
const message = allLines.slice(headerEndIndex + 1).join('\n').trim();
193225
console.log({ headerLines, message });
194226

195-
const { tree, parents, authorInfo, committerInfo } = getParsedData(headerLines);
227+
const { tree, parents, author, committer } = getParsedData(headerLines);
196228
// No parent headers -> zero hash
197229
const parent = parents.length > 0 ? parents[0] : EMPTY_COMMIT_HASH;
198230

199-
// Validation for required attributes
200-
if (!tree || !authorInfo || !committerInfo) {
201-
const missing = [];
202-
if (!tree) missing.push('tree');
203-
if (!authorInfo) missing.push('author');
204-
if (!committerInfo) missing.push('committer');
205-
throw new Error(`Invalid commit data: Missing ${missing.join(', ')}`);
206-
}
207-
208231
return {
209232
tree,
210233
parent,
211-
author: authorInfo.name,
212-
committer: committerInfo.name,
213-
authorEmail: authorInfo.email,
214-
commitTimestamp: committerInfo.timestamp,
234+
author: author.name,
235+
committer: committer.name,
236+
authorEmail: author.email,
237+
commitTimestamp: committer.timestamp,
215238
message,
216239
};
217240
})

src/proxy/processors/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ export type PersonLine = {
2424
email: string;
2525
timestamp: string;
2626
}
27+
28+
export type CommitHeader = {
29+
tree: string;
30+
parents: string[];
31+
author: PersonLine;
32+
committer: PersonLine;
33+
}

0 commit comments

Comments
 (0)