11import * as fs from "fs" ;
22import util from "util" ;
33import * as path from "path" ;
4+ import { ListLogSummary } from "simple-git/typings/response" ;
45import gitP , { SimpleGit } from "simple-git/promise" ;
56import { validateCommitOrder } from "./validateCommits" ;
67
@@ -15,6 +16,44 @@ type GetCommitOptions = {
1516
1617export type CommitLogObject = { [ position : string ] : string [ ] } ;
1718
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+
1857export async function getCommits ( {
1958 localDir,
2059 codeBranch,
@@ -49,48 +88,19 @@ export async function getCommits({
4988 // track the original branch in case of failure
5089 const originalBranch = branches . current ;
5190
52- // Filter relevant logs
53- const commits : CommitLogObject = { } ;
54-
5591 try {
5692 // Checkout the code branches
5793 await git . checkout ( codeBranch ) ;
5894
5995 // Load all logs
6096 const logs = await git . log ( ) ;
61- const positions : string [ ] = [ ] ;
6297
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 ) ;
6799
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
93101 validateCommitOrder ( positions ) ;
102+
103+ return commits ;
94104 } catch ( e ) {
95105 console . error ( "Error with checkout or commit matching" ) ;
96106 throw new Error ( e . message ) ;
@@ -100,8 +110,4 @@ export async function getCommits({
100110 // cleanup the tmp directory
101111 await rmdir ( tmpDir , { recursive : true } ) ;
102112 }
103-
104- console . log ( commits ) ;
105-
106- return commits ;
107113}
0 commit comments