3 # Copyright (c) 2012 Felipe Contreras
7 # Just copy to your ~/bin, or anywhere in your $PATH.
8 # Then you can clone with:
9 # % git clone bzr::/path/to/bzr/repo/or/url
12 # % git clone bzr::$HOME/myrepo
14 # % git clone bzr::lp:myrepo
20 if hasattr(bzrlib, "initialize"):
24 bzrlib.plugin.load_plugins()
26 import bzrlib.generate_ids
27 import bzrlib.transport
35 NAME_RE = re.compile('^([^<>]+)')
36 AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
37 RAW_AUTHOR_RE = re.compile('^(\w+) (.+)? <(.*)> (\d+) ([+-]\d+)')
40 sys.stderr.write('ERROR: %s\n' % (msg % args))
44 sys.stderr.write('WARNING: %s\n' % (msg % args))
47 return '%+03d%02d' % (tz / 3600, tz % 3600 / 60)
51 def __init__(self, path):
60 if not os.path.exists(self.path):
63 tmp = json.load(open(self.path))
64 self.tips = tmp['tips']
65 self.marks = tmp['marks']
66 self.last_mark = tmp['last-mark']
68 for rev, mark in self.marks.iteritems():
69 self.rev_marks[mark] = rev
72 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
75 json.dump(self.dict(), open(self.path, 'w'))
78 return str(self.dict())
80 def from_rev(self, rev):
81 return self.marks[rev]
83 def to_rev(self, mark):
84 return self.rev_marks[mark]
90 def get_mark(self, rev):
92 self.marks[rev] = self.last_mark
95 def is_marked(self, rev):
96 return self.marks.has_key(rev)
98 def new_mark(self, rev, mark):
99 self.marks[rev] = mark
100 self.rev_marks[mark] = rev
101 self.last_mark = mark
103 def get_tip(self, branch):
104 return self.tips.get(branch, None)
106 def set_tip(self, branch, tip):
107 self.tips[branch] = tip
111 def __init__(self, repo):
113 self.line = self.get_line()
116 return sys.stdin.readline().strip()
118 def __getitem__(self, i):
119 return self.line.split()[i]
121 def check(self, word):
122 return self.line.startswith(word)
124 def each_block(self, separator):
125 while self.line != separator:
127 self.line = self.get_line()
130 return self.each_block('')
133 self.line = self.get_line()
134 if self.line == 'done':
138 i = self.line.index(':') + 1
139 return int(self.line[i:])
142 if not self.check('data'):
144 i = self.line.index(' ') + 1
145 size = int(self.line[i:])
146 return sys.stdin.read(size)
148 def get_author(self):
149 m = RAW_AUTHOR_RE.match(self.line)
152 _, name, email, date, tz = m.groups()
153 committer = '%s <%s>' % (name, email)
155 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
156 return (committer, int(date), tz)
158 def rev_to_mark(rev):
160 return marks.from_rev(rev)
162 def mark_to_rev(mark):
164 return marks.to_rev(mark)
166 def fixup_user(user):
168 user = user.replace('"', '')
169 m = AUTHOR_RE.match(user)
172 mail = m.group(2).strip()
174 m = NAME_RE.match(user)
176 name = m.group(1).strip()
178 return '%s <%s>' % (name, mail)
180 def get_filechanges(cur, prev):
184 changes = cur.changes_from(prev)
186 for path, fid, kind in changes.added:
188 for path, fid, kind in changes.removed:
190 for path, fid, kind, mod, _ in changes.modified:
192 for oldpath, newpath, fid, kind, mod, _ in changes.renamed:
193 removed[oldpath] = None
194 if kind == 'directory':
195 lst = cur.list_files(from_dir=newpath, recursive=True)
196 for path, file_class, kind, fid, entry in lst:
197 if kind != 'directory':
198 modified[newpath + '/' + path] = fid
200 modified[newpath] = fid
202 return modified, removed
204 def export_files(tree, files):
205 global marks, filenodes
208 for path, fid in files.iteritems():
209 kind = tree.kind(fid)
211 h = tree.get_file_sha1(fid)
213 if kind == 'symlink':
214 d = tree.get_symlink_target(fid)
218 if tree.is_executable(fid):
223 # is the blog already exported?
226 final.append((mode, mark, path.encode('utf-8')))
229 d = tree.get_file_text(fid)
230 elif kind == 'directory':
233 die("Unhandled kind '%s' for path '%s'" % (kind, path))
235 mark = marks.next_mark()
239 print "mark :%u" % mark
240 print "data %d" % len(d)
243 final.append((mode, mark, path.encode('utf-8')))
247 def export_branch(branch, name):
248 global prefix, dirname
250 ref = '%s/heads/%s' % (prefix, name)
251 tip = marks.get_tip(name)
253 repo = branch.repository
255 revs = branch.iter_merge_sorted_revisions(None, tip, 'exclude', 'forward')
258 revs = [revid for revid, _, _, _ in revs if not marks.is_marked(revid)]
262 rev = repo.get_revision(revid)
264 parents = rev.parent_ids
267 committer = rev.committer.encode('utf-8')
268 committer = "%s %u %s" % (fixup_user(committer), time, gittz(tz))
269 authors = rev.get_apparent_authors()
271 author = authors[0].encode('utf-8')
272 author = "%s %u %s" % (fixup_user(author), time, gittz(tz))
275 msg = rev.message.encode('utf-8')
279 if len(parents) == 0:
280 parent = bzrlib.revision.NULL_REVISION
284 cur_tree = repo.revision_tree(revid)
285 prev = repo.revision_tree(parent)
286 modified, removed = get_filechanges(cur_tree, prev)
288 modified_final = export_files(cur_tree, modified)
290 if len(parents) == 0:
291 print 'reset %s' % ref
293 print "commit %s" % ref
294 print "mark :%d" % (marks.get_mark(revid))
295 print "author %s" % (author)
296 print "committer %s" % (committer)
297 print "data %d" % (len(msg))
300 for i, p in enumerate(parents):
309 print "merge :%s" % m
313 for f in modified_final:
314 print "M %s :%u %s" % f
318 if (count % 100 == 0):
319 print "progress revision %s (%d/%d)" % (revid, count, len(revs))
320 print "#############################################################"
324 revid = branch.last_revision()
326 # make sure the ref is updated
327 print "reset %s" % ref
328 print "from :%u" % rev_to_mark(revid)
331 marks.set_tip(name, revid)
333 def export_tag(repo, name):
336 print "reset refs/tags/%s" % name
337 print "from :%u" % rev_to_mark(tags[name])
340 warn("TODO: fetch tag '%s'" % name)
342 def do_import(parser):
346 path = os.path.join(dirname, 'marks-git')
349 if os.path.exists(path):
350 print "feature import-marks=%s" % path
351 print "feature export-marks=%s" % path
354 while parser.check('import'):
356 if ref.startswith('refs/heads/'):
357 name = ref[len('refs/heads/'):]
358 export_branch(branch, name)
359 if ref.startswith('refs/tags/'):
360 name = ref[len('refs/tags/'):]
361 export_tag(branch, name)
368 def parse_blob(parser):
372 mark = parser.get_mark()
374 data = parser.get_data()
375 blob_marks[mark] = data
380 def __init__(self, repo, revid, parents, files):
385 self.parents = parents
388 def copy_tree(revid):
389 files = files_cache[revid] = {}
390 tree = repo.repository.revision_tree(revid)
393 for path, entry in tree.iter_entries_by_dir():
394 files[path] = entry.file_id
399 if len(parents) == 0:
400 self.base_id = bzrlib.revision.NULL_REVISION
403 self.base_id = parents[0]
404 self.base_files = files_cache.get(self.base_id, None)
405 if not self.base_files:
406 self.base_files = copy_tree(self.base_id)
408 self.files = files_cache[revid] = self.base_files.copy()
410 for path, f in files.iteritems():
411 fid = self.files.get(path, None)
413 fid = bzrlib.generate_ids.gen_file_id(path)
415 self.updates[fid] = f
417 def last_revision(self):
420 def iter_changes(self):
423 def get_parent(dirname, basename):
424 parent_fid = self.base_files.get(dirname, None)
427 parent_fid = self.files.get(dirname, None)
432 fid = bzrlib.generate_ids.gen_file_id(path)
433 d = add_entry(fid, dirname, 'directory')
436 def add_entry(fid, path, kind, mode = None):
437 dirname, basename = os.path.split(path)
438 parent_fid = get_parent(dirname, basename)
443 elif mode == '120000':
454 self.files[path] = change[0]
455 changes.append(change)
458 def update_entry(fid, path, kind, mode = None):
459 dirname, basename = os.path.split(path)
460 parent_fid = get_parent(dirname, basename)
465 elif mode == '120000':
476 self.files[path] = change[0]
477 changes.append(change)
480 def remove_entry(fid, path, kind):
481 dirname, basename = os.path.split(path)
482 parent_fid = get_parent(dirname, basename)
492 changes.append(change)
495 for fid, f in self.updates.iteritems():
499 remove_entry(fid, path, 'file')
502 if path in self.base_files:
503 update_entry(fid, path, 'file', f['mode'])
505 add_entry(fid, path, 'file', f['mode'])
509 def get_file_with_stat(self, file_id, path=None):
510 return (StringIO.StringIO(self.updates[file_id]['data']), None)
512 def get_symlink_target(self, file_id):
513 return self.updates[file_id]['data']
515 def c_style_unescape(string):
516 if string[0] == string[-1] == '"':
517 return string.decode('string-escape')[1:-1]
520 def parse_commit(parser):
521 global marks, blob_marks, bmarks, parsed_refs
529 if ref != 'refs/heads/master':
530 die("bzr doesn't support multiple branches; use 'master'")
532 commit_mark = parser.get_mark()
534 author = parser.get_author()
536 committer = parser.get_author()
538 data = parser.get_data()
540 if parser.check('from'):
541 parents.append(parser.get_mark())
543 while parser.check('merge'):
544 parents.append(parser.get_mark())
550 if parser.check('M'):
551 t, m, mark_ref, path = line.split(' ', 3)
552 mark = int(mark_ref[1:])
553 f = { 'mode' : m, 'data' : blob_marks[mark] }
554 elif parser.check('D'):
555 t, path = line.split(' ')
556 f = { 'deleted' : True }
558 die('Unknown file command: %s' % line)
559 path = c_style_unescape(path).decode('utf-8')
564 committer, date, tz = committer
565 parents = [str(mark_to_rev(p)) for p in parents]
566 revid = bzrlib.generate_ids.gen_revision_id(committer, date)
568 props['branch-nick'] = repo.nick
570 mtree = CustomTree(repo, revid, parents, files)
571 changes = mtree.iter_changes()
575 builder = repo.get_commit_builder(parents, None, date, tz, committer, props, revid)
577 list(builder.record_iter_changes(mtree, mtree.last_revision(), changes))
578 builder.finish_inventory()
579 builder.commit(data.decode('utf-8', 'replace'))
586 parsed_refs[ref] = revid
587 marks.new_mark(revid, commit_mark)
589 def parse_reset(parser):
595 if ref != 'refs/heads/master':
596 die("bzr doesn't support multiple branches; use 'master'")
599 if parser.check('commit'):
602 if not parser.check('from'):
604 from_mark = parser.get_mark()
607 parsed_refs[ref] = mark_to_rev(from_mark)
609 def do_export(parser):
610 global parsed_refs, dirname, peer
614 for line in parser.each_block('done'):
615 if parser.check('blob'):
617 elif parser.check('commit'):
619 elif parser.check('reset'):
621 elif parser.check('tag'):
623 elif parser.check('feature'):
626 die('unhandled export command: %s' % line)
630 for ref, revid in parsed_refs.iteritems():
631 if ref == 'refs/heads/master':
632 repo.generate_revision_history(revid, marks.get_tip('master'))
633 revno, revid = repo.last_revision_info()
635 if hasattr(peer, "import_last_revision_info_and_tags"):
636 peer.import_last_revision_info_and_tags(repo, revno, revid)
638 peer.import_last_revision_info(repo.repository, revno, revid)
640 wt = repo.bzrdir.open_workingtree()
645 def do_capabilities(parser):
650 print "refspec refs/heads/*:%s/heads/*" % prefix
652 path = os.path.join(dirname, 'marks-git')
654 if os.path.exists(path):
655 print "*import-marks %s" % path
656 print "*export-marks %s" % path
662 print "? refs/heads/%s" % 'master'
664 history = parser.repo.revision_history()
665 for tag, revid in parser.repo.tags.get_tag_dict().items():
666 if revid not in history:
668 print "? refs/tags/%s" % tag
670 print "@refs/heads/%s HEAD" % 'master'
673 def get_repo(url, alias):
676 origin = bzrlib.bzrdir.BzrDir.open(url)
677 branch = origin.open_branch()
679 if not isinstance(origin.transport, bzrlib.transport.local.LocalTransport):
680 clone_path = os.path.join(dirname, 'clone')
681 remote_branch = branch
682 if os.path.exists(clone_path):
684 d = bzrlib.bzrdir.BzrDir.open(clone_path)
685 branch = d.open_branch()
686 result = branch.pull(remote_branch, [], None, False)
689 d = origin.sprout(clone_path, None,
690 hardlink=True, create_tree_if_local=False,
691 source_branch=remote_branch)
692 branch = d.open_branch()
693 branch.bind(remote_branch)
702 global marks, prefix, dirname
703 global tags, filenodes
711 prefix = 'refs/bzr/%s' % alias
718 gitdir = os.environ['GIT_DIR']
719 dirname = os.path.join(gitdir, 'bzr', alias)
721 if not os.path.exists(dirname):
724 repo = get_repo(url, alias)
726 marks_path = os.path.join(dirname, 'marks-int')
727 marks = Marks(marks_path)
729 parser = Parser(repo)
731 if parser.check('capabilities'):
732 do_capabilities(parser)
733 elif parser.check('list'):
735 elif parser.check('import'):
737 elif parser.check('export'):
740 die('unhandled command: %s' % line)
745 sys.exit(main(sys.argv))