@@ -45,41 +45,45 @@ export async function getCommits({
45
45
throw new Error ( `Code branch "${ codeBranch } " not found` ) ;
46
46
}
47
47
48
- console . log ( "branches" , branches ) ;
49
-
50
- // Checkout the code branches
51
- await git . checkout ( codeBranch ) ;
52
-
53
- console . log ( "checked out" ) ;
54
-
55
- // Load all logs
56
- const logs = await git . log ( ) ;
57
-
58
- console . log ( "logs" , logs ) ;
48
+ // track the original branch in case of failure
49
+ const originalBranch = branches . current ;
59
50
60
51
// Filter relevant logs
61
52
const commits : CommitLogObject = { } ;
62
53
63
- for ( const commit of logs . all ) {
64
- const matches = commit . message . match (
65
- / ^ (?< stepId > (?< levelId > L \d + ) ( S \d + ) ) (?< stepType > [ Q A ] ) ? /
66
- ) ;
67
-
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 ] . push ( commit . hash ) ;
54
+ try {
55
+ // Checkout the code branches
56
+ await git . checkout ( codeBranch ) ;
57
+
58
+ // Load all logs
59
+ const logs = await git . log ( ) ;
60
+
61
+ for ( const commit of logs . all ) {
62
+ const matches = commit . message . match (
63
+ / ^ (?< stepId > (?< levelId > L \d + ) ( S \d + ) ) (?< stepType > [ Q A ] ) ? /
64
+ ) ;
65
+
66
+ if ( matches && matches . length ) {
67
+ // Use an object of commit arrays to collect all commits
68
+ const position = matches [ 0 ] ;
69
+ if ( ! commits [ position ] ) {
70
+ // does not exist, create the list
71
+ commits [ position ] = [ commit . hash ] ;
72
+ } else {
73
+ // add to the list
74
+ commits [ position ] . push ( commit . hash ) ;
75
+ }
77
76
}
78
77
}
78
+ } catch ( e ) {
79
+ console . error ( "Error with checkout or commit matching" ) ;
80
+ throw new Error ( e . message ) ;
81
+ } finally {
82
+ // revert back to the original branch on failure
83
+ await git . checkout ( originalBranch ) ;
84
+ // cleanup the tmp directory
85
+ await rmdir ( tmpDir , { recursive : true } ) ;
79
86
}
80
87
81
- console . log ( "remove" ) ;
82
- // cleanup the tmp directory
83
- await rmdir ( tmpDir , { recursive : true } ) ;
84
88
return commits ;
85
89
}
0 commit comments