Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollback Git operations when publish fails and on termination #334

Merged
merged 15 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,9 @@ exports.verifyTagDoesNotExistOnRemote = async tagName => {
exports.commitLogFromRevision = revision => execa.stdout('git', ['log', '--format=%s %h', `${revision}..HEAD`]);

exports.push = () => execa('git', ['push', '--follow-tags']);

exports.getLastCommit = () => execa.stdout('git', ['log', '--max-count=1', '--pretty=%B']);

exports.deleteTag = tagName => execa('git', ['tag', '--delete', tagName]);
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved

exports.removeLastCommit = () => execa('git', ['reset', '--hard', 'HEAD~1']);
8 changes: 4 additions & 4 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ module.exports = async (input = 'patch', options) => {
const rollback = onetime(async () => {
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
console.log('\nPublish failed. Rolling back to the previous state…');

const lastCommit = await execa.stdout('git', ['log', 'HEAD^..HEAD', '--pretty=%B']);
const lastCommit = await git.getLastCommit();
try {
if (lastCommit === util.readPkg().version && lastCommit !== pkg.version) {
await execa('git', ['tag', '--delete', options.version]);
await execa('git', ['reset', '--hard', 'HEAD~1']);
if (lastCommit === util.readPkg().version && lastCommit !== pkg.version) { // Verify that the package's version has been bumped before deleting the last tag and commit.
await git.deleteTag(options.version);
await git.removeLastCommit();
}

console.log('Successfully rolled back the project to its previous state.');
Expand Down