Skip to content

Commit b7ef41f

Browse files
committed
git_helpers: Handle misnamed tags
Fixes #1
1 parent 3cd557e commit b7ef41f

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

conda_build_prepare/git_helpers.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,52 @@ def fetch_tags(**env):
133133
subprocess.check_call(['git', 'fetch', '--tags'], env=env)
134134

135135

136-
def _call_custom_git_cmd(git_repo, cmd_string, check=True, quiet=False):
136+
def _call_custom_git_cmd_cp(git_repo, cmd_string, check=True,
137+
stderr=subprocess.PIPE) -> subprocess.CompletedProcess:
137138
cmd = cmd_string.split()
138139
if cmd[0] != 'git':
139140
cmd.insert(0, 'git')
141+
return subprocess.run(cmd, check=check, cwd=git_repo,
142+
encoding='utf-8', stderr=stderr, stdout=subprocess.PIPE)
140143

141-
stdout = subprocess.run(cmd, check=check, cwd=git_repo, encoding='utf-8',
142-
# None means "don't capture"
143-
stderr=subprocess.DEVNULL if quiet else None,
144-
stdout=subprocess.PIPE).stdout
144+
145+
def _call_custom_git_cmd(git_repo, cmd_string, check=True, quiet=False) -> str:
146+
stdout = _call_custom_git_cmd_cp(
147+
git_repo, cmd_string, check,
148+
# None means "don't capture"
149+
stderr=subprocess.DEVNULL if quiet else None
150+
).stdout
145151
return stdout.strip()
146152

147153

148-
def get_latest_describe_tag(git_repo):
149-
# Find the `git describe` tag having any version-like part
154+
# It's possible to have a git tag named differently than it's internally known as.
155+
# See 'misnamed' in git's 'builtin/describe.c' source file for details.
156+
#
157+
# Let's check whether git printed a warning about such a situation to STDERR and
158+
# use the internal name of the tag if so. Two versions of the warning were used.
159+
def handle_git_describe_stderr(stderr) -> str:
160+
for pattern in [
161+
"tag '.*' is really '(.*)' here", # since git v1.5.5
162+
"tag '(.*)' is externally known as '.*'", # since git v2.27
163+
]:
164+
match = re.search(pattern, stderr)
165+
if match:
166+
return match.group(1)
167+
168+
# Let's print the stderr if it wasn't any of these messages.
169+
print(stderr)
170+
return None
171+
172+
173+
def get_first_reachable_tag(git_repo):
150174
try:
151-
return _call_custom_git_cmd(git_repo, 'describe --tags --abbrev=0', quiet=True)
175+
cp = _call_custom_git_cmd_cp(git_repo, 'describe --tags --abbrev=0')
176+
tag_name = cp.stdout.strip()
177+
if cp.stderr:
178+
misnamed_tag = handle_git_describe_stderr(cp.stderr)
179+
if misnamed_tag:
180+
return misnamed_tag
181+
return tag_name
152182
except subprocess.CalledProcessError:
153183
return None
154184

@@ -232,7 +262,7 @@ def git_rewrite_tags(git_repo):
232262
print(f'\nRewriting tags in "{os.path.abspath(git_repo)}"...\n')
233263

234264
while True:
235-
tag = get_latest_describe_tag(git_repo)
265+
tag = get_first_reachable_tag(git_repo)
236266
if tag is None:
237267
# Add '0.0' tag on initial commit and skip rewriting.
238268
git_add_initial_tag(git_repo)

0 commit comments

Comments
 (0)