Skip to content

Commit 422ab5b

Browse files
felipecpeff
authored andcommitted
remote-hg: add support for hg-git compat mode
Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 6497a2b commit 422ab5b

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

contrib/remote-helpers/git-remote-hg

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import sys
1616
import os
1717
import json
1818
import shutil
19+
import subprocess
20+
21+
#
22+
# If you want to switch to hg-git compatibility mode:
23+
# git config --global remote-hg.hg-git-compat true
24+
#
25+
# git:
26+
# Sensible defaults for git.
27+
# hg bookmarks are exported as git branches, hg branches are prefixed
28+
# with 'branches/'.
29+
#
30+
# hg:
31+
# Emulate hg-git.
32+
# Only hg bookmarks are exported as git branches.
33+
# Commits are modified to preserve hg information and allow biridectionality.
34+
#
1935

2036
NAME_RE = re.compile('^([^<>]+)')
2137
AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]+)>$')
@@ -226,7 +242,7 @@ def mark_to_rev(mark):
226242
return marks.to_rev(mark)
227243

228244
def export_ref(repo, name, kind, head):
229-
global prefix, marks
245+
global prefix, marks, mode
230246

231247
ename = '%s/%s' % (kind, name)
232248
tip = marks.get_tip(ename)
@@ -261,6 +277,33 @@ def export_ref(repo, name, kind, head):
261277
else:
262278
modified, removed = get_filechanges(repo, c, parents[0])
263279

280+
if mode == 'hg':
281+
extra_msg = ''
282+
283+
if rev_branch != 'default':
284+
extra_msg += 'branch : %s\n' % rev_branch
285+
286+
renames = []
287+
for f in c.files():
288+
if f not in c.manifest():
289+
continue
290+
rename = c.filectx(f).renamed()
291+
if rename:
292+
renames.append((rename[0], f))
293+
294+
for e in renames:
295+
extra_msg += "rename : %s => %s\n" % e
296+
297+
for key, value in extra.iteritems():
298+
if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
299+
continue
300+
else:
301+
extra_msg += "extra : %s : %s\n" % (key, urllib.quote(value))
302+
303+
desc += '\n'
304+
if extra_msg:
305+
desc += '\n--HG--\n' + extra_msg
306+
264307
if len(parents) == 0 and rev:
265308
print 'reset %s/%s' % (prefix, ename)
266309

@@ -354,7 +397,7 @@ def list_head(repo, cur):
354397
g_head = (head, node)
355398

356399
def do_list(parser):
357-
global branches, bmarks
400+
global branches, bmarks, mode
358401

359402
repo = parser.repo
360403
for branch in repo.branchmap():
@@ -368,8 +411,11 @@ def do_list(parser):
368411
cur = repo.dirstate.branch()
369412

370413
list_head(repo, cur)
371-
for branch in branches:
372-
print "? refs/heads/branches/%s" % branch
414+
415+
if mode != 'hg':
416+
for branch in branches:
417+
print "? refs/heads/branches/%s" % branch
418+
373419
for bmark in bmarks:
374420
print "? refs/heads/%s" % bmark
375421

@@ -437,6 +483,7 @@ def get_merge_files(repo, p1, p2, files):
437483

438484
def parse_commit(parser):
439485
global marks, blob_marks, bmarks, parsed_refs
486+
global mode
440487

441488
from_mark = merge_mark = None
442489

@@ -482,7 +529,9 @@ def parse_commit(parser):
482529
return of['ctx']
483530
is_exec = of['mode'] == 'x'
484531
is_link = of['mode'] == 'l'
485-
return context.memfilectx(f, of['data'], is_link, is_exec, None)
532+
rename = of.get('rename', None)
533+
return context.memfilectx(f, of['data'],
534+
is_link, is_exec, rename)
486535

487536
repo = parser.repo
488537

@@ -509,6 +558,21 @@ def parse_commit(parser):
509558
if merge_mark:
510559
get_merge_files(repo, p1, p2, files)
511560

561+
if mode == 'hg':
562+
i = data.find('\n--HG--\n')
563+
if i >= 0:
564+
tmp = data[i + len('\n--HG--\n'):].strip()
565+
for k, v in [e.split(' : ') for e in tmp.split('\n')]:
566+
if k == 'rename':
567+
old, new = v.split(' => ', 1)
568+
files[new]['rename'] = old
569+
elif k == 'branch':
570+
extra[k] = v
571+
elif k == 'extra':
572+
ek, ev = v.split(' : ', 1)
573+
extra[ek] = urllib.unquote(ev)
574+
data = data[:i]
575+
512576
ctx = context.memctx(repo, (p1, p2), data,
513577
files.keys(), getfilectx,
514578
user, (date, tz), extra)
@@ -596,12 +660,25 @@ def do_export(parser):
596660
def main(args):
597661
global prefix, dirname, branches, bmarks
598662
global marks, blob_marks, parsed_refs
599-
global peer
663+
global peer, mode
600664

601665
alias = args[1]
602666
url = args[2]
603667
peer = None
604668

669+
cmd = ['git', 'config', '--get', 'remote-hg.hg-git-compat']
670+
hg_git_compat = False
671+
try:
672+
if subprocess.check_output(cmd) == 'true\n':
673+
hg_git_compat = True
674+
except subprocess.CalledProcessError:
675+
pass
676+
677+
if hg_git_compat:
678+
mode = 'hg'
679+
else:
680+
mode = 'git'
681+
605682
if alias[4:] == url:
606683
is_tmp = True
607684
alias = util.sha1(alias).hexdigest()

0 commit comments

Comments
 (0)