Skip to content

Commit e53bf97

Browse files
committed
Identify the type of the hash param passed to update command and display whether it's a hash or a branch
1 parent a21b12c commit e53bf97

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

mbed/mbed.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def pull(repo):
274274
def update(repo, hash=None, clean=False):
275275
log("Pulling remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
276276
popen([hg_cmd, 'pull'] + (['-v'] if verbose else ['-q']))
277-
log("Updating \"%s\" to %s" % (repo.name, "rev #"+hash if hash else "latest revision in the current branch"))
277+
log("Updating \"%s\" to %s" % (repo.name, repo.hashtype(hash, True) if hash else "latest revision in the current branch"))
278278
popen([hg_cmd, 'update'] + (['-r', hash] if hash else []) + (['-C'] if clean else []) + (['-v'] if verbose else ['-q']))
279279

280280
def status():
@@ -463,7 +463,7 @@ def update(repo, hash=None, clean=False):
463463
if hash:
464464
log("Fetching remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
465465
popen([git_cmd, 'fetch', '-v', '--all'] + (['-v'] if verbose else ['-q']))
466-
log("Updating \"%s\" to rev #%s" % (repo.name, hash))
466+
log("Updating \"%s\" to %s" % (repo.name, repo.hashtype(hash, True)))
467467
popen([git_cmd, 'checkout'] + [hash] + ([] if verbose else ['-q']))
468468
else:
469469
log("Fetching remote repository \"%s\" to local \"%s\" and updating to latest revision in the current branch" % (repo.url, repo.name))
@@ -629,7 +629,7 @@ def findroot(cls, path=None):
629629
return rpath
630630

631631
@classmethod
632-
def typerepo(cls, path=None):
632+
def pathtype(cls, path=None):
633633
path = os.path.abspath(path or os.getcwd())
634634

635635
depth = 0
@@ -646,6 +646,13 @@ def typerepo(cls, path=None):
646646

647647
return "directory" if depth == 0 else ("program" if depth == 1 else "library")
648648

649+
@classmethod
650+
def hashtype(cls, hash, ret_hash=False):
651+
if re.match(r'^([a-zA-Z0-9]{12,40})$', hash):
652+
return 'rev' + (' #'+hash if ret_hash else '')
653+
else:
654+
return 'branch' + (' '+hash if ret_hash else '')
655+
649656
@property
650657
def lib(self):
651658
return self.path + '.lib'
@@ -791,9 +798,9 @@ def formaturl(url, format="default"):
791798

792799

793800
# Help messages adapt based on current dir
794-
cwd_type = Repo.typerepo()
795-
cwd_dest = "program" if cwd_type == "directory" else "library"
796801
cwd_root = os.getcwd()
802+
cwd_type = Repo.pathtype(cwd_root)
803+
cwd_dest = "program" if cwd_type == "directory" else "library"
797804

798805
# Subparser handling
799806
parser = argparse.ArgumentParser(description="Command-line code management tool for ARM mbed OS - http://www.mbed.com\nversion %s" % ver)
@@ -885,7 +892,7 @@ def import_(url, path=None, depth=None, protocol=None, top=True):
885892

886893
repo = Repo.fromurl(url, path)
887894
if top and cwd_type != "directory":
888-
error("Cannot import program in the specified location \"%s\" because it's already part of a program.\nPlease change your working directory to a different location or use command \"add\" to import the URL as a library." % os.path.abspath(repo.path), 1)
895+
error("Cannot import program in the specified location \"%s\" because it's already part of a program.\nPlease change your working directory to a different location or use command \"mbed add\" to import the URL as a library." % os.path.abspath(repo.path), 1)
889896

890897
if os.path.isdir(repo.path) and len(os.listdir(repo.path)) > 1:
891898
error("Directory \"%s\" is not empty. Please ensure that the destination folder is empty." % repo.path, 1)
@@ -895,7 +902,7 @@ def import_(url, path=None, depth=None, protocol=None, top=True):
895902
sorted_scms = sorted(sorted_scms, key=lambda (m, _): not m)
896903

897904
text = "Importing program" if top else "Adding library"
898-
action("%s \"%s\" from \"%s/\"%s" % (text, relpath(cwd_root, repo.path), repo.url, ' at rev #'+repo.hash if repo.hash else ''))
905+
action("%s \"%s\" from \"%s/\"%s" % (text, relpath(cwd_root, repo.path), repo.url, ' at '+(repo.hashtype(repo.hash, True) if repo.hash else '')))
899906
for _, scm in sorted_scms:
900907
try:
901908
scm.clone(repo.url, repo.path, repo.hash, depth=depth, protocol=protocol)
@@ -1026,7 +1033,10 @@ def update(rev=None, clean=False, force=False, ignore=False, top=True, depth=Non
10261033
error("This %s is in detached HEAD state, and you won't be able to receive updates from the remote repository until you either checkout a branch or create a new one.\nYou can checkout a branch using \"%s checkout <branch_name>\" command before running \"mbed update\"." % (cwd_type, repo.scm.name), 1)
10271034

10281035
# Fetch from remote repo
1029-
action("Updating %s \"%s\" to %s" % (cwd_type if top else cwd_dest, os.path.basename(repo.path) if top else relpath(cwd_root, repo.path), "rev #"+rev if rev else "latest revision in the current branch"))
1036+
action("Updating %s \"%s\" to %s" % (
1037+
cwd_type if top else cwd_dest,
1038+
os.path.basename(repo.path) if top else relpath(cwd_root, repo.path),
1039+
repo.hashtype(rev, True) if rev else "latest revision in the current branch"))
10301040
repo.scm.update(repo, rev, clean)
10311041
repo.rm_untracked()
10321042

0 commit comments

Comments
 (0)