Skip to content

Commit c8e9721

Browse files
committed
return to original branch even in failure
Signed-off-by: shmck <[email protected]>
1 parent 3be863b commit c8e9721

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

Diff for: src/utils/commits.ts

+32-28
Original file line numberDiff line numberDiff line change
@@ -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>[QA])?/
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>[QA])?/
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
}

Diff for: src/utils/schema/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default {
193193
"The solution commits that can be loaded if the user gets stuck. It can also run commands and/or open files",
194194
},
195195
{
196-
required: ["commits"],
196+
required: [],
197197
},
198198
],
199199
},

0 commit comments

Comments
 (0)