@@ -45,41 +45,45 @@ export async function getCommits({
4545 throw new Error ( `Code branch "${ codeBranch } " not found` ) ;
4646 }
4747
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 ;
5950
6051 // Filter relevant logs
6152 const commits : CommitLogObject = { } ;
6253
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+ }
7776 }
7877 }
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 } ) ;
7986 }
8087
81- console . log ( "remove" ) ;
82- // cleanup the tmp directory
83- await rmdir ( tmpDir , { recursive : true } ) ;
8488 return commits ;
8589}
0 commit comments