Skip to content

Commit

Permalink
feat(git-node): improve backport workflow (#732)
Browse files Browse the repository at this point in the history
Checkout from staging branch and ask to update if needed.

Fixes: #731
  • Loading branch information
rluvaton committed Sep 24, 2023
1 parent d103f28 commit be11c08
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions lib/backport_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class BackportSession extends Session {
cli.stopSpinner(`${file} does not exist in current working tree`,
cli.SPINNER_STATUS.WARN);
continue;
};
}
if (ancestors.length === 0) {
cli.stopSpinner(`Cannot find ancestor commits of ${file}`,
cli.SPINNER_STATUS.INFO);
Expand Down Expand Up @@ -155,12 +155,33 @@ export default class BackportSession extends Session {
cli.log(` - ${commit.sha} ${commit.title}`);
}

if (!this.isLocalBranchExists(this.stagingBranch)) {
const shouldCreateStagingBranch = await cli.prompt(
`It seems like ${this.stagingBranch} is missing locally, ` +
'do you want to create it locally to get ready for backporting?', {
defaultAnswer: true
});

if (shouldCreateStagingBranch) {
this.syncBranchWithUpstream(this.stagingBranch);
}
} else if (!this.isBranchUpToDateWithUpstream(this.stagingBranch)) {
const shouldSyncBranch = await cli.prompt(
`It seems like your ${this.stagingBranch} is behind the ${this.upstream} remote` +
'do you want to sync it?', { defaultAnswer: true });

if (shouldSyncBranch) {
this.syncBranchWithUpstream(this.stagingBranch);
}
}

const newBranch = `backport-${this.prid}-to-${this.target}`;
const shouldCheckout = await cli.prompt(
`Do you want to checkout to a new branch \`${newBranch}\`` +
' to start backporting?', { defaultAnswer: false });

if (shouldCheckout) {
await runAsync('git', ['checkout', '-b', newBranch]);
await runAsync('git', ['checkout', '-b', newBranch, this.stagingBranch]);
}

const shouldAnalyze = await cli.prompt(
Expand Down Expand Up @@ -226,4 +247,56 @@ export default class BackportSession extends Session {
};
});
}

getCurrentBranch() {
return runSync('git',
['rev-parse', '--abbrev-ref', 'HEAD']
).trim();
}

updateUpstreamRefs(branchName) {
runSync('git',
['fetch', this.upstream, branchName]
);
}

getBranchCommit(branch) {
return runSync('git',
['rev-parse', branch]
).trim();
}

isBranchUpToDateWithUpstream(branch) {
this.updateUpstreamRefs(branch);

const localCommit = this.getBranchCommit(branch);
const upstreamCommit = this.getBranchCommit(`${this.upstream}/${branch}`);

return localCommit === upstreamCommit;
};

isLocalBranchExists(branch) {
try {
// will exit with code 1 if branch does not exist
runSync('git',
['rev-parse', '--verify', '--quiet', branch]
);
return true;
} catch (e) {
return false;
}
}

syncBranchWithUpstream(branch) {
const currentBranch = this.getCurrentBranch();

runSync('git',
[
currentBranch !== branch ? 'fetch' : 'pull',
this.upstream,
`${branch}:${branch}`,
'-f'
]
);
}
}

0 comments on commit be11c08

Please sign in to comment.