@@ -3,7 +3,7 @@ import zlib from 'zlib';
3
3
import fs from 'fs' ;
4
4
import path from 'path' ;
5
5
import lod from 'lodash' ;
6
- import { CommitContent , PersonLine } from '../types' ;
6
+ import { CommitContent , CommitHeader , PersonLine } from '../types' ;
7
7
import {
8
8
BRANCH_PREFIX ,
9
9
EMPTY_COMMIT_HASH ,
@@ -120,15 +120,15 @@ const parsePersonLine = (line: string): PersonLine => {
120
120
/**
121
121
* Parses the header lines of a commit.
122
122
* @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 .
124
124
*/
125
125
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
+ } ;
132
132
133
133
for ( const line of headerLines ) {
134
134
const firstSpaceIndex = line . indexOf ( ' ' ) ;
@@ -142,22 +142,54 @@ const getParsedData = (headerLines: string[]) => {
142
142
143
143
switch ( key ) {
144
144
case 'tree' :
145
+ if ( parsedData . tree ) {
146
+ throw new Error ( 'Multiple tree lines found in commit.' ) ;
147
+ }
145
148
parsedData . tree = value . trim ( ) ;
146
149
break ;
147
150
case 'parent' :
148
151
parsedData . parents . push ( value . trim ( ) ) ;
149
152
break ;
150
153
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 ) ;
152
158
break ;
153
159
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 ) ;
155
164
break ;
156
165
}
157
166
}
167
+ validateParsedData ( parsedData ) ;
158
168
return parsedData ;
159
169
} ;
160
170
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
+
161
193
/**
162
194
* Parses the commit data from the contents of a pack file.
163
195
* @param {CommitContent[] } contents - The contents of the pack file.
@@ -192,26 +224,17 @@ const getCommitData = (contents: CommitContent[]) => {
192
224
const message = allLines . slice ( headerEndIndex + 1 ) . join ( '\n' ) . trim ( ) ;
193
225
console . log ( { headerLines, message } ) ;
194
226
195
- const { tree, parents, authorInfo , committerInfo } = getParsedData ( headerLines ) ;
227
+ const { tree, parents, author , committer } = getParsedData ( headerLines ) ;
196
228
// No parent headers -> zero hash
197
229
const parent = parents . length > 0 ? parents [ 0 ] : EMPTY_COMMIT_HASH ;
198
230
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
-
208
231
return {
209
232
tree,
210
233
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 ,
215
238
message,
216
239
} ;
217
240
} )
0 commit comments