Skip to content

Commit 886ec16

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 886ec16

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

mbed/mbed.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737
# Application version
38-
ver = '0.9.9'
38+
ver = '0.9.10'
3939

4040
# Default paths to Mercurial and Git
4141
hg_cmd = 'hg'
@@ -600,15 +600,24 @@ 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 and m.group(2) != "HEAD": # matches origin/<branch> and isn't HEAD ref
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+
elif ref != "HEAD":
611+
branch = ref # matches local branch and isn't HEAD ref
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
611617

618+
if not branch:
619+
popen([git_cmd, 'checkout', rev] + (['-f'] if clean else []) + ([] if very_verbose else ['-q']))
620+
612621
def update(rev=None, clean=False, clean_files=False, is_local=False):
613622
if clean:
614623
Git.discard(clean_files)
@@ -698,9 +707,9 @@ def getrev():
698707
return pquery([git_cmd, 'rev-parse', 'HEAD']).strip()
699708

700709
# Gets current branch or returns empty string if detached
701-
def getbranch():
710+
def getbranch(rev='HEAD'):
702711
try:
703-
branch = pquery([git_cmd, 'rev-parse', '--symbolic-full-name', '--abbrev-ref', 'HEAD']).strip()
712+
branch = pquery([git_cmd, 'rev-parse', '--symbolic-full-name', '--abbrev-ref', rev]).strip()
704713
except ProcessException:
705714
branch = "master"
706715
return branch if branch != "HEAD" else ""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
setup(
2020
name="mbed-cli",
21-
version="0.9.9",
21+
version="0.9.10",
2222
description="ARM mbed command line tool for repositories version control, publishing and updating code from remotely hosted repositories (GitHub, GitLab and mbed.org), and invoking mbed OS own build system and export functions, among other operations",
2323
long_description=LONG_DESC,
2424
url='http://github.com/ARMmbed/mbed-cli',

0 commit comments

Comments
 (0)