0b5928d29067f4d0306ab7b8f6a20c1711e47063
1#!/usr/bin/env python
2
3# hashlib is only available in python >= 2.5
4try:
5 import hashlib
6 _digest = hashlib.sha1
7except ImportError:
8 import sha
9 _digest = sha.new
10import sys
11import os
12sys.path.insert(0, os.getenv("GITPYTHONLIB","."))
13
14from git_remote_helpers.util import die, debug, warn
15from git_remote_helpers.git.repo import GitRepo
16from git_remote_helpers.git.exporter import GitExporter
17from git_remote_helpers.git.importer import GitImporter
18from git_remote_helpers.git.non_local import NonLocalGit
19
20def get_repo(alias, url):
21 """Returns a git repository object initialized for usage.
22 """
23
24 repo = GitRepo(url)
25 repo.get_revs()
26 repo.get_head()
27
28 hasher = _digest()
29 hasher.update(repo.path)
30 repo.hash = hasher.hexdigest()
31
32 repo.get_base_path = lambda base: os.path.join(
33 base, 'info', 'fast-import', repo.hash)
34
35 prefix = 'refs/testgit/%s/' % alias
36 debug("prefix: '%s'", prefix)
37
38 repo.gitdir = os.environ["GIT_DIR"]
39 repo.alias = alias
40 repo.prefix = prefix
41
42 repo.exporter = GitExporter(repo)
43 repo.importer = GitImporter(repo)
44 repo.non_local = NonLocalGit(repo)
45
46 return repo
47
48
49def local_repo(repo, path):
50 """Returns a git repository object initalized for usage.
51 """
52
53 local = GitRepo(path)
54
55 local.non_local = None
56 local.gitdir = repo.gitdir
57 local.alias = repo.alias
58 local.prefix = repo.prefix
59 local.hash = repo.hash
60 local.get_base_path = repo.get_base_path
61 local.exporter = GitExporter(local)
62 local.importer = GitImporter(local)
63
64 return local
65
66
67def do_capabilities(repo, args):
68 """Prints the supported capabilities.
69 """
70
71 print "import"
72 print "export"
73 print "refspec refs/heads/*:%s*" % repo.prefix
74
75 print # end capabilities
76
77
78def do_list(repo, args):
79 """Lists all known references.
80
81 Bug: This will always set the remote head to master for non-local
82 repositories, since we have no way of determining what the remote
83 head is at clone time.
84 """
85
86 for ref in repo.revs:
87 debug("? refs/heads/%s", ref)
88 print "? refs/heads/%s" % ref
89
90 if repo.head:
91 debug("@refs/heads/%s HEAD" % repo.head)
92 print "@refs/heads/%s HEAD" % repo.head
93 else:
94 debug("@refs/heads/master HEAD")
95 print "@refs/heads/master HEAD"
96
97 print # end list
98
99
100def update_local_repo(repo):
101 """Updates (or clones) a local repo.
102 """
103
104 if repo.local:
105 return repo
106
107 path = repo.non_local.clone(repo.gitdir)
108 repo.non_local.update(repo.gitdir)
109 repo = local_repo(repo, path)
110 return repo
111
112
113def do_import(repo, args):
114 """Exports a fast-import stream from testgit for git to import.
115 """
116
117 if len(args) != 1:
118 die("Import needs exactly one ref")
119
120 if not repo.gitdir:
121 die("Need gitdir to import")
122
123 repo = update_local_repo(repo)
124 repo.exporter.export_repo(repo.gitdir, args)
125
126 print "done"
127
128
129def do_export(repo, args):
130 """Imports a fast-import stream from git to testgit.
131 """
132
133 if not repo.gitdir:
134 die("Need gitdir to export")
135
136 dirname = repo.get_base_path(repo.gitdir)
137
138 if not os.path.exists(dirname):
139 os.makedirs(dirname)
140
141 path = os.path.join(dirname, 'testgit.marks')
142 print path
143 if os.path.exists(path):
144 print path
145 else:
146 print ""
147 sys.stdout.flush()
148
149 update_local_repo(repo)
150 changed = repo.importer.do_import(repo.gitdir)
151
152 if not repo.local:
153 repo.non_local.push(repo.gitdir)
154
155 for ref in changed:
156 print "ok %s" % ref
157 print
158
159
160COMMANDS = {
161 'capabilities': do_capabilities,
162 'list': do_list,
163 'import': do_import,
164 'export': do_export,
165}
166
167
168def sanitize(value):
169 """Cleans up the url.
170 """
171
172 if value.startswith('testgit::'):
173 value = value[9:]
174
175 return value
176
177
178def read_one_line(repo):
179 """Reads and processes one command.
180 """
181
182 line = sys.stdin.readline()
183
184 cmdline = line
185
186 if not cmdline:
187 warn("Unexpected EOF")
188 return False
189
190 cmdline = cmdline.strip().split()
191 if not cmdline:
192 # Blank line means we're about to quit
193 return False
194
195 cmd = cmdline.pop(0)
196 debug("Got command '%s' with args '%s'", cmd, ' '.join(cmdline))
197
198 if cmd not in COMMANDS:
199 die("Unknown command, %s", cmd)
200
201 func = COMMANDS[cmd]
202 func(repo, cmdline)
203 sys.stdout.flush()
204
205 return True
206
207
208def main(args):
209 """Starts a new remote helper for the specified repository.
210 """
211
212 if len(args) != 3:
213 die("Expecting exactly three arguments.")
214 sys.exit(1)
215
216 if os.getenv("GIT_DEBUG_TESTGIT"):
217 import git_remote_helpers.util
218 git_remote_helpers.util.DEBUG = True
219
220 alias = sanitize(args[1])
221 url = sanitize(args[2])
222
223 if not alias.isalnum():
224 warn("non-alnum alias '%s'", alias)
225 alias = "tmp"
226
227 args[1] = alias
228 args[2] = url
229
230 repo = get_repo(alias, url)
231
232 debug("Got arguments %s", args[1:])
233
234 more = True
235
236 while (more):
237 more = read_one_line(repo)
238
239if __name__ == '__main__':
240 sys.exit(main(sys.argv))