Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,19 @@ function getCombinedBranch(branchConfig) {
return branchName;
}

async function handleSwitchToBranchCommand(sg, sortedBranches, combinedBranch) {
async function handleSwitchToBranchCommand(sg, currentBranch, sortedBranches, combinedBranch) {
const switchToBranchIndex = program.args[0];
if (typeof switchToBranchIndex === 'undefined') {
return;
}
let targetBranch;
let currentBranchIndex = sortedBranches.indexOf(currentBranch)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When contributing to another repository, it is important to maintain the existing style. You are free to adopt whatever style you like in your own repos.

In this case, the repo uses semicolons, so all your changes should do the same.

Suggested change
let currentBranchIndex = sortedBranches.indexOf(currentBranch)
let currentBranchIndex = sortedBranches.indexOf(currentBranch);

if (switchToBranchIndex === 'combined') {
targetBranch = combinedBranch;
} else if (switchToBranchIndex.startsWith("+")) {
targetBranch = sortedBranches[currentBranchIndex + parseInt(switchToBranchIndex.slice(1))]
} else if (switchToBranchIndex.startsWith("-")) {
targetBranch = sortedBranches[currentBranchIndex - parseInt(switchToBranchIndex.slice(1))]
Comment on lines +160 to +163

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have not accounted for index out of bounds errors because the provided argument is larger than the number of branches in the chain.

Suggested change
} else if (switchToBranchIndex.startsWith("+")) {
targetBranch = sortedBranches[currentBranchIndex + parseInt(switchToBranchIndex.slice(1))]
} else if (switchToBranchIndex.startsWith("-")) {
targetBranch = sortedBranches[currentBranchIndex - parseInt(switchToBranchIndex.slice(1))]
} else if (switchToBranchIndex.startsWith("+")) {
const nextIndex = Math.min(sortedBranches.length - 1, currentBranchIndex + parseInt(switchToBranchIndex.slice(1)));
targetBranch = sortedBranches[nextIndex];
} else if (switchToBranchIndex.startsWith("-")) {
const nextIndex = Math.max(0, currentBranchIndex - parseInt(switchToBranchIndex.slice(1)));
targetBranch = sortedBranches[nextIndex];

} else {
targetBranch = sortedBranches[switchToBranchIndex];
}
Expand Down Expand Up @@ -238,8 +243,10 @@ async function main() {
console.log(' Switching branches:');
console.log('');
console.log(
' $ `git pr-train <index>` will switch to branch with index <index> (e.g. 0 or 5). ' +
'If <index> is "combined", it will switch to the combined branch.'
' $ `git pr-train <index>` will switch to branch with index <index> (e.g. 0 or 5).\n' +
' $ `git pr-train [+-]<index>` will switch to branch with index bigger or smaller by <index>\n' +
' relative to the current one branch index.\n' +
' If <index> is "combined", it will switch to the combined branch.\n'
);
console.log('');
console.log(' Creating GitHub PRs:');
Expand Down Expand Up @@ -277,7 +284,7 @@ async function main() {
await sg.raw(['branch', combinedTrainBranch, lastBranchBeforeCombined]);
}

await handleSwitchToBranchCommand(sg, sortedTrainBranches, combinedTrainBranch);
await handleSwitchToBranchCommand(sg, currentBranch, sortedTrainBranches, combinedTrainBranch);

console.log(`I've found these partial branches:`);
const branchesToPrint = sortedTrainBranches.map((b, idx) => {
Expand Down