Skip to content

UX: Tags/branches awareness #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions mbed/mbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def add(dest):
def remove(dest):
info("Removing reference \"%s\" " % dest)
try:
popen([hg_cmd, 'rm', '-f', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
pquery([hg_cmd, 'rm', '-f', dest] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
except ProcessException:
pass

Expand Down Expand Up @@ -533,13 +533,13 @@ def getrev():
def getbranch():
return pquery([hg_cmd, 'branch']).strip() or ""

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

def remoteid(url, rev=None):
Expand Down Expand Up @@ -646,7 +646,7 @@ def add(dest):
def remove(dest):
info("Removing reference "+dest)
try:
popen([git_cmd, 'rm', '-f', dest] + ([] if very_verbose else ['-q']))
pquery([git_cmd, 'rm', '-f', dest] + ([] if very_verbose else ['-q']))
except ProcessException:
pass

Expand Down Expand Up @@ -745,13 +745,18 @@ def outgoing():
if not branch:
# Default to "master" in detached mode
branch = "master"
# Check if local branch exists. If not, then just carry on
try:
pquery([git_cmd, 'rev-parse', '%s' % branch])
except ProcessException:
return 0
# Check if remote branch exists. If not, then it's a new branch
try:
# Check if remote branch exists
if not pquery([git_cmd, 'rev-parse', '%s/%s' % (remote, branch)]):
return 1
except ProcessException:
return 1
# Check for outgoing commits for the same remote branch
# Check for outgoing commits for the same remote branch only if it exists locally and remotely
return 1 if pquery([git_cmd, 'log', '%s/%s..%s' % (remote, branch, branch)]) else 0

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

# Finds tags. Will match rev if specified
def gettags(rev=None):
def gettags():
tags = []
refs = Git.getrefs()
for ref in refs:
m = re.match(r'^(.+)\s+refs\/tags\/(.+)$', ref)
if m and (not rev or m.group(1).startswith(rev)):
if m:
t = m.group(2)
if re.match(r'^(.+)\^\{\}$', t): # detect tag "pointer"
t = re.sub(r'\^\{\}$', '', t) # remove "pointer" chars, e.g. some-tag^{}
for tag in tags:
if tag[1] == t:
tags.remove(tag)
tags.append(t if rev else [m.group(1), t])
tags.append([m.group(1), t])
return tags

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

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

@classmethod
def revtype(cls, rev, ret_rev=False):
def revtype(self, rev=None, ret_type=True, ret_rev=True, fmt=3):
if rev is None or len(rev) == 0:
return 'latest' + (' revision in the current branch' if ret_rev else '')
output = ('latest' if fmt & 1 else '') + (' revision in the current branch' if fmt & 2 else '')
elif re.match(r'^([a-fA-F0-9]{6,40})$', rev) or re.match(r'^([0-9]+)$', rev):
return 'rev' + (' #'+rev[0:12] if ret_rev else '')
revtags = self.gettags(rev) if rev else []
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 '')
else:
return 'branch' + (' '+rev if ret_rev else '')
output = ('branch/tag' if fmt & 1 else '') + (' "'+rev+'"' if fmt & 2 else '')

return re.sub(r' \(', ', ', re.sub(r'\)', '', output)) if fmt & 4 else output

@classmethod
def isurl(cls, url):
Expand Down Expand Up @@ -1085,6 +1092,13 @@ def getscm(self):
if os.path.isdir(os.path.join(self.path, '.'+name)):
return scm

def gettags(self, rev=None):
tags = self.scm.gettags() if self.scm else []
if rev:
return [tag[1] for tag in tags if tag[0].startswith(rev)]
else:
return tags

# Pass backend SCM commands and parameters if SCM exists
def __wrap_scm(self, method):
def __scm_call(*args, **kwargs):
Expand Down Expand Up @@ -1829,17 +1843,17 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, top=True):
warning("Importing from a local folder \"%s\", not from a URL" % orig_url)

text = "Importing program" if top else "Adding library"
action("%s \"%s\" from \"%s\"%s" % (text, relpath(cwd_root, repo.path), formaturl(repo.url, protocol), ' at '+(repo.revtype(repo.rev, True))))
action("%s \"%s\" from \"%s\"%s" % (text, relpath(cwd_root, repo.path), formaturl(repo.url, protocol), ' at '+(repo.revtype(repo.rev))))
if repo.clone(repo.url, repo.path, rev=repo.rev, depth=depth, protocol=protocol):
with cd(repo.path):
Program(repo.path).set_root()
try:
if repo.rev and repo.getrev() != repo.rev:
repo.checkout(repo.rev, True)
except ProcessException as e:
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev, True))
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(repo.rev))
if depth:
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev)))
if ignore:
warning(err)
else:
Expand Down Expand Up @@ -2033,14 +2047,14 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
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),
repo.revtype(rev, True)))
repo.revtype(rev)))

try:
repo.update(rev, clean, clean_files, repo.is_local)
except ProcessException as e:
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev, True))
err = "Unable to update \"%s\" to %s" % (repo.name, repo.revtype(rev))
if depth:
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev, True)))
err = err + ("\nThe --depth option might prevent fetching the whole revision tree and checking out %s." % (repo.revtype(repo.rev)))
if ignore:
warning(err)
else:
Expand Down Expand Up @@ -2177,10 +2191,8 @@ def sync(recursive=True, keep_refs=False, top=True):
"View the dependency tree of the current program or library."))
def list_(detailed=False, prefix='', p_path=None, ignore=False):
repo = Repo.fromrepo()
revtags = repo.scm.gettags(repo.rev) if repo.rev else []
revstr = ('#' + repo.rev[:12] + (', tags: ' + ', '.join(revtags[0:2]) if len(revtags) else '')) if repo.rev else ''

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'))
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'))

for i, lib in enumerate(sorted(repo.libs, key=lambda l: l.path)):
nprefix = (prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ')) if prefix else ''
Expand All @@ -2201,9 +2213,8 @@ def list_(detailed=False, prefix='', p_path=None, ignore=False):
"Show release tags for the current program or library."))
def releases_(detailed=False, unstable=False, recursive=False, prefix='', p_path=None):
repo = Repo.fromrepo()
tags = repo.scm.gettags()
revtags = repo.scm.gettags(repo.rev) if repo.rev else [] # associated tags with current commit
revstr = ('#' + repo.rev[:12] + (', tags: ' + ', '.join(revtags[0:2]) if len(revtags) else '')) if repo.rev else ''
tags = repo.gettags()
revtags = repo.gettags(repo.rev) if repo.rev and len(tags) else [] # associated tags with current commit
regex_rels = regex_rels_all if unstable else regex_rels_official

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

# Print header
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'))
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'))

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