1
1
import * as fs from "fs" ;
2
2
import util from "util" ;
3
3
import * as path from "path" ;
4
+ import { ListLogSummary } from "simple-git/typings/response" ;
4
5
import gitP , { SimpleGit } from "simple-git/promise" ;
5
6
import { validateCommitOrder } from "./validateCommits" ;
6
7
@@ -15,6 +16,44 @@ type GetCommitOptions = {
15
16
16
17
export type CommitLogObject = { [ position : string ] : string [ ] } ;
17
18
19
+ export function parseCommits ( logs : ListLogSummary < any > ) {
20
+ // Filter relevant logs
21
+ const commits : CommitLogObject = { } ;
22
+ const positions : string [ ] = [ ] ;
23
+
24
+ for ( const commit of logs . all ) {
25
+ const matches = commit . message . match (
26
+ / ^ (?< stepId > (?< levelId > L ? \d + ) ( [ S | \. ] \d + ) ) (?< stepType > [ Q | A | T | S ] ) ? /
27
+ ) ;
28
+
29
+ if ( matches && matches . length ) {
30
+ // Use an object of commit arrays to collect all commits
31
+ const position = matches [ 0 ] ;
32
+ if ( ! commits [ position ] ) {
33
+ // does not exist, create the list
34
+ commits [ position ] = [ commit . hash ] ;
35
+ } else {
36
+ // add to the list
37
+ commits [ position ] . unshift ( commit . hash ) ;
38
+ }
39
+ positions . unshift ( position ) ;
40
+ } else {
41
+ const initMatches = commit . message . match ( / ^ I N I T / ) ;
42
+ if ( initMatches && initMatches . length ) {
43
+ if ( ! commits . INIT ) {
44
+ // does not exist, create the list
45
+ commits . INIT = [ commit . hash ] ;
46
+ } else {
47
+ // add to the list
48
+ commits . INIT . unshift ( commit . hash ) ;
49
+ }
50
+ positions . unshift ( "INIT" ) ;
51
+ }
52
+ }
53
+ }
54
+ return { commits, positions } ;
55
+ }
56
+
18
57
export async function getCommits ( {
19
58
localDir,
20
59
codeBranch,
@@ -49,48 +88,19 @@ export async function getCommits({
49
88
// track the original branch in case of failure
50
89
const originalBranch = branches . current ;
51
90
52
- // Filter relevant logs
53
- const commits : CommitLogObject = { } ;
54
-
55
91
try {
56
92
// Checkout the code branches
57
93
await git . checkout ( codeBranch ) ;
58
94
59
95
// Load all logs
60
96
const logs = await git . log ( ) ;
61
- const positions : string [ ] = [ ] ;
62
97
63
- for ( const commit of logs . all ) {
64
- const matches = commit . message . match (
65
- / ^ (?< stepId > (?< levelId > L ? \d + ) ( [ S | \. ] \d + ) ) (?< stepType > [ Q A ] ) ? /
66
- ) ;
98
+ const { commits, positions } = parseCommits ( logs ) ;
67
99
68
- if ( matches && matches . length ) {
69
- // Use an object of commit arrays to collect all commits
70
- const position = matches [ 0 ] ;
71
- if ( ! commits [ position ] ) {
72
- // does not exist, create the list
73
- commits [ position ] = [ commit . hash ] ;
74
- } else {
75
- // add to the list
76
- commits [ position ] . unshift ( commit . hash ) ;
77
- }
78
- positions . unshift ( position ) ;
79
- } else {
80
- const initMatches = commit . message . match ( / ^ I N I T / ) ;
81
- if ( initMatches && initMatches . length ) {
82
- if ( ! commits . INIT ) {
83
- // does not exist, create the list
84
- commits . INIT = [ commit . hash ] ;
85
- } else {
86
- // add to the list
87
- commits . INIT . unshift ( commit . hash ) ;
88
- }
89
- positions . unshift ( "INIT" ) ;
90
- }
91
- }
92
- }
100
+ // validate order
93
101
validateCommitOrder ( positions ) ;
102
+
103
+ return commits ;
94
104
} catch ( e ) {
95
105
console . error ( "Error with checkout or commit matching" ) ;
96
106
throw new Error ( e . message ) ;
@@ -100,8 +110,4 @@ export async function getCommits({
100
110
// cleanup the tmp directory
101
111
await rmdir ( tmpDir , { recursive : true } ) ;
102
112
}
103
-
104
- console . log ( commits ) ;
105
-
106
- return commits ;
107
113
}
0 commit comments