6 sys.path.insert(0, os.getenv("GITPYTHONLIB","."))
8 from git_remote_helpers.util import die, debug, warn
9 from git_remote_helpers.git.repo import GitRepo
10 from git_remote_helpers.git.exporter import GitExporter
11 from git_remote_helpers.git.importer import GitImporter
12 from git_remote_helpers.git.non_local import NonLocalGit
14 def get_repo(alias, url):
15 """Returns a git repository object initialized for usage.
22 hasher = hashlib.sha1()
23 hasher.update(repo.path)
24 repo.hash = hasher.hexdigest()
26 repo.get_base_path = lambda base: os.path.join(
27 base, 'info', 'fast-import', repo.hash)
29 prefix = 'refs/testgit/%s/' % alias
30 debug("prefix: '%s'", prefix)
36 repo.exporter = GitExporter(repo)
37 repo.importer = GitImporter(repo)
38 repo.non_local = NonLocalGit(repo)
43 def local_repo(repo, path):
44 """Returns a git repository object initalized for usage.
49 local.non_local = None
50 local.gitdir = repo.gitdir
51 local.alias = repo.alias
52 local.prefix = repo.prefix
53 local.hash = repo.hash
54 local.get_base_path = repo.get_base_path
55 local.exporter = GitExporter(local)
56 local.importer = GitImporter(local)
61 def do_capabilities(repo, args):
62 """Prints the supported capabilities.
68 print "refspec refs/heads/*:%s*" % repo.prefix
70 print # end capabilities
73 def do_list(repo, args):
74 """Lists all known references.
76 Bug: This will always set the remote head to master for non-local
77 repositories, since we have no way of determining what the remote
78 head is at clone time.
82 debug("? refs/heads/%s", ref)
83 print "? refs/heads/%s" % ref
86 debug("@refs/heads/%s HEAD" % repo.head)
87 print "@refs/heads/%s HEAD" % repo.head
89 debug("@refs/heads/master HEAD")
90 print "@refs/heads/master HEAD"
95 def update_local_repo(repo):
96 """Updates (or clones) a local repo.
102 path = repo.non_local.clone(repo.gitdir)
103 repo.non_local.update(repo.gitdir)
104 repo = local_repo(repo, path)
108 def do_import(repo, args):
109 """Exports a fast-import stream from testgit for git to import.
113 die("Import needs exactly one ref")
116 die("Need gitdir to import")
118 repo = update_local_repo(repo)
119 repo.exporter.export_repo(repo.gitdir)
122 def do_export(repo, args):
123 """Imports a fast-import stream from git to testgit.
127 die("Need gitdir to export")
129 dirname = repo.get_base_path(repo.gitdir)
131 if not os.path.exists(dirname):
134 path = os.path.join(dirname, 'testgit.marks')
136 print path if os.path.exists(path) else ""
139 update_local_repo(repo)
140 repo.importer.do_import(repo.gitdir)
141 repo.non_local.push(repo.gitdir)
144 def do_gitdir(repo, args):
145 """Stores the location of the gitdir.
149 die("gitdir needs an argument")
151 repo.gitdir = ' '.join(args)
155 'capabilities': do_capabilities,
164 """Cleans up the url.
167 if value.startswith('testgit::'):
173 def read_one_line(repo):
174 """Reads and processes one command.
177 line = sys.stdin.readline()
182 warn("Unexpected EOF")
185 cmdline = cmdline.strip().split()
187 # Blank line means we're about to quit
191 debug("Got command '%s' with args '%s'", cmd, ' '.join(cmdline))
193 if cmd not in COMMANDS:
194 die("Unknown command, %s", cmd)
204 """Starts a new remote helper for the specified repository.
208 die("Expecting exactly three arguments.")
211 if os.getenv("GIT_DEBUG_TESTGIT"):
212 import git_remote_helpers.util
213 git_remote_helpers.util.DEBUG = True
215 alias = sanitize(args[1])
216 url = sanitize(args[2])
218 if not alias.isalnum():
219 warn("non-alnum alias '%s'", alias)
225 repo = get_repo(alias, url)
227 debug("Got arguments %s", args[1:])
232 more = read_one_line(repo)
234 if __name__ == '__main__':
235 sys.exit(main(sys.argv))