Skip to content

Commit 437ac2e

Browse files
authored
Merge pull request #592 from screamerbg/development
UX: Tags/branches awareness
2 parents dd543b7 + cabd81b commit 437ac2e

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

mbed/mbed.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def add(dest):
425425
def remove(dest):
426426
info("Removing reference \"%s\" " % dest)
427427
try:
428-
popen([hg_cmd, 'rm', '-f', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
428+
pquery([hg_cmd, 'rm', '-f', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
429429
except ProcessException:
430430
pass
431431

@@ -533,13 +533,13 @@ def getrev():
533533
def getbranch():
534534
return pquery([hg_cmd, 'branch']).strip() or ""
535535

536-
def gettags(rev=None):
536+
def gettags():
537537
tags = []
538538
refs = pquery([hg_cmd, 'tags']).strip().splitlines() or []
539539
for ref in refs:
540540
m = re.match(r'^(.+?)\s+(\d+)\:([a-f0-9]+)$', ref)
541-
if m and (not rev or m.group(1).startswith(rev)):
542-
tags.append(m.group(1) if rev else [m.group(3), m.group(1)])
541+
if m:
542+
tags.append([m.group(3), m.group(1)])
543543
return tags
544544

545545
def remoteid(url, rev=None):
@@ -646,7 +646,7 @@ def add(dest):
646646
def remove(dest):
647647
info("Removing reference "+dest)
648648
try:
649-
popen([git_cmd, 'rm', '-f', dest] + ([] if very_verbose else ['-q']))
649+
pquery([git_cmd, 'rm', '-f', dest] + ([] if very_verbose else ['-q']))
650650
except ProcessException:
651651
pass
652652

@@ -745,13 +745,18 @@ def outgoing():
745745
if not branch:
746746
# Default to "master" in detached mode
747747
branch = "master"
748+
# Check if local branch exists. If not, then just carry on
749+
try:
750+
pquery([git_cmd, 'rev-parse', '%s' % branch])
751+
except ProcessException:
752+
return 0
753+
# Check if remote branch exists. If not, then it's a new branch
748754
try:
749-
# Check if remote branch exists
750755
if not pquery([git_cmd, 'rev-parse', '%s/%s' % (remote, branch)]):
751756
return 1
752757
except ProcessException:
753758
return 1
754-
# Check for outgoing commits for the same remote branch
759+
# Check for outgoing commits for the same remote branch only if it exists locally and remotely
755760
return 1 if pquery([git_cmd, 'log', '%s/%s..%s' % (remote, branch, branch)]) else 0
756761

757762
# Checks whether current working tree is detached
@@ -822,19 +827,19 @@ def getbranches(rev=None, ret_rev=False):
822827
return result
823828

824829
# Finds tags. Will match rev if specified
825-
def gettags(rev=None):
830+
def gettags():
826831
tags = []
827832
refs = Git.getrefs()
828833
for ref in refs:
829834
m = re.match(r'^(.+)\s+refs\/tags\/(.+)$', ref)
830-
if m and (not rev or m.group(1).startswith(rev)):
835+
if m:
831836
t = m.group(2)
832837
if re.match(r'^(.+)\^\{\}$', t): # detect tag "pointer"
833838
t = re.sub(r'\^\{\}$', '', t) # remove "pointer" chars, e.g. some-tag^{}
834839
for tag in tags:
835840
if tag[1] == t:
836841
tags.remove(tag)
837-
tags.append(t if rev else [m.group(1), t])
842+
tags.append([m.group(1), t])
838843
return tags
839844

840845
# Finds branches a rev belongs to
@@ -1025,14 +1030,16 @@ def pathtype(cls, path=None):
10251030

10261031
return "directory" if depth == 0 else ("program" if depth == 1 else "library")
10271032

1028-
@classmethod
1029-
def revtype(cls, rev, ret_rev=False):
1033+
def revtype(self, rev=None, ret_type=True, ret_rev=True, fmt=3):
10301034
if rev is None or len(rev) == 0:
1031-
return 'latest' + (' revision in the current branch' if ret_rev else '')
1035+
output = ('latest' if fmt & 1 else '') + (' revision in the current branch' if fmt & 2 else '')
10321036
elif re.match(r'^([a-fA-F0-9]{6,40})$', rev) or re.match(r'^([0-9]+)$', rev):
1033-
return 'rev' + (' #'+rev[0:12] if ret_rev else '')
1037+
revtags = self.gettags(rev) if rev else []
1038+
output = ('rev ' if fmt & 1 else '') + (('#' + rev[:12] + ((' (tag' + ('s' if len(revtags) > 1 else '') + ': ' + ', '.join(revtags[0:2]) + ')') if len(revtags) else '')) if fmt & 2 and rev else '')
10341039
else:
1035-
return 'branch' + (' '+rev if ret_rev else '')
1040+
output = ('branch/tag' if fmt & 1 else '') + (' "'+rev+'"' if fmt & 2 else '')
1041+
1042+
return re.sub(r' \(', ', ', re.sub(r'\)', '', output)) if fmt & 4 else output
10361043

10371044
@classmethod
10381045
def isurl(cls, url):
@@ -1085,6 +1092,13 @@ def getscm(self):
10851092
if os.path.isdir(os.path.join(self.path, '.'+name)):
10861093
return scm
10871094

1095+
def gettags(self, rev=None):
1096+
tags = self.scm.gettags() if self.scm else []
1097+
if rev:
1098+
return [tag[1] for tag in tags if tag[0].startswith(rev)]
1099+
else:
1100+
return tags
1101+
10881102
# Pass backend SCM commands and parameters if SCM exists
10891103
def __wrap_scm(self, method):
10901104
def __scm_call(*args, **kwargs):
@@ -1839,17 +1853,17 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
18391853
warning("Importing from a local folder \"%s\", not from a URL" % orig_url)
18401854

18411855
text = "Importing program" if top else "Adding library"
1842-
action("%s \"%s\" from \"%s\"%s" % (text, relpath(cwd_root, repo.path), formaturl(repo.url, protocol), ' at '+(repo.revtype(repo.rev, True))))
1856+
action("%s \"%s\" from \"%s\"%s" % (text, relpath(cwd_root, repo.path), formaturl(repo.url, protocol), ' at '+(repo.revtype(repo.rev))))
18431857
if repo.clone(repo.url, repo.path, rev=repo.rev, depth=depth, protocol=protocol):
18441858
with cd(repo.path):
18451859
Program(repo.path).set_root()
18461860
try:
18471861
if repo.rev and repo.getrev() != repo.rev:
18481862
repo.checkout(repo.rev, True)
18491863
except ProcessException as e:
1850-
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
1864+
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev))
18511865
if depth:
1852-
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
1866+
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev)))
18531867
if ignore:
18541868
warning(err)
18551869
else:
@@ -2047,14 +2061,14 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
20472061
action("Updating %s \"%s\" to %s" % (
20482062
cwd_type if top else cwd_dest,
20492063
os.path.basename(repo.path) if top else relpath(cwd_root, repo.path),
2050-
repo.revtype(rev, True)))
2064+
repo.revtype(rev)))
20512065

20522066
try:
20532067
repo.update(rev, clean, clean_files, repo.is_local)
20542068
except ProcessException as e:
2055-
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev, True))
2069+
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev))
20562070
if depth:
2057-
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
2071+
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev)))
20582072
if ignore:
20592073
warning(err)
20602074
else:
@@ -2191,10 +2205,8 @@ def sync(recursive=True, keep_refs=False, top=True):
21912205
"View the dependency tree of the current program or library."))
21922206
def list_(detailed=False, prefix='', p_path=None, ignore=False):
21932207
repo = Repo.fromrepo()
2194-
revtags = repo.scm.gettags(repo.rev) if repo.rev else []
2195-
revstr = ('#' + repo.rev[:12] + (', tags: ' + ', '.join(revtags[0:2]) if len(revtags) else '')) if repo.rev else ''
21962208

2197-
print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else revstr) or 'no revision'))
2209+
print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision'))
21982210

21992211
for i, lib in enumerate(sorted(repo.libs, key=lambda l: l.path)):
22002212
nprefix = (prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ')) if prefix else ''
@@ -2216,9 +2228,8 @@ def list_(detailed=False, prefix='', p_path=None, ignore=False):
22162228
"Show release tags for the current program or library."))
22172229
def releases_(detailed=False, unstable=False, recursive=False, prefix='', p_path=None):
22182230
repo = Repo.fromrepo()
2219-
tags = repo.scm.gettags()
2220-
revtags = repo.scm.gettags(repo.rev) if repo.rev else [] # associated tags with current commit
2221-
revstr = ('#' + repo.rev[:12] + (', tags: ' + ', '.join(revtags[0:2]) if len(revtags) else '')) if repo.rev else ''
2231+
tags = repo.gettags()
2232+
revtags = repo.gettags(repo.rev) if repo.rev and len(tags) else [] # associated tags with current commit
22222233
regex_rels = regex_rels_all if unstable else regex_rels_official
22232234

22242235
# Generate list of tags
@@ -2228,7 +2239,7 @@ def releases_(detailed=False, unstable=False, recursive=False, prefix='', p_path
22282239
rels.append(tag[1] + " %s%s" % ('#' + tag[0] if detailed else "", " <- current" if tag[1] in revtags else ""))
22292240

22302241
# Print header
2231-
print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else revstr) or 'no revision'))
2242+
print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision'))
22322243

22332244
# Print list of tags
22342245
rprefix = (prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ')) if recursive and prefix else ''

0 commit comments

Comments
 (0)