diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -2667,7 +2667,7 @@ def help_(ui, name=None, **opts): ('b', 'branch', None, _('show branch')), ('t', 'tags', None, _('show tags')), ('B', 'bookmarks', None, _('show bookmarks')), - ] + remoteopts, + ] + remoteopts + formatteropts, _('[-nibtB] [-r REV] [SOURCE]'), optionalrepo=True) def identify(ui, repo, source=None, rev=None, @@ -2726,6 +2726,9 @@ def identify(ui, repo, source=None, rev= repo = peer.local() revs, checkout = hg.addbranchrevs(repo, peer, branches, None) + fm = ui.formatter('identify', opts) + fm.startitem() + if not repo: if num or branch or tags: raise error.Abort( @@ -2736,8 +2739,10 @@ def identify(ui, repo, source=None, rev= rev = "tip" remoterev = peer.lookup(rev) + hexrev = hexfunc(remoterev) if default or id: - output = [hexfunc(remoterev)] + output = [hexrev] + fm.data(id=hexrev) def getbms(): bms = [] @@ -2749,13 +2754,17 @@ def identify(ui, repo, source=None, rev= return sorted(bms) + bms = getbms() if bookmarks: - output.extend(getbms()) + output.extend(bms) elif default and not ui.quiet: # multiple bookmarks for a single parent separated by '/' - bm = '/'.join(getbms()) + bm = '/'.join(bms) if bm: output.append(bm) + + fm.data(node=hex(remoterev)) + fm.data(bookmarks=fm.formatlist(bms, name='bookmark')) else: ctx = scmutil.revsingle(repo, rev, None) @@ -2767,19 +2776,32 @@ def identify(ui, repo, source=None, rev= taglist.extend(p.tags()) changed = "" - if default or id or num: - if (any(repo.status()) - or any(ctx.sub(s).dirty() for s in ctx.substate)): - changed = '+' + if (any(repo.status()) + or any(ctx.sub(s).dirty() for s in ctx.substate)): + changed = '+' + fm.data(changed=changed) + + hexoutput = [hexfunc(p.node()) for p in parents] if default or id: - output = ["%s%s" % - ('+'.join([hexfunc(p.node()) for p in parents]), changed)] + output = ["%s%s" % ('+'.join(hexoutput), changed)] + fm.data(id="%s%s" % ('+'.join(hexoutput), changed)) + if num: - output.append("%s%s" % - ('+'.join(["%d" % p.rev() for p in parents]), changed)) + numoutput = ["%d" % p.rev() for p in parents] + output.append("%s%s" % ('+'.join(numoutput), changed)) + + for i, p in enumerate(parents): + fn = fm.nested('p%d' % (i + 1)) + fn.startitem() + fn.data(rev=p.rev()) + fn.data(node=p.hex()) + fn.end() else: + hexoutput = hexfunc(ctx.node()) if default or id: - output = [hexfunc(ctx.node())] + output = [hexoutput] + fm.data(id=hexoutput) + if num: output.append(pycompat.bytestr(ctx.rev())) taglist = ctx.tags() @@ -2808,7 +2830,13 @@ def identify(ui, repo, source=None, rev= if bookmarks: output.extend(ctx.bookmarks()) - ui.write("%s\n" % ' '.join(output)) + fm.data(node=ctx.hex()) + fm.data(branch=ctx.branch()) + fm.data(tags=fm.formatlist(taglist, name='tag', sep=':')) + fm.data(bookmarks=fm.formatlist(ctx.bookmarks(), name='bookmark')) + + fm.plain("%s\n" % ' '.join(output)) + fm.end() @command('import|patch', [('p', 'strip', 1, diff --git a/tests/test-completion.t b/tests/test-completion.t --- a/tests/test-completion.t +++ b/tests/test-completion.t @@ -295,7 +295,7 @@ Show all commands + options grep: print0, all, text, follow, ignore-case, files-with-matches, line-number, rev, user, date, template, include, exclude heads: rev, topo, active, closed, style, template help: extension, command, keyword, system - identify: rev, num, id, branch, tags, bookmarks, ssh, remotecmd, insecure + identify: rev, num, id, branch, tags, bookmarks, ssh, remotecmd, insecure, template import: strip, base, edit, force, no-commit, bypass, partial, exact, prefix, import-branch, message, logfile, date, user, similarity incoming: force, newest-first, bundle, rev, bookmarks, branch, patch, git, limit, no-merges, stat, graph, style, template, ssh, remotecmd, insecure, subrepos locate: rev, print0, fullpath, include, exclude diff --git a/tests/test-hook.t b/tests/test-hook.t --- a/tests/test-hook.t +++ b/tests/test-hook.t @@ -95,7 +95,7 @@ pretxncommit and commit hooks can see bo test generic hooks $ hg id - pre-identify hook: HG_ARGS=id HG_HOOKNAME=pre-identify HG_HOOKTYPE=pre-identify HG_OPTS={'bookmarks': None, 'branch': None, 'id': None, 'insecure': None, 'num': None, 'remotecmd': '', 'rev': '', 'ssh': '', 'tags': None} HG_PATS=[] + pre-identify hook: HG_ARGS=id HG_HOOKNAME=pre-identify HG_HOOKTYPE=pre-identify HG_OPTS={'bookmarks': None, 'branch': None, 'id': None, 'insecure': None, 'num': None, 'remotecmd': '', 'rev': '', 'ssh': '', 'tags': None, 'template': ''} HG_PATS=[] abort: pre-identify hook exited with status 1 [255] $ hg cat b diff --git a/tests/test-identify.t b/tests/test-identify.t --- a/tests/test-identify.t +++ b/tests/test-identify.t @@ -43,12 +43,36 @@ with options cb9a9f314b8b $ hg id -n -t -b -i cb9a9f314b8b 0 default tip + $ hg id -Tjson + [ + { + "bookmarks": [], + "branch": "default", + "changed": "", + "id": "cb9a9f314b8b", + "node": "ffffffffffffffffffffffffffffffffffffffff", + "p1": [{"node": "cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b", "rev": 0}], + "tags": ["tip"] + } + ] with modifications $ echo b > a $ hg id -n -t -b -i cb9a9f314b8b+ 0+ default tip + $ hg id -Tjson + [ + { + "bookmarks": [], + "branch": "default", + "changed": "+", + "id": "cb9a9f314b8b+", + "node": "ffffffffffffffffffffffffffffffffffffffff", + "p1": [{"node": "cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b", "rev": 0}], + "tags": ["tip"] + } + ] other local repo diff --git a/tests/test-merge-default.t b/tests/test-merge-default.t --- a/tests/test-merge-default.t +++ b/tests/test-merge-default.t @@ -49,6 +49,19 @@ Should succeed: $ hg merge 2 0 files updated, 1 files merged, 0 files removed, 0 files unresolved (branch merge, don't forget to commit) + $ hg id -Tjson + [ + { + "bookmarks": [], + "branch": "default", + "changed": "+", + "id": "f25cbe84d8b3+2d95304fed5d+", + "node": "ffffffffffffffffffffffffffffffffffffffff", + "p1": [{"node": "f25cbe84d8b320e298e7703f18a25a3959518c23", "rev": 4}], + "p2": [{"node": "2d95304fed5d89bc9d70b2a0d02f0d567469c3ab", "rev": 2}], + "tags": ["tip"] + } + ] $ hg commit -mm1 Should succeed - 2 heads: @@ -65,6 +78,17 @@ Should succeed - 2 heads: (branch merge, don't forget to commit) $ hg commit -mm2 + $ hg id -r 1 -Tjson + [ + { + "bookmarks": [], + "branch": "default", + "id": "1846eede8b68", + "node": "1846eede8b6886d8cc8a88c96a687b7fe8f3b9d1", + "tags": [] + } + ] + Should fail because at tip: $ hg merge