Skip to content
Merged
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
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;
};
}
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated style change? (I'm surprised we don't lint these files.)

Copy link
Member

Choose a reason for hiding this comment

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

(Although, yeah, that semicolon shouldn't be there. Maybe leave it and ignore my comment. But uh, future enhancement for someone: Add linting or adjust the current linting to catch that stuff automatically.)

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'
]
);
}
}