1#!/usr/bin/env python
2#
3# Copyright (c) 2012 Felipe Contreras
4#
5
6#
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
10#
11# For example:
12# % git clone bzr::$HOME/myrepo
13# or
14# % git clone bzr::lp:myrepo
15#
16# If you want to specify which branches you want to track (per repo):
17# % git config remote.origin.bzr-branches 'trunk, devel, test'
18#
19# Where 'origin' is the name of the repository you want to specify the
20# branches.
21#
22
23import sys
24
25import bzrlib
26if hasattr(bzrlib, "initialize"):
27 bzrlib.initialize()
28
29import bzrlib.plugin
30bzrlib.plugin.load_plugins()
31
32import bzrlib.generate_ids
33import bzrlib.transport
34import bzrlib.errors
35import bzrlib.ui
36import bzrlib.urlutils
37import bzrlib.branch
38
39import sys
40import os
41import json
42import re
43import StringIO
44import atexit, shutil, hashlib, urlparse, subprocess
45
46NAME_RE = re.compile('^([^<>]+)')
47AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
48EMAIL_RE = re.compile('^([^<>]+[^ \\\t<>])?\\b(?:[ \\t<>]*?)\\b([^ \\t<>]+@[^ \\t<>]+)')
49RAW_AUTHOR_RE = re.compile('^(\w+) (.+)? <(.*)> (\d+) ([+-]\d+)')
50
51def die(msg, *args):
52 sys.stderr.write('ERROR: %s\n' % (msg % args))
53 sys.exit(1)
54
55def warn(msg, *args):
56 sys.stderr.write('WARNING: %s\n' % (msg % args))
57
58def gittz(tz):
59 return '%+03d%02d' % (tz / 3600, tz % 3600 / 60)
60
61def get_config(config):
62 cmd = ['git', 'config', '--get', config]
63 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
64 output, _ = process.communicate()
65 return output
66
67class Marks:
68
69 def __init__(self, path):
70 self.path = path
71 self.tips = {}
72 self.marks = {}
73 self.rev_marks = {}
74 self.last_mark = 0
75 self.load()
76
77 def load(self):
78 if not os.path.exists(self.path):
79 return
80
81 tmp = json.load(open(self.path))
82 self.tips = tmp['tips']
83 self.marks = tmp['marks']
84 self.last_mark = tmp['last-mark']
85
86 for rev, mark in self.marks.iteritems():
87 self.rev_marks[mark] = rev
88
89 def dict(self):
90 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
91
92 def store(self):
93 json.dump(self.dict(), open(self.path, 'w'))
94
95 def __str__(self):
96 return str(self.dict())
97
98 def from_rev(self, rev):
99 return self.marks[rev]
100
101 def to_rev(self, mark):
102 return str(self.rev_marks[mark])
103
104 def next_mark(self):
105 self.last_mark += 1
106 return self.last_mark
107
108 def get_mark(self, rev):
109 self.last_mark += 1
110 self.marks[rev] = self.last_mark
111 return self.last_mark
112
113 def is_marked(self, rev):
114 return rev in self.marks
115
116 def new_mark(self, rev, mark):
117 self.marks[rev] = mark
118 self.rev_marks[mark] = rev
119 self.last_mark = mark
120
121 def get_tip(self, branch):
122 try:
123 return str(self.tips[branch])
124 except KeyError:
125 return None
126
127 def set_tip(self, branch, tip):
128 self.tips[branch] = tip
129
130class Parser:
131
132 def __init__(self, repo):
133 self.repo = repo
134 self.line = self.get_line()
135
136 def get_line(self):
137 return sys.stdin.readline().strip()
138
139 def __getitem__(self, i):
140 return self.line.split()[i]
141
142 def check(self, word):
143 return self.line.startswith(word)
144
145 def each_block(self, separator):
146 while self.line != separator:
147 yield self.line
148 self.line = self.get_line()
149
150 def __iter__(self):
151 return self.each_block('')
152
153 def next(self):
154 self.line = self.get_line()
155 if self.line == 'done':
156 self.line = None
157
158 def get_mark(self):
159 i = self.line.index(':') + 1
160 return int(self.line[i:])
161
162 def get_data(self):
163 if not self.check('data'):
164 return None
165 i = self.line.index(' ') + 1
166 size = int(self.line[i:])
167 return sys.stdin.read(size)
168
169 def get_author(self):
170 m = RAW_AUTHOR_RE.match(self.line)
171 if not m:
172 return None
173 _, name, email, date, tz = m.groups()
174 name = name.decode('utf-8')
175 committer = '%s <%s>' % (name, email)
176 tz = int(tz)
177 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
178 return (committer, int(date), tz)
179
180def rev_to_mark(rev):
181 return marks.from_rev(rev)
182
183def mark_to_rev(mark):
184 return marks.to_rev(mark)
185
186def fixup_user(user):
187 name = mail = None
188 user = user.replace('"', '')
189 m = AUTHOR_RE.match(user)
190 if m:
191 name = m.group(1)
192 mail = m.group(2).strip()
193 else:
194 m = EMAIL_RE.match(user)
195 if m:
196 name = m.group(1)
197 mail = m.group(2)
198 else:
199 m = NAME_RE.match(user)
200 if m:
201 name = m.group(1).strip()
202
203 if not name:
204 name = 'unknown'
205 if not mail:
206 mail = 'Unknown'
207
208 return '%s <%s>' % (name, mail)
209
210def get_filechanges(cur, prev):
211 modified = {}
212 removed = {}
213
214 changes = cur.changes_from(prev)
215
216 def u(s):
217 return s.encode('utf-8')
218
219 for path, fid, kind in changes.added:
220 modified[u(path)] = fid
221 for path, fid, kind in changes.removed:
222 removed[u(path)] = None
223 for path, fid, kind, mod, _ in changes.modified:
224 modified[u(path)] = fid
225 for oldpath, newpath, fid, kind, mod, _ in changes.renamed:
226 removed[u(oldpath)] = None
227 if kind == 'directory':
228 lst = cur.list_files(from_dir=newpath, recursive=True)
229 for path, file_class, kind, fid, entry in lst:
230 if kind != 'directory':
231 modified[u(newpath + '/' + path)] = fid
232 else:
233 modified[u(newpath)] = fid
234
235 return modified, removed
236
237def export_files(tree, files):
238 final = []
239 for path, fid in files.iteritems():
240 kind = tree.kind(fid)
241
242 h = tree.get_file_sha1(fid)
243
244 if kind == 'symlink':
245 d = tree.get_symlink_target(fid)
246 mode = '120000'
247 elif kind == 'file':
248
249 if tree.is_executable(fid):
250 mode = '100755'
251 else:
252 mode = '100644'
253
254 # is the blob already exported?
255 if h in filenodes:
256 mark = filenodes[h]
257 final.append((mode, mark, path))
258 continue
259
260 d = tree.get_file_text(fid)
261 elif kind == 'directory':
262 continue
263 else:
264 die("Unhandled kind '%s' for path '%s'" % (kind, path))
265
266 mark = marks.next_mark()
267 filenodes[h] = mark
268
269 print "blob"
270 print "mark :%u" % mark
271 print "data %d" % len(d)
272 print d
273
274 final.append((mode, mark, path))
275
276 return final
277
278def export_branch(repo, name):
279 ref = '%s/heads/%s' % (prefix, name)
280 tip = marks.get_tip(name)
281
282 branch = get_remote_branch(name)
283 repo = branch.repository
284
285 branch.lock_read()
286 revs = branch.iter_merge_sorted_revisions(None, tip, 'exclude', 'forward')
287 try:
288 tip_revno = branch.revision_id_to_revno(tip)
289 last_revno, _ = branch.last_revision_info()
290 total = last_revno - tip_revno
291 except bzrlib.errors.NoSuchRevision:
292 tip_revno = 0
293 total = 0
294
295 for revid, _, seq, _ in revs:
296
297 if marks.is_marked(revid):
298 continue
299
300 rev = repo.get_revision(revid)
301 revno = seq[0]
302
303 parents = rev.parent_ids
304 time = rev.timestamp
305 tz = rev.timezone
306 committer = rev.committer.encode('utf-8')
307 committer = "%s %u %s" % (fixup_user(committer), time, gittz(tz))
308 authors = rev.get_apparent_authors()
309 if authors:
310 author = authors[0].encode('utf-8')
311 author = "%s %u %s" % (fixup_user(author), time, gittz(tz))
312 else:
313 author = committer
314 msg = rev.message.encode('utf-8')
315
316 msg += '\n'
317
318 if len(parents) == 0:
319 parent = bzrlib.revision.NULL_REVISION
320 else:
321 parent = parents[0]
322
323 cur_tree = repo.revision_tree(revid)
324 prev = repo.revision_tree(parent)
325 modified, removed = get_filechanges(cur_tree, prev)
326
327 modified_final = export_files(cur_tree, modified)
328
329 if len(parents) == 0:
330 print 'reset %s' % ref
331
332 print "commit %s" % ref
333 print "mark :%d" % (marks.get_mark(revid))
334 print "author %s" % (author)
335 print "committer %s" % (committer)
336 print "data %d" % (len(msg))
337 print msg
338
339 for i, p in enumerate(parents):
340 try:
341 m = rev_to_mark(p)
342 except KeyError:
343 # ghost?
344 continue
345 if i == 0:
346 print "from :%s" % m
347 else:
348 print "merge :%s" % m
349
350 for f in removed:
351 print "D %s" % (f,)
352 for f in modified_final:
353 print "M %s :%u %s" % f
354 print
355
356 if len(seq) > 1:
357 # let's skip branch revisions from the progress report
358 continue
359
360 progress = (revno - tip_revno)
361 if (progress % 100 == 0):
362 if total:
363 print "progress revision %d '%s' (%d/%d)" % (revno, name, progress, total)
364 else:
365 print "progress revision %d '%s' (%d)" % (revno, name, progress)
366
367 branch.unlock()
368
369 revid = branch.last_revision()
370
371 # make sure the ref is updated
372 print "reset %s" % ref
373 print "from :%u" % rev_to_mark(revid)
374 print
375
376 marks.set_tip(name, revid)
377
378def export_tag(repo, name):
379 ref = '%s/tags/%s' % (prefix, name)
380 print "reset %s" % ref
381 print "from :%u" % rev_to_mark(tags[name])
382 print
383
384def do_import(parser):
385 repo = parser.repo
386 path = os.path.join(dirname, 'marks-git')
387
388 print "feature done"
389 if os.path.exists(path):
390 print "feature import-marks=%s" % path
391 print "feature export-marks=%s" % path
392 print "feature force"
393 sys.stdout.flush()
394
395 while parser.check('import'):
396 ref = parser[1]
397 if ref.startswith('refs/heads/'):
398 name = ref[len('refs/heads/'):]
399 export_branch(repo, name)
400 if ref.startswith('refs/tags/'):
401 name = ref[len('refs/tags/'):]
402 export_tag(repo, name)
403 parser.next()
404
405 print 'done'
406
407 sys.stdout.flush()
408
409def parse_blob(parser):
410 parser.next()
411 mark = parser.get_mark()
412 parser.next()
413 data = parser.get_data()
414 blob_marks[mark] = data
415 parser.next()
416
417class CustomTree():
418
419 def __init__(self, branch, revid, parents, files):
420 self.updates = {}
421 self.branch = branch
422
423 def copy_tree(revid):
424 files = files_cache[revid] = {}
425 branch.lock_read()
426 tree = branch.repository.revision_tree(revid)
427 try:
428 for path, entry in tree.iter_entries_by_dir():
429 files[path] = [entry.file_id, None]
430 finally:
431 branch.unlock()
432 return files
433
434 if len(parents) == 0:
435 self.base_id = bzrlib.revision.NULL_REVISION
436 self.base_files = {}
437 else:
438 self.base_id = parents[0]
439 self.base_files = files_cache.get(self.base_id, None)
440 if not self.base_files:
441 self.base_files = copy_tree(self.base_id)
442
443 self.files = files_cache[revid] = self.base_files.copy()
444 self.rev_files = {}
445
446 for path, data in self.files.iteritems():
447 fid, mark = data
448 self.rev_files[fid] = [path, mark]
449
450 for path, f in files.iteritems():
451 fid, mark = self.files.get(path, [None, None])
452 if not fid:
453 fid = bzrlib.generate_ids.gen_file_id(path)
454 f['path'] = path
455 self.rev_files[fid] = [path, mark]
456 self.updates[fid] = f
457
458 def last_revision(self):
459 return self.base_id
460
461 def iter_changes(self):
462 changes = []
463
464 def get_parent(dirname, basename):
465 parent_fid, mark = self.base_files.get(dirname, [None, None])
466 if parent_fid:
467 return parent_fid
468 parent_fid, mark = self.files.get(dirname, [None, None])
469 if parent_fid:
470 return parent_fid
471 if basename == '':
472 return None
473 fid = bzrlib.generate_ids.gen_file_id(path)
474 add_entry(fid, dirname, 'directory')
475 return fid
476
477 def add_entry(fid, path, kind, mode=None):
478 dirname, basename = os.path.split(path)
479 parent_fid = get_parent(dirname, basename)
480
481 executable = False
482 if mode == '100755':
483 executable = True
484 elif mode == '120000':
485 kind = 'symlink'
486
487 change = (fid,
488 (None, path),
489 True,
490 (False, True),
491 (None, parent_fid),
492 (None, basename),
493 (None, kind),
494 (None, executable))
495 self.files[path] = [change[0], None]
496 changes.append(change)
497
498 def update_entry(fid, path, kind, mode=None):
499 dirname, basename = os.path.split(path)
500 parent_fid = get_parent(dirname, basename)
501
502 executable = False
503 if mode == '100755':
504 executable = True
505 elif mode == '120000':
506 kind = 'symlink'
507
508 change = (fid,
509 (path, path),
510 True,
511 (True, True),
512 (None, parent_fid),
513 (None, basename),
514 (None, kind),
515 (None, executable))
516 self.files[path] = [change[0], None]
517 changes.append(change)
518
519 def remove_entry(fid, path, kind):
520 dirname, basename = os.path.split(path)
521 parent_fid = get_parent(dirname, basename)
522 change = (fid,
523 (path, None),
524 True,
525 (True, False),
526 (parent_fid, None),
527 (None, None),
528 (None, None),
529 (None, None))
530 del self.files[path]
531 changes.append(change)
532
533 for fid, f in self.updates.iteritems():
534 path = f['path']
535
536 if 'deleted' in f:
537 remove_entry(fid, path, 'file')
538 continue
539
540 if path in self.base_files:
541 update_entry(fid, path, 'file', f['mode'])
542 else:
543 add_entry(fid, path, 'file', f['mode'])
544
545 self.files[path][1] = f['mark']
546 self.rev_files[fid][1] = f['mark']
547
548 return changes
549
550 def get_content(self, file_id):
551 path, mark = self.rev_files[file_id]
552 if mark:
553 return blob_marks[mark]
554
555 # last resort
556 tree = self.branch.repository.revision_tree(self.base_id)
557 return tree.get_file_text(file_id)
558
559 def get_file_with_stat(self, file_id, path=None):
560 content = self.get_content(file_id)
561 return (StringIO.StringIO(content), None)
562
563 def get_symlink_target(self, file_id):
564 return self.get_content(file_id)
565
566 def id2path(self, file_id):
567 path, mark = self.rev_files[file_id]
568 return path
569
570def c_style_unescape(string):
571 if string[0] == string[-1] == '"':
572 return string.decode('string-escape')[1:-1]
573 return string
574
575def parse_commit(parser):
576 parents = []
577
578 ref = parser[1]
579 parser.next()
580
581 if ref.startswith('refs/heads/'):
582 name = ref[len('refs/heads/'):]
583 branch = get_remote_branch(name)
584 else:
585 die('unknown ref')
586
587 commit_mark = parser.get_mark()
588 parser.next()
589 author = parser.get_author()
590 parser.next()
591 committer = parser.get_author()
592 parser.next()
593 data = parser.get_data()
594 parser.next()
595 if parser.check('from'):
596 parents.append(parser.get_mark())
597 parser.next()
598 while parser.check('merge'):
599 parents.append(parser.get_mark())
600 parser.next()
601
602 # fast-export adds an extra newline
603 if data[-1] == '\n':
604 data = data[:-1]
605
606 files = {}
607
608 for line in parser:
609 if parser.check('M'):
610 t, m, mark_ref, path = line.split(' ', 3)
611 mark = int(mark_ref[1:])
612 f = { 'mode' : m, 'mark' : mark }
613 elif parser.check('D'):
614 t, path = line.split(' ', 1)
615 f = { 'deleted' : True }
616 else:
617 die('Unknown file command: %s' % line)
618 path = c_style_unescape(path).decode('utf-8')
619 files[path] = f
620
621 committer, date, tz = committer
622 parents = [mark_to_rev(p) for p in parents]
623 revid = bzrlib.generate_ids.gen_revision_id(committer, date)
624 props = {}
625 props['branch-nick'] = branch.nick
626
627 mtree = CustomTree(branch, revid, parents, files)
628 changes = mtree.iter_changes()
629
630 branch.lock_write()
631 try:
632 builder = branch.get_commit_builder(parents, None, date, tz, committer, props, revid)
633 try:
634 list(builder.record_iter_changes(mtree, mtree.last_revision(), changes))
635 builder.finish_inventory()
636 builder.commit(data.decode('utf-8', 'replace'))
637 except Exception, e:
638 builder.abort()
639 raise
640 finally:
641 branch.unlock()
642
643 parsed_refs[ref] = revid
644 marks.new_mark(revid, commit_mark)
645
646def parse_reset(parser):
647 ref = parser[1]
648 parser.next()
649
650 # ugh
651 if parser.check('commit'):
652 parse_commit(parser)
653 return
654 if not parser.check('from'):
655 return
656 from_mark = parser.get_mark()
657 parser.next()
658
659 parsed_refs[ref] = mark_to_rev(from_mark)
660
661def do_export(parser):
662 parser.next()
663
664 for line in parser.each_block('done'):
665 if parser.check('blob'):
666 parse_blob(parser)
667 elif parser.check('commit'):
668 parse_commit(parser)
669 elif parser.check('reset'):
670 parse_reset(parser)
671 elif parser.check('tag'):
672 pass
673 elif parser.check('feature'):
674 pass
675 else:
676 die('unhandled export command: %s' % line)
677
678 for ref, revid in parsed_refs.iteritems():
679 if ref.startswith('refs/heads/'):
680 name = ref[len('refs/heads/'):]
681 branch = get_remote_branch(name)
682 branch.generate_revision_history(revid, marks.get_tip(name))
683
684 if name in peers:
685 peer = bzrlib.branch.Branch.open(peers[name],
686 possible_transports=transports)
687 try:
688 peer.bzrdir.push_branch(branch, revision_id=revid,
689 overwrite=force)
690 except bzrlib.errors.DivergedBranches:
691 print "error %s non-fast forward" % ref
692 continue
693
694 try:
695 wt = branch.bzrdir.open_workingtree()
696 wt.update()
697 except bzrlib.errors.NoWorkingTree:
698 pass
699 elif ref.startswith('refs/tags/'):
700 # TODO: implement tag push
701 print "error %s pushing tags not supported" % ref
702 continue
703 else:
704 # transport-helper/fast-export bugs
705 continue
706
707 print "ok %s" % ref
708
709 print
710
711def do_capabilities(parser):
712 print "import"
713 print "export"
714 print "refspec refs/heads/*:%s/heads/*" % prefix
715 print "refspec refs/tags/*:%s/tags/*" % prefix
716
717 path = os.path.join(dirname, 'marks-git')
718
719 if os.path.exists(path):
720 print "*import-marks %s" % path
721 print "*export-marks %s" % path
722
723 print "option"
724 print
725
726class InvalidOptionValue(Exception):
727 pass
728
729def get_bool_option(val):
730 if val == 'true':
731 return True
732 elif val == 'false':
733 return False
734 else:
735 raise InvalidOptionValue()
736
737def do_option(parser):
738 global force
739 opt, val = parser[1:3]
740 try:
741 if opt == 'force':
742 force = get_bool_option(val)
743 print 'ok'
744 else:
745 print 'unsupported'
746 except InvalidOptionValue:
747 print "error '%s' is not a valid value for option '%s'" % (val, opt)
748
749def ref_is_valid(name):
750 return not True in [c in name for c in '~^: \\']
751
752def do_list(parser):
753 master_branch = None
754
755 for name in branches:
756 if not master_branch:
757 master_branch = name
758 print "? refs/heads/%s" % name
759
760 branch = get_remote_branch(master_branch)
761 branch.lock_read()
762 for tag, revid in branch.tags.get_tag_dict().items():
763 try:
764 branch.revision_id_to_dotted_revno(revid)
765 except bzrlib.errors.NoSuchRevision:
766 continue
767 if not ref_is_valid(tag):
768 continue
769 print "? refs/tags/%s" % tag
770 tags[tag] = revid
771 branch.unlock()
772
773 print "@refs/heads/%s HEAD" % master_branch
774 print
775
776def clone(path, remote_branch):
777 try:
778 bdir = bzrlib.bzrdir.BzrDir.create(path, possible_transports=transports)
779 except bzrlib.errors.AlreadyControlDirError:
780 bdir = bzrlib.bzrdir.BzrDir.open(path, possible_transports=transports)
781 repo = bdir.find_repository()
782 repo.fetch(remote_branch.repository)
783 return remote_branch.sprout(bdir, repository=repo)
784
785def get_remote_branch(name):
786 remote_branch = bzrlib.branch.Branch.open(branches[name],
787 possible_transports=transports)
788 if isinstance(remote_branch.user_transport, bzrlib.transport.local.LocalTransport):
789 return remote_branch
790
791 branch_path = os.path.join(dirname, 'clone', name)
792
793 try:
794 branch = bzrlib.branch.Branch.open(branch_path,
795 possible_transports=transports)
796 except bzrlib.errors.NotBranchError:
797 # clone
798 branch = clone(branch_path, remote_branch)
799 else:
800 # pull
801 try:
802 branch.pull(remote_branch, overwrite=True)
803 except bzrlib.errors.DivergedBranches:
804 # use remote branch for now
805 return remote_branch
806
807 return branch
808
809def find_branches(repo):
810 transport = repo.bzrdir.root_transport
811
812 for fn in transport.iter_files_recursive():
813 if not fn.endswith('.bzr/branch-format'):
814 continue
815
816 name = subdir = fn[:-len('/.bzr/branch-format')]
817 name = name if name != '' else 'master'
818 name = name.replace('/', '+')
819
820 try:
821 cur = transport.clone(subdir)
822 branch = bzrlib.branch.Branch.open_from_transport(cur)
823 except bzrlib.errors.NotBranchError:
824 continue
825 else:
826 yield name, branch.base
827
828def get_repo(url, alias):
829 normal_url = bzrlib.urlutils.normalize_url(url)
830 origin = bzrlib.bzrdir.BzrDir.open(url, possible_transports=transports)
831 is_local = isinstance(origin.transport, bzrlib.transport.local.LocalTransport)
832
833 shared_path = os.path.join(gitdir, 'bzr')
834 try:
835 shared_dir = bzrlib.bzrdir.BzrDir.open(shared_path,
836 possible_transports=transports)
837 except bzrlib.errors.NotBranchError:
838 shared_dir = bzrlib.bzrdir.BzrDir.create(shared_path,
839 possible_transports=transports)
840 try:
841 shared_repo = shared_dir.open_repository()
842 except bzrlib.errors.NoRepositoryPresent:
843 shared_repo = shared_dir.create_repository(shared=True)
844
845 if not is_local:
846 clone_path = os.path.join(dirname, 'clone')
847 if not os.path.exists(clone_path):
848 os.mkdir(clone_path)
849 else:
850 # check and remove old organization
851 try:
852 bdir = bzrlib.bzrdir.BzrDir.open(clone_path,
853 possible_transports=transports)
854 bdir.destroy_repository()
855 except bzrlib.errors.NotBranchError:
856 pass
857 except bzrlib.errors.NoRepositoryPresent:
858 pass
859
860 wanted = get_config('remote.%s.bzr-branches' % alias).rstrip().split(', ')
861 # stupid python
862 wanted = [e for e in wanted if e]
863 if not wanted:
864 wanted = get_config('remote-bzr.branches').rstrip().split(', ')
865 # stupid python
866 wanted = [e for e in wanted if e]
867
868 if not wanted:
869 try:
870 repo = origin.open_repository()
871 if not repo.user_transport.listable():
872 # this repository is not usable for us
873 raise bzrlib.errors.NoRepositoryPresent(repo.bzrdir)
874 except bzrlib.errors.NoRepositoryPresent:
875 wanted = ['master']
876
877 if wanted:
878 def list_wanted(url, wanted):
879 for name in wanted:
880 subdir = name if name != 'master' else ''
881 yield name, bzrlib.urlutils.join(url, subdir)
882
883 branch_list = list_wanted(url, wanted)
884 else:
885 branch_list = find_branches(repo)
886
887 for name, url in branch_list:
888 if not is_local:
889 peers[name] = url
890 branches[name] = url
891
892 return origin
893
894def fix_path(alias, orig_url):
895 url = urlparse.urlparse(orig_url, 'file')
896 if url.scheme != 'file' or os.path.isabs(url.path):
897 return
898 abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)
899 cmd = ['git', 'config', 'remote.%s.url' % alias, "bzr::%s" % abs_url]
900 subprocess.call(cmd)
901
902def main(args):
903 global marks, prefix, gitdir, dirname
904 global tags, filenodes
905 global blob_marks
906 global parsed_refs
907 global files_cache
908 global is_tmp
909 global branches, peers
910 global transports
911 global force
912
913 alias = args[1]
914 url = args[2]
915
916 tags = {}
917 filenodes = {}
918 blob_marks = {}
919 parsed_refs = {}
920 files_cache = {}
921 marks = None
922 branches = {}
923 peers = {}
924 transports = []
925 force = False
926
927 if alias[5:] == url:
928 is_tmp = True
929 alias = hashlib.sha1(alias).hexdigest()
930 else:
931 is_tmp = False
932
933 prefix = 'refs/bzr/%s' % alias
934 gitdir = os.environ['GIT_DIR']
935 dirname = os.path.join(gitdir, 'bzr', alias)
936
937 if not is_tmp:
938 fix_path(alias, url)
939
940 if not os.path.exists(dirname):
941 os.makedirs(dirname)
942
943 if hasattr(bzrlib.ui.ui_factory, 'be_quiet'):
944 bzrlib.ui.ui_factory.be_quiet(True)
945
946 repo = get_repo(url, alias)
947
948 marks_path = os.path.join(dirname, 'marks-int')
949 marks = Marks(marks_path)
950
951 parser = Parser(repo)
952 for line in parser:
953 if parser.check('capabilities'):
954 do_capabilities(parser)
955 elif parser.check('list'):
956 do_list(parser)
957 elif parser.check('import'):
958 do_import(parser)
959 elif parser.check('export'):
960 do_export(parser)
961 elif parser.check('option'):
962 do_option(parser)
963 else:
964 die('unhandled command: %s' % line)
965 sys.stdout.flush()
966
967def bye():
968 if not marks:
969 return
970 if not is_tmp:
971 marks.store()
972 else:
973 shutil.rmtree(dirname)
974
975atexit.register(bye)
976sys.exit(main(sys.argv))