remote-helpers: export GIT_DIR variable to helpers
[git/jrn.git] / git-remote-testgit.py
blobb0c1e9b273a2763300eba3431eb001aa9cd75263
1 #!/usr/bin/env python
3 # hashlib is only available in python >= 2.5
4 try:
5 import hashlib
6 _digest = hashlib.sha1
7 except ImportError:
8 import sha
9 _digest = sha.new
10 import sys
11 import os
12 sys.path.insert(0, os.getenv("GITPYTHONLIB","."))
14 from git_remote_helpers.util import die, debug, warn
15 from git_remote_helpers.git.repo import GitRepo
16 from git_remote_helpers.git.exporter import GitExporter
17 from git_remote_helpers.git.importer import GitImporter
18 from git_remote_helpers.git.non_local import NonLocalGit
20 def get_repo(alias, url):
21 """Returns a git repository object initialized for usage.
22 """
24 repo = GitRepo(url)
25 repo.get_revs()
26 repo.get_head()
28 hasher = _digest()
29 hasher.update(repo.path)
30 repo.hash = hasher.hexdigest()
32 repo.get_base_path = lambda base: os.path.join(
33 base, 'info', 'fast-import', repo.hash)
35 prefix = 'refs/testgit/%s/' % alias
36 debug("prefix: '%s'", prefix)
38 repo.gitdir = os.environ["GIT_DIR"]
39 repo.alias = alias
40 repo.prefix = prefix
42 repo.exporter = GitExporter(repo)
43 repo.importer = GitImporter(repo)
44 repo.non_local = NonLocalGit(repo)
46 return repo
49 def local_repo(repo, path):
50 """Returns a git repository object initalized for usage.
51 """
53 local = GitRepo(path)
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)
64 return local
67 def do_capabilities(repo, args):
68 """Prints the supported capabilities.
69 """
71 print "import"
72 print "export"
73 print "refspec refs/heads/*:%s*" % repo.prefix
75 print # end capabilities
78 def do_list(repo, args):
79 """Lists all known references.
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 """
86 for ref in repo.revs:
87 debug("? refs/heads/%s", ref)
88 print "? refs/heads/%s" % ref
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"
97 print # end list
100 def update_local_repo(repo):
101 """Updates (or clones) a local repo.
104 if repo.local:
105 return repo
107 path = repo.non_local.clone(repo.gitdir)
108 repo.non_local.update(repo.gitdir)
109 repo = local_repo(repo, path)
110 return repo
113 def do_import(repo, args):
114 """Exports a fast-import stream from testgit for git to import.
117 if len(args) != 1:
118 die("Import needs exactly one ref")
120 if not repo.gitdir:
121 die("Need gitdir to import")
123 repo = update_local_repo(repo)
124 repo.exporter.export_repo(repo.gitdir, args)
127 def do_export(repo, args):
128 """Imports a fast-import stream from git to testgit.
131 if not repo.gitdir:
132 die("Need gitdir to export")
134 dirname = repo.get_base_path(repo.gitdir)
136 if not os.path.exists(dirname):
137 os.makedirs(dirname)
139 path = os.path.join(dirname, 'testgit.marks')
140 print path
141 if os.path.exists(path):
142 print path
143 else:
144 print ""
145 sys.stdout.flush()
147 update_local_repo(repo)
148 repo.importer.do_import(repo.gitdir)
149 repo.non_local.push(repo.gitdir)
152 COMMANDS = {
153 'capabilities': do_capabilities,
154 'list': do_list,
155 'import': do_import,
156 'export': do_export,
160 def sanitize(value):
161 """Cleans up the url.
164 if value.startswith('testgit::'):
165 value = value[9:]
167 return value
170 def read_one_line(repo):
171 """Reads and processes one command.
174 line = sys.stdin.readline()
176 cmdline = line
178 if not cmdline:
179 warn("Unexpected EOF")
180 return False
182 cmdline = cmdline.strip().split()
183 if not cmdline:
184 # Blank line means we're about to quit
185 return False
187 cmd = cmdline.pop(0)
188 debug("Got command '%s' with args '%s'", cmd, ' '.join(cmdline))
190 if cmd not in COMMANDS:
191 die("Unknown command, %s", cmd)
193 func = COMMANDS[cmd]
194 func(repo, cmdline)
195 sys.stdout.flush()
197 return True
200 def main(args):
201 """Starts a new remote helper for the specified repository.
204 if len(args) != 3:
205 die("Expecting exactly three arguments.")
206 sys.exit(1)
208 if os.getenv("GIT_DEBUG_TESTGIT"):
209 import git_remote_helpers.util
210 git_remote_helpers.util.DEBUG = True
212 alias = sanitize(args[1])
213 url = sanitize(args[2])
215 if not alias.isalnum():
216 warn("non-alnum alias '%s'", alias)
217 alias = "tmp"
219 args[1] = alias
220 args[2] = url
222 repo = get_repo(alias, url)
224 debug("Got arguments %s", args[1:])
226 more = True
228 while (more):
229 more = read_one_line(repo)
231 if __name__ == '__main__':
232 sys.exit(main(sys.argv))