3 # hashlib is only available in python >= 2.5
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.
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"]
42 repo
.exporter
= GitExporter(repo
)
43 repo
.importer
= GitImporter(repo
)
44 repo
.non_local
= NonLocalGit(repo
)
49 def local_repo(repo
, path
):
50 """Returns a git repository object initalized for usage.
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
)
67 def do_capabilities(repo
, args
):
68 """Prints the supported capabilities.
73 print "refspec refs/heads/*:%s*" % repo
.prefix
75 dirname
= repo
.get_base_path(repo
.gitdir
)
77 if not os
.path
.exists(dirname
):
80 path
= os
.path
.join(dirname
, 'testgit.marks')
82 print "*export-marks %s" % path
83 if os
.path
.exists(path
):
84 print "*import-marks %s" % path
86 print # end capabilities
89 def do_list(repo
, args
):
90 """Lists all known references.
92 Bug: This will always set the remote head to master for non-local
93 repositories, since we have no way of determining what the remote
94 head is at clone time.
98 debug("? refs/heads/%s", ref
)
99 print "? refs/heads/%s" % ref
102 debug("@refs/heads/%s HEAD" % repo
.head
)
103 print "@refs/heads/%s HEAD" % repo
.head
105 debug("@refs/heads/master HEAD")
106 print "@refs/heads/master HEAD"
111 def update_local_repo(repo
):
112 """Updates (or clones) a local repo.
118 path
= repo
.non_local
.clone(repo
.gitdir
)
119 repo
.non_local
.update(repo
.gitdir
)
120 repo
= local_repo(repo
, path
)
124 def do_import(repo
, args
):
125 """Exports a fast-import stream from testgit for git to import.
129 die("Import needs exactly one ref")
132 die("Need gitdir to import")
138 line
= sys
.stdin
.readline()
141 if not line
.startswith('import '):
142 die("Expected import line.")
144 # strip of leading 'import '
145 ref
= line
[7:].strip()
148 repo
= update_local_repo(repo
)
149 repo
.exporter
.export_repo(repo
.gitdir
, refs
)
154 def do_export(repo
, args
):
155 """Imports a fast-import stream from git to testgit.
159 die("Need gitdir to export")
161 update_local_repo(repo
)
162 changed
= repo
.importer
.do_import(repo
.gitdir
)
165 repo
.non_local
.push(repo
.gitdir
)
173 'capabilities': do_capabilities
,
181 """Cleans up the url.
184 if value
.startswith('testgit::'):
190 def read_one_line(repo
):
191 """Reads and processes one command.
194 line
= sys
.stdin
.readline()
199 warn("Unexpected EOF")
202 cmdline
= cmdline
.strip().split()
204 # Blank line means we're about to quit
208 debug("Got command '%s' with args '%s'", cmd
, ' '.join(cmdline
))
210 if cmd
not in COMMANDS
:
211 die("Unknown command, %s", cmd
)
221 """Starts a new remote helper for the specified repository.
225 die("Expecting exactly three arguments.")
228 if os
.getenv("GIT_DEBUG_TESTGIT"):
229 import git_remote_helpers
.util
230 git_remote_helpers
.util
.DEBUG
= True
232 alias
= sanitize(args
[1])
233 url
= sanitize(args
[2])
235 if not alias
.isalnum():
236 warn("non-alnum alias '%s'", alias
)
242 repo
= get_repo(alias
, url
)
244 debug("Got arguments %s", args
[1:])
249 more
= read_one_line(repo
)
251 if __name__
== '__main__':
252 sys
.exit(main(sys
.argv
))