6 from git_remote_helpers
.util
import die
, debug
, warn
7 from git_remote_helpers
.git
.repo
import GitRepo
8 from git_remote_helpers
.git
.exporter
import GitExporter
9 from git_remote_helpers
.git
.importer
import GitImporter
10 from git_remote_helpers
.git
.non_local
import NonLocalGit
12 def get_repo(alias
, url
):
13 """Returns a git repository object initialized for usage.
20 hasher
= hashlib
.sha1()
21 hasher
.update(repo
.path
)
22 repo
.hash = hasher
.hexdigest()
24 repo
.get_base_path
= lambda base
: os
.path
.join(
25 base
, 'info', 'fast-import', repo
.hash)
27 prefix
= 'refs/testgit/%s/' % alias
28 debug("prefix: '%s'", prefix
)
34 repo
.exporter
= GitExporter(repo
)
35 repo
.importer
= GitImporter(repo
)
36 repo
.non_local
= NonLocalGit(repo
)
41 def local_repo(repo
, path
):
42 """Returns a git repository object initalized for usage.
47 local
.non_local
= None
48 local
.gitdir
= repo
.gitdir
49 local
.alias
= repo
.alias
50 local
.prefix
= repo
.prefix
51 local
.hash = repo
.hash
52 local
.get_base_path
= repo
.get_base_path
53 local
.exporter
= GitExporter(local
)
54 local
.importer
= GitImporter(local
)
59 def do_capabilities(repo
, args
):
60 """Prints the supported capabilities.
66 print "refspec refs/heads/*:%s*" % repo
.prefix
68 print # end capabilities
71 def do_list(repo
, args
):
72 """Lists all known references.
74 Bug: This will always set the remote head to master for non-local
75 repositories, since we have no way of determining what the remote
76 head is at clone time.
80 debug("? refs/heads/%s", ref
)
81 print "? refs/heads/%s" % ref
84 debug("@refs/heads/%s HEAD" % repo
.head
)
85 print "@refs/heads/%s HEAD" % repo
.head
87 debug("@refs/heads/master HEAD")
88 print "@refs/heads/master HEAD"
93 def update_local_repo(repo
):
94 """Updates (or clones) a local repo.
100 path
= repo
.non_local
.clone(repo
.gitdir
)
101 repo
.non_local
.update(repo
.gitdir
)
102 repo
= local_repo(repo
, path
)
106 def do_import(repo
, args
):
107 """Exports a fast-import stream from testgit for git to import.
111 die("Import needs exactly one ref")
114 die("Need gitdir to import")
116 repo
= update_local_repo(repo
)
117 repo
.exporter
.export_repo(repo
.gitdir
)
120 def do_export(repo
, args
):
121 """Imports a fast-import stream from git to testgit.
125 die("Need gitdir to export")
127 dirname
= repo
.get_base_path(repo
.gitdir
)
129 if not os
.path
.exists(dirname
):
132 path
= os
.path
.join(dirname
, 'testgit.marks')
134 print path
if os
.path
.exists(path
) else ""
137 update_local_repo(repo
)
138 repo
.importer
.do_import(repo
.gitdir
)
139 repo
.non_local
.push(repo
.gitdir
)
142 def do_gitdir(repo
, args
):
143 """Stores the location of the gitdir.
147 die("gitdir needs an argument")
149 repo
.gitdir
= ' '.join(args
)
153 'capabilities': do_capabilities
,
162 """Cleans up the url.
165 if value
.startswith('testgit::'):
171 def read_one_line(repo
):
172 """Reads and processes one command.
175 line
= sys
.stdin
.readline()
180 warn("Unexpected EOF")
183 cmdline
= cmdline
.strip().split()
185 # Blank line means we're about to quit
189 debug("Got command '%s' with args '%s'", cmd
, ' '.join(cmdline
))
191 if cmd
not in COMMANDS
:
192 die("Unknown command, %s", cmd
)
202 """Starts a new remote helper for the specified repository.
206 die("Expecting exactly three arguments.")
209 if os
.getenv("GIT_DEBUG_TESTGIT"):
210 import git_remote_helpers
.util
211 git_remote_helpers
.util
.DEBUG
= True
213 alias
= sanitize(args
[1])
214 url
= sanitize(args
[2])
216 if not alias
.isalnum():
217 warn("non-alnum alias '%s'", alias
)
223 repo
= get_repo(alias
, url
)
225 debug("Got arguments %s", args
[1:])
230 more
= read_one_line(repo
)
232 if __name__
== '__main__':
233 sys
.exit(main(sys
.argv
))