Mercurial > hg-git
annotate git_handler.py @ 5:d6c443a91b18
refactored the git handling stuff out into another class
author | Scott Chacon <schacon@gmail.com> |
---|---|
date | Thu, 23 Apr 2009 15:26:10 -0700 |
parents | |
children | c77197123d95 |
rev | line source |
---|---|
5
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
1 import os, errno, sys |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
2 import dulwich |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
3 from dulwich.repo import Repo |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
4 from dulwich.client import SimpleFetchGraphWalker |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
5 from hgext import bookmarks |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
6 from mercurial.i18n import _ |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
7 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
8 class GitHandler(object): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
9 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
10 def __init__(self, dest_repo, ui): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
11 self.repo = dest_repo |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
12 self.ui = ui |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
13 git_dir = os.path.join(self.repo.path, 'git') |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
14 self.git = Repo(git_dir) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
15 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
16 def fetch(self, git_url): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
17 self.ui.status(_("fetching from git url: " + git_url + "\n")) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
18 self.export_git_objects() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
19 self.fetch_pack(git_url) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
20 self.import_git_objects() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
21 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
22 def fetch_pack(self, git_url): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
23 client, path = self.get_transport_and_path(git_url) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
24 graphwalker = SimpleFetchGraphWalker(self.git.heads().values(), self.git.get_parents) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
25 f, commit = self.git.object_store.add_pack() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
26 try: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
27 determine_wants = self.git.object_store.determine_wants_all |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
28 refs = client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
29 f.close() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
30 commit() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
31 self.git.set_refs(refs) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
32 except: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
33 f.close() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
34 raise |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
35 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
36 def import_git_objects(self): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
37 self.ui.status(_("importing Git objects into Hg\n")) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
38 # import heads as remote references |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
39 todo = [] |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
40 done = set() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
41 convert_list = [] |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
42 for head, sha in self.git.heads().iteritems(): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
43 todo.append(sha) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
44 while todo: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
45 sha = todo.pop() |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
46 assert isinstance(sha, str) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
47 if sha in done: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
48 continue |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
49 done.add(sha) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
50 commit = self.git.commit(sha) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
51 convert_list.append(commit) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
52 todo.extend([p for p in commit.parents if p not in done]) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
53 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
54 convert_list.sort(cmp=lambda x,y: x.commit_time-y.commit_time) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
55 for commit in convert_list: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
56 print "commit: %s" % sha |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
57 self.import_git_commit(commit) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
58 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
59 print bookmarks.parse(self.repo) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
60 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
61 def import_git_commit(self, commit): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
62 # check that parents are all in Hg, or no parents |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
63 print commit.parents |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
64 pass |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
65 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
66 def export_git_objects(self): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
67 pass |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
68 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
69 def check_bookmarks(self): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
70 if self.ui.config('extensions', 'hgext.bookmarks') is not None: |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
71 print "YOU NEED TO SETUP BOOKMARKS" |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
72 |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
73 def get_transport_and_path(self, uri): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
74 from dulwich.client import TCPGitClient, SSHGitClient, SubprocessGitClient |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
75 for handler, transport in (("git://", TCPGitClient), ("git+ssh://", SSHGitClient)): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
76 if uri.startswith(handler): |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
77 host, path = uri[len(handler):].split("/", 1) |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
78 return transport(host), "/"+path |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
79 # if its not git or git+ssh, try a local url.. |
d6c443a91b18
refactored the git handling stuff out into another class
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
80 return SubprocessGitClient(), uri |