From c25018ee4a27dc89676dcda1c1b6f0679a830fd6 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Wed, 8 Nov 2017 02:10:48 +0100 Subject: [PATCH] Check that parent_tag is in an ancestor of the checked out branch `git rev-list A..B` returns the set of commits present in B but not A. Only if A is an ancestor of B will the cardinality of the set be the same as the forward-running offset from A to B. --- TarSCM/scm/git.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/TarSCM/scm/git.py b/TarSCM/scm/git.py index db0338f1..12e22ad4 100644 --- a/TarSCM/scm/git.py +++ b/TarSCM/scm/git.py @@ -182,6 +182,30 @@ def _detect_version_tag_offset(self, parent_tag, versionformat): sys.exit("\033[31m@TAG_OFFSET@ cannot be expanded, " "as no parent tag was discovered.\033[0m") + rcode, tgcommit = self.helpers.run_cmd( + ["git", "rev-parse", parent_tag + "^{}"], + self.clone_dir + ) + if rcode: + sys.exit("\033[31m`git rev-parse " + parent_tag + "` failed: " + + output + "\033[0m") + rcode, head = self.helpers.run_cmd( + ["git", "rev-parse", "HEAD"], + self.clone_dir + ) + if rcode: + sys.exit("\033[31m`git rev-parse HEAD` failed: " + + output + "\033[0m") + rcode, mgbase = self.helpers.run_cmd( + ["git", "merge-base", parent_tag, "HEAD"], + self.clone_dir + ) + tgcommit = tgcommit.strip() + head = head.strip() + mgbase = mgbase.strip() + if mgbase != tgcommit: + sys.exit("\033[31mparent_tag is not an ancestor of HEAD. " + + "Cannot compute a (meaningful) distance.\033[0m") rcode, output = self.helpers.run_cmd( ['git', 'rev-list', '--count', parent_tag + '..HEAD'], self.clone_dir