Changeset View
Changeset View
Standalone View
Standalone View
support/hg/arc-hg.py
from __future__ import absolute_import | from __future__ import absolute_import | ||||
import sys | |||||
is_python_3 = sys.version_info[0] >= 3 | |||||
if is_python_3: | |||||
def arc_items(dict): | |||||
return dict.items() | |||||
else: | |||||
def arc_items(dict): | |||||
return dict.iteritems() | |||||
import os | import os | ||||
import json | import json | ||||
from mercurial import ( | from mercurial import ( | ||||
cmdutil, | cmdutil, | ||||
bookmarks, | bookmarks, | ||||
bundlerepo, | bundlerepo, | ||||
error, | error, | ||||
hg, | hg, | ||||
i18n, | i18n, | ||||
node, | node, | ||||
registrar, | registrar, | ||||
) | ) | ||||
_ = i18n._ | _ = i18n._ | ||||
cmdtable = {} | cmdtable = {} | ||||
command = registrar.command(cmdtable) | command = registrar.command(cmdtable) | ||||
@command( | @command( | ||||
"arc-ls-markers", | b'arc-ls-markers', | ||||
[('', 'output', '', | [(b'', b'output', b'', | ||||
_('file to output refs to'), _('FILE')), | _('file to output refs to'), _('FILE')), | ||||
] + cmdutil.remoteopts, | ] + cmdutil.remoteopts, | ||||
_('[--output FILENAME] [SOURCE]')) | _('[--output FILENAME] [SOURCE]')) | ||||
def lsmarkers(ui, repo, source=None, **opts): | def lsmarkers(ui, repo, source=None, **opts): | ||||
"""list markers | """list markers | ||||
Show the current branch heads and bookmarks in the local working copy, or | Show the current branch heads and bookmarks in the local working copy, or | ||||
a specified path/URL. | a specified path/URL. | ||||
Markers are printed to stdout in JSON. | Markers are printed to stdout in JSON. | ||||
(This is an Arcanist extension to Mercurial.) | (This is an Arcanist extension to Mercurial.) | ||||
Returns 0 if listing the markers succeeds, 1 otherwise. | Returns 0 if listing the markers succeeds, 1 otherwise. | ||||
""" | """ | ||||
if source is None: | if source is None: | ||||
markers = localmarkers(ui, repo) | markers = localmarkers(ui, repo) | ||||
else: | else: | ||||
markers = remotemarkers(ui, repo, source, opts) | markers = remotemarkers(ui, repo, source, opts) | ||||
for m in markers: | |||||
if m['name'] != None: | |||||
m['name'] = m['name'].decode('utf-8') | |||||
if m['node'] != None: | |||||
m['node'] = m['node'].decode('utf-8') | |||||
if m['description'] != None: | |||||
m['description'] = m['description'].decode('utf-8') | |||||
json_opts = { | json_opts = { | ||||
'indent': 2, | 'indent': 2, | ||||
'sort_keys': True, | 'sort_keys': True, | ||||
} | } | ||||
output_file = opts.get('output') | output_file = opts.get('output') | ||||
if output_file: | if output_file: | ||||
if os.path.exists(output_file): | if os.path.exists(output_file): | ||||
raise error.Abort(_('File "%s" already exists.' % output_file)) | raise error.Abort(_('File "%s" already exists.' % output_file)) | ||||
with open(output_file, 'w+') as f: | with open(output_file, 'w+') as f: | ||||
json.dump(markers, f, **json_opts) | json.dump(markers, f, **json_opts) | ||||
else: | else: | ||||
print json.dumps(markers, output_file, **json_opts) | json_data = json.dumps(markers, **json_opts) | ||||
print(json_data) | |||||
return 0 | return 0 | ||||
def localmarkers(ui, repo): | def localmarkers(ui, repo): | ||||
markers = [] | markers = [] | ||||
active_node = repo['.'].node() | active_node = repo[b'.'].node() | ||||
all_heads = set(repo.heads()) | all_heads = set(repo.heads()) | ||||
current_name = repo.dirstate.branch() | current_name = repo.dirstate.branch() | ||||
saw_current = False | saw_current = False | ||||
saw_active = False | saw_active = False | ||||
branch_list = repo.branchmap().iterbranches() | branch_list = repo.branchmap().iterbranches() | ||||
for branch_name, branch_heads, tip_node, is_closed in branch_list: | for branch_name, branch_heads, tip_node, is_closed in branch_list: | ||||
for head_node in branch_heads: | for head_node in branch_heads: | ||||
Show All 39 Lines | markers.append({ | ||||
'isTip': False, | 'isTip': False, | ||||
'isCurrent': True, | 'isCurrent': True, | ||||
'description': None, | 'description': None, | ||||
}) | }) | ||||
bookmarks = repo._bookmarks | bookmarks = repo._bookmarks | ||||
active_bookmark = repo._activebookmark | active_bookmark = repo._activebookmark | ||||
for bookmark_name, bookmark_node in bookmarks.iteritems(): | for bookmark_name, bookmark_node in arc_items(bookmarks): | ||||
is_active = (active_bookmark == bookmark_name) | is_active = (active_bookmark == bookmark_name) | ||||
description = repo[bookmark_node].description() | description = repo[bookmark_node].description() | ||||
if is_active: | if is_active: | ||||
saw_active = True | saw_active = True | ||||
markers.append({ | markers.append({ | ||||
'type': 'bookmark', | 'type': 'bookmark', | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |