From ce7cb5b3c7329d3440d72039e0b331db3df68b27 Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Sat, 28 Jan 2017 16:50:53 -0500 Subject: [PATCH] Do not undo tags when git state is dirty When using `tito tag --undo` in a repository with a dirty `git` state, no action should be taken. Signed-off-by: Steve Kuznetsov --- src/tito/common.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tito/common.py b/src/tito/common.py index db0693f..80ceced 100644 --- a/src/tito/common.py +++ b/src/tito/common.py @@ -544,12 +544,30 @@ def undo_tag(tag): commit. Assumes you have taken necessary precautions to ensure this is what you want to do. """ + if not is_git_state_clean(): + error_out("Cannot undo a tag when the current git state is not clean!") + # Using --merge here as it appears to undo the changes in the commit, # but preserve any modified files: output = run_command("git tag -d %s && git reset --merge HEAD^1" % tag) print(output) +def is_git_state_clean(): + """ + Determines if the state of the current git repository is clean or not. + """ + (status, _) = getstatusoutput("git diff-index --quiet HEAD") + if status != 0: + return False + + (status, output) = getstatusoutput("git ls-files --exclude-standard --others") + if len(output) > 0 or status > 0: + return False + + return True + + def get_remote_tag_sha1(tag): """ Get the SHA1 referenced by this git tag in the remote git repo.