remote-hg: add support for pushing
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Jeff King <peff@peff.net>
Felipe Contreras committed
Nov 4, 2012 at 03:13 UTC
23b4a11fa47161a5e5f221c58f47400207403bc4
1 file changed
+215
-2
contrib/remote-helpers/git-remote-hg
+215
-2
@@ -9,7 +9,7 @@
9
# Then you can clone with:
10
# git clone hg::/path/to/mercurial/repo/
11
12
-from mercurial import hg, ui, bookmarks
12
+from mercurial import hg, ui, bookmarks, context
13
14
import re
15
import sys
@@ -18,6 +18,7 @@ import json
18
19
NAME_RE = re.compile('^([^<>]+)')
20
AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]+)>$')
21
+RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.+)> (\d+) ([+-]\d+)')
22
23
def die(msg, *args):
24
sys.stderr.write('ERROR: %s\n' % (msg % args))
@@ -32,12 +33,17 @@ def gitmode(flags):
33
def gittz(tz):
34
return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60)
35
36
+def hgmode(mode):
37
+ m = { '0100755': 'x', '0120000': 'l' }
38
+ return m.get(mode, '')
39
+
40
class Marks:
41
42
def __init__(self, path):
43
self.path = path
44
self.tips = {}
45
self.marks = {}
46
+ self.rev_marks = {}
47
self.last_mark = 0
48
49
self.load()
@@ -52,6 +58,9 @@ class Marks:
58
self.marks = tmp['marks']
59
self.last_mark = tmp['last-mark']
60
61
+ for rev, mark in self.marks.iteritems():
62
+ self.rev_marks[mark] = int(rev)
63
+
64
def dict(self):
65
return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
66
@@ -64,11 +73,19 @@ class Marks:
73
def from_rev(self, rev):
74
return self.marks[str(rev)]
75
76
+ def to_rev(self, mark):
77
+ return self.rev_marks[mark]
78
+
79
def get_mark(self, rev):
80
self.last_mark += 1
81
self.marks[str(rev)] = self.last_mark
82
return self.last_mark
83
84
+ def new_mark(self, rev, mark):
85
+ self.marks[str(rev)] = mark
86
+ self.rev_marks[mark] = rev
87
+ self.last_mark = mark
88
+
89
def is_marked(self, rev):
90
return self.marks.has_key(str(rev))
91
@@ -106,6 +123,35 @@ class Parser:
123
if self.line == 'done':
124
self.line = None
125
126
+ def get_mark(self):
127
+ i = self.line.index(':') + 1
128
+ return int(self.line[i:])
129
+
130
+ def get_data(self):
131
+ if not self.check('data'):
132
+ return None
133
+ i = self.line.index(' ') + 1
134
+ size = int(self.line[i:])
135
+ return sys.stdin.read(size)
136
+
137
+ def get_author(self):
138
+ m = RAW_AUTHOR_RE.match(self.line)
139
+ if not m:
140
+ return None
141
+ _, name, email, date, tz = m.groups()
142
+
143
+ if email != 'unknown':
144
+ if name:
145
+ user = '%s <%s>' % (name, email)
146
+ else:
147
+ user = '<%s>' % (email)
148
+ else:
149
+ user = name
150
+
151
+ tz = int(tz)
152
+ tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
153
+ return (user, int(date), -tz)
154
+
155
def export_file(fc):
156
d = fc.data()
157
print "M %s inline %s" % (gitmode(fc.flags()), fc.path())
@@ -174,6 +220,10 @@ def rev_to_mark(rev):
220
global marks
221
return marks.from_rev(rev)
222
223
+def mark_to_rev(mark):
224
+ global marks
225
+ return marks.to_rev(mark)
226
+
227
def export_ref(repo, name, kind, head):
228
global prefix, marks
229
@@ -263,9 +313,17 @@ def do_capabilities(parser):
313
global prefix, dirname
314
315
print "import"
316
+ print "export"
317
print "refspec refs/heads/branches/*:%s/branches/*" % prefix
318
print "refspec refs/heads/*:%s/bookmarks/*" % prefix
319
print "refspec refs/tags/*:%s/tags/*" % prefix
320
+
321
+ path = os.path.join(dirname, 'marks-git')
322
+
323
+ if os.path.exists(path):
324
+ print "*import-marks %s" % path
325
+ print "*export-marks %s" % path
326
+
327
print
328
329
def get_branch_tip(repo, branch):
@@ -352,8 +410,161 @@ def do_import(parser):
410
411
print 'done'
412
413
+def parse_blob(parser):
414
+ global blob_marks
415
+
416
+ parser.next()
417
+ mark = parser.get_mark()
418
+ parser.next()
419
+ data = parser.get_data()
420
+ blob_marks[mark] = data
421
+ parser.next()
422
+ return
423
+
424
+def parse_commit(parser):
425
+ global marks, blob_marks, bmarks, parsed_refs
426
+
427
+ from_mark = merge_mark = None
428
+
429
+ ref = parser[1]
430
+ parser.next()
431
+
432
+ commit_mark = parser.get_mark()
433
+ parser.next()
434
+ author = parser.get_author()
435
+ parser.next()
436
+ committer = parser.get_author()
437
+ parser.next()
438
+ data = parser.get_data()
439
+ parser.next()
440
+ if parser.check('from'):
441
+ from_mark = parser.get_mark()
442
+ parser.next()
443
+ if parser.check('merge'):
444
+ merge_mark = parser.get_mark()
445
+ parser.next()
446
+ if parser.check('merge'):
447
+ die('octopus merges are not supported yet')
448
+
449
+ files = {}
450
+
451
+ for line in parser:
452
+ if parser.check('M'):
453
+ t, m, mark_ref, path = line.split(' ')
454
+ mark = int(mark_ref[1:])
455
+ f = { 'mode' : hgmode(m), 'data' : blob_marks[mark] }
456
+ elif parser.check('D'):
457
+ t, path = line.split(' ')
458
+ f = { 'deleted' : True }
459
+ else:
460
+ die('Unknown file command: %s' % line)
461
+ files[path] = f
462
+
463
+ def getfilectx(repo, memctx, f):
464
+ of = files[f]
465
+ if 'deleted' in of:
466
+ raise IOError
467
+ is_exec = of['mode'] == 'x'
468
+ is_link = of['mode'] == 'l'
469
+ return context.memfilectx(f, of['data'], is_link, is_exec, None)
470
+
471
+ repo = parser.repo
472
+
473
+ user, date, tz = author
474
+ extra = {}
475
+
476
+ if committer != author:
477
+ extra['committer'] = "%s %u %u" % committer
478
+
479
+ if from_mark:
480
+ p1 = repo.changelog.node(mark_to_rev(from_mark))
481
+ else:
482
+ p1 = '\0' * 20
483
+
484
+ if merge_mark:
485
+ p2 = repo.changelog.node(mark_to_rev(merge_mark))
486
+ else:
487
+ p2 = '\0' * 20
488
+
489
+ ctx = context.memctx(repo, (p1, p2), data,
490
+ files.keys(), getfilectx,
491
+ user, (date, tz), extra)
492
+
493
+ node = repo.commitctx(ctx)
494
+
495
+ rev = repo[node].rev()
496
+
497
+ parsed_refs[ref] = node
498
+
499
+ marks.new_mark(rev, commit_mark)
500
+
501
+def parse_reset(parser):
502
+ ref = parser[1]
503
+ parser.next()
504
+ # ugh
505
+ if parser.check('commit'):
506
+ parse_commit(parser)
507
+ return
508
+ if not parser.check('from'):
509
+ return
510
+ from_mark = parser.get_mark()
511
+ parser.next()
512
+
513
+ node = parser.repo.changelog.node(mark_to_rev(from_mark))
514
+ parsed_refs[ref] = node
515
+
516
+def parse_tag(parser):
517
+ name = parser[1]
518
+ parser.next()
519
+ from_mark = parser.get_mark()
520
+ parser.next()
521
+ tagger = parser.get_author()
522
+ parser.next()
523
+ data = parser.get_data()
524
+ parser.next()
525
+
526
+ # nothing to do
527
+
528
+def do_export(parser):
529
+ global parsed_refs, bmarks
530
+
531
+ parser.next()
532
+
533
+ for line in parser.each_block('done'):
534
+ if parser.check('blob'):
535
+ parse_blob(parser)
536
+ elif parser.check('commit'):
537
+ parse_commit(parser)
538
+ elif parser.check('reset'):
539
+ parse_reset(parser)
540
+ elif parser.check('tag'):
541
+ parse_tag(parser)
542
+ elif parser.check('feature'):
543
+ pass
544
+ else:
545
+ die('unhandled export command: %s' % line)
546
+
547
+ for ref, node in parsed_refs.iteritems():
548
+ if ref.startswith('refs/heads/branches'):
549
+ pass
550
+ elif ref.startswith('refs/heads/'):
551
+ bmark = ref[len('refs/heads/'):]
552
+ if bmark in bmarks:
553
+ old = bmarks[bmark].hex()
554
+ else:
555
+ old = ''
556
+ if not bookmarks.pushbookmark(parser.repo, bmark, old, node):
557
+ continue
558
+ elif ref.startswith('refs/tags/'):
559
+ tag = ref[len('refs/tags/'):]
560
+ parser.repo.tag([tag], node, None, True, None, {})
561
+ print "ok %s" % ref
562
+
563
+ print
564
+
565
def main(args):
356
- global prefix, dirname, marks, branches, bmarks
566
+ global prefix, dirname, branches, bmarks
567
+ global marks, blob_marks, parsed_refs
568
569
alias = args[1]
570
url = args[2]
@@ -362,6 +573,8 @@ def main(args):
573
dirname = os.path.join(gitdir, 'hg', alias)
574
branches = {}
575
bmarks = {}
576
+ blob_marks = {}
577
+ parsed_refs = {}
578
579
repo = get_repo(url, alias)
580
prefix = 'refs/hg/%s' % alias