Skip to content

Commit 463a45d

Browse files
committed
Fixed git re-association with tip of a branch matching the checkout hash, where local and remote branches were treated equally
1 parent a955847 commit 463a45d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

mbed/mbed.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,22 @@ def checkout(rev, clean=False):
600600
if not rev:
601601
return
602602
info("Checkout \"%s\" in %s" % (rev, os.path.basename(os.getcwd())))
603-
popen([git_cmd, 'checkout', rev] + (['-f'] if clean else []) + ([] if very_verbose else ['-q']))
604-
if Git.isdetached(): # try to find associated refs to avoid detached state
605-
refs = Git.getrefs(rev)
606-
for ref in refs: # re-associate with a local or remote branch (rev is the same)
607-
branch = re.sub(r'^(.*?)\/(.*?)$', r'\2', ref)
603+
branch = None
604+
refs = Git.getrefs(rev)
605+
for ref in refs: # re-associate with a local or remote branch (rev is the same)
606+
m = re.match(r'^(.*?)\/(.*?)$', ref)
607+
if m: # matches origin/<branch>
608+
if not os.path.exists(os.path.join('.git', 'refs', 'heads', m.group(2))): # okay only if local branch with that name doesn't exist (git will checkout the origin/<branch> in that case)
609+
branch = m.group(2)
610+
else:
611+
branch = ref # matches local branch
612+
613+
if branch:
608614
info("Revision \"%s\" matches a branch \"%s\" reference. Re-associating with branch" % (rev, branch))
609615
popen([git_cmd, 'checkout', branch] + ([] if very_verbose else ['-q']))
610616
break
617+
else:
618+
popen([git_cmd, 'checkout', rev] + (['-f'] if clean else []) + ([] if very_verbose else ['-q']))
611619

612620
def update(rev=None, clean=False, clean_files=False, is_local=False):
613621
if clean:
@@ -698,9 +706,9 @@ def getrev():
698706
return pquery([git_cmd, 'rev-parse', 'HEAD']).strip()
699707

700708
# Gets current branch or returns empty string if detached
701-
def getbranch():
709+
def getbranch(rev='HEAD'):
702710
try:
703-
branch = pquery([git_cmd, 'rev-parse', '--symbolic-full-name', '--abbrev-ref', 'HEAD']).strip()
711+
branch = pquery([git_cmd, 'rev-parse', '--symbolic-full-name', '--abbrev-ref', rev]).strip()
704712
except ProcessException:
705713
branch = "master"
706714
return branch if branch != "HEAD" else ""

0 commit comments

Comments
 (0)