@@ -90,6 +90,60 @@ async function exec(req: any, action: Action): Promise<Action> {
90
90
return action ;
91
91
} ;
92
92
93
+ /**
94
+ * Parses the name, email, and timestamp from an author or committer line.
95
+ *
96
+ * Timestamp including timezone offset is required.
97
+ * @param {string } line - The line to parse.
98
+ * @return {Object } An object containing the name, email, and timestamp.
99
+ */
100
+ const parsePersonLine = ( line : string ) : { name : string ; email : string ; timestamp : string } | null => {
101
+ const personRegex = / ^ ( .* ?) < ( .* ?) > ( \d + ) ( [ + - ] \d + ) $ / ;
102
+ const match = line . match ( personRegex ) ;
103
+ if ( ! match ) {
104
+ throw new Error ( `Failed to parse person line: ${ line } . Make sure to include a name, email, timestamp and timezone offset.` ) ;
105
+ }
106
+ return { name : match [ 1 ] . trim ( ) , email : match [ 2 ] , timestamp : match [ 3 ] } ;
107
+ } ;
108
+
109
+ /**
110
+ * Parses the header lines of a commit.
111
+ * @param {string[] } headerLines - The header lines of a commit.
112
+ * @return {Object } An object containing the parsed data.
113
+ */
114
+ const getParsedData = ( headerLines : string [ ] ) => {
115
+ const parsedData : {
116
+ tree ?: string ;
117
+ parents : string [ ] ;
118
+ authorInfo ?: ReturnType < typeof parsePersonLine > ;
119
+ committerInfo ?: ReturnType < typeof parsePersonLine > ;
120
+ } = { parents : [ ] } ;
121
+
122
+ for ( const line of headerLines ) {
123
+ const spaceIndex = line . indexOf ( ' ' ) ;
124
+ if ( spaceIndex === - 1 ) continue ;
125
+
126
+ const key = line . substring ( 0 , spaceIndex ) ;
127
+ const value = line . substring ( spaceIndex + 1 ) ;
128
+
129
+ switch ( key ) {
130
+ case 'tree' :
131
+ parsedData . tree = value . trim ( ) ;
132
+ break ;
133
+ case 'parent' :
134
+ parsedData . parents . push ( value . trim ( ) ) ;
135
+ break ;
136
+ case 'author' :
137
+ parsedData . authorInfo = parsePersonLine ( value ) ;
138
+ break ;
139
+ case 'committer' :
140
+ parsedData . committerInfo = parsePersonLine ( value ) ;
141
+ break ;
142
+ }
143
+ }
144
+ return parsedData ;
145
+ } ;
146
+
93
147
/**
94
148
* Parses the commit data from the contents of a pack file.
95
149
* @param {CommitContent[] } contents - The contents of the pack file.
0 commit comments