gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / git-remote-testgit.py
blob92539222c57d1966f847a641c976754f1941dc2b
1 #!/usr/bin/env python
3 import hashlib
4 import sys
5 import os
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.
16 """
18 repo = GitRepo(url)
19 repo.get_revs()
20 repo.get_head()
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)
32 repo.gitdir = ""
33 repo.alias = alias
34 repo.prefix = prefix
36 repo.exporter = GitExporter(repo)
37 repo.importer = GitImporter(repo)
38 repo.non_local = NonLocalGit(repo)
40 return repo
43 def local_repo(repo, path):
44 """Returns a git repository object initalized for usage.
45 """
47 local = GitRepo(path)
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)
58 return local
61 def do_capabilities(repo, args):
62 """Prints the supported capabilities.
63 """
65 print "import"
66 print "export"
67 print "gitdir"
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.
79 """
81 for ref in repo.revs:
82 debug("? refs/heads/%s", ref)
83 print "? refs/heads/%s" % ref
85 if repo.head:
86 debug("@refs/heads/%s HEAD" % repo.head)
87 print "@refs/heads/%s HEAD" % repo.head
88 else:
89 debug("@refs/heads/master HEAD")
90 print "@refs/heads/master HEAD"
92 print # end list
95 def update_local_repo(repo):
96 """Updates (or clones) a local repo.
97 """
99 if repo.local:
100 return repo
102 path = repo.non_local.clone(repo.gitdir)
103 repo.non_local.update(repo.gitdir)
104 repo = local_repo(repo, path)
105 return repo
108 def do_import(repo, args):
109 """Exports a fast-import stream from testgit for git to import.
112 if len(args) != 1:
113 die("Import needs exactly one ref")
115 if not repo.gitdir:
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.
126 if not repo.gitdir:
127 die("Need gitdir to export")
129 dirname = repo.get_base_path(repo.gitdir)
131 if not os.path.exists(dirname):
132 os.makedirs(dirname)
134 path = os.path.join(dirname, 'testgit.marks')
135 print path
136 print path if os.path.exists(path) else ""
137 sys.stdout.flush()
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.
148 if not args:
149 die("gitdir needs an argument")
151 repo.gitdir = ' '.join(args)
154 COMMANDS = {
155 'capabilities': do_capabilities,
156 'list': do_list,
157 'import': do_import,
158 'export': do_export,
159 'gitdir': do_gitdir,
163 def sanitize(value):
164 """Cleans up the url.
167 if value.startswith('testgit::'):
168 value = value[9:]
170 return value
173 def read_one_line(repo):
174 """Reads and processes one command.
177 line = sys.stdin.readline()
179 cmdline = line
181 if not cmdline:
182 warn("Unexpected EOF")
183 return False
185 cmdline = cmdline.strip().split()
186 if not cmdline:
187 # Blank line means we're about to quit
188 return False
190 cmd = cmdline.pop(0)
191 debug("Got command '%s' with args '%s'", cmd, ' '.join(cmdline))
193 if cmd not in COMMANDS:
194 die("Unknown command, %s", cmd)
196 func = COMMANDS[cmd]
197 func(repo, cmdline)
198 sys.stdout.flush()
200 return True
203 def main(args):
204 """Starts a new remote helper for the specified repository.
207 if len(args) != 3:
208 die("Expecting exactly three arguments.")
209 sys.exit(1)
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)
220 alias = "tmp"
222 args[1] = alias
223 args[2] = url
225 repo = get_repo(alias, url)
227 debug("Got arguments %s", args[1:])
229 more = True
231 while (more):
232 more = read_one_line(repo)
234 if __name__ == '__main__':
235 sys.exit(main(sys.argv))