Skip to content

Commit 5accc76

Browse files
authored
Merge pull request #2835 from zas/fix_switch_ref
Fix switch ref
2 parents 4fef1b9 + 6bfb62d commit 5accc76

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

picard/git/ops.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ def switch_ref(plugin, ref, discard_changes=False):
245245
origin_remote = repo.get_remote('origin')
246246
repo.fetch_remote(origin_remote, None, callbacks._callbacks)
247247

248+
# If the ref is not found locally, try fetching it specifically
249+
references = repo.get_references()
250+
tag_ref = f'refs/tags/{ref}'
251+
if tag_ref not in references:
252+
try:
253+
# Fetch the specific tag
254+
repo.fetch_remote(origin_remote, f'+refs/tags/{ref}:refs/tags/{ref}', callbacks._callbacks)
255+
except Exception as e:
256+
# If specific fetch fails, continue with what we have
257+
log.debug('Failed to fetch specific tag %s: %s', ref, e)
258+
248259
# Find the ref
249260
try:
250261
references = repo.get_references()
@@ -277,6 +288,19 @@ def switch_ref(plugin, ref, discard_changes=False):
277288
repo.set_head(commit.id)
278289
log.info('Switched plugin %s to tag %s', plugin.plugin_id, ref)
279290
return old_ref, ref, old_commit, commit.id
291+
else:
292+
# Try resolving tag by short name (git can sometimes resolve tags without refs/tags/ prefix)
293+
try:
294+
commit_obj = repo.revparse_single(ref)
295+
# Check if this is actually a tag by seeing if refs/tags/{ref} exists after resolution
296+
if hasattr(commit_obj, 'type') and commit_obj.type in ['tag', 'commit']:
297+
commit = repo.peel_to_commit(commit_obj)
298+
repo.checkout_tree(commit)
299+
repo.set_head(commit.id)
300+
log.info('Switched plugin %s to tag %s', plugin.plugin_id, ref)
301+
return old_ref, ref, old_commit, commit.id
302+
except KeyError:
303+
pass
280304

281305
# Try as commit hash
282306
try:

0 commit comments

Comments
 (0)