Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Matthieu Moy | 9609dc9 | 2011-09-01 18:49:38 +0200 | [diff] [blame] | 3 | # This command is a simple remote-helper, that is used both as a |
| 4 | # testcase for the remote-helper functionality, and as an example to |
| 5 | # show remote-helper authors one possible implementation. |
| 6 | # |
| 7 | # This is a Git <-> Git importer/exporter, that simply uses git |
| 8 | # fast-import and git fast-export to consume and produce fast-import |
| 9 | # streams. |
| 10 | # |
| 11 | # To understand better the way things work, one can activate debug |
| 12 | # traces by setting (to any value) the environment variables |
| 13 | # GIT_TRANSPORT_HELPER_DEBUG and GIT_DEBUG_TESTGIT, to see messages |
| 14 | # from the transport-helper side, or from this example remote-helper. |
| 15 | |
Brandon Casey | 23b093e | 2010-06-09 19:24:54 -0500 | [diff] [blame] | 16 | # hashlib is only available in python >= 2.5 |
| 17 | try: |
| 18 | import hashlib |
| 19 | _digest = hashlib.sha1 |
| 20 | except ImportError: |
| 21 | import sha |
| 22 | _digest = sha.new |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 23 | import sys |
Brian Gernhardt | f733f6a | 2010-04-09 11:57:45 -0400 | [diff] [blame] | 24 | import os |
Pete Wyckoff | 7fb8e16 | 2012-04-22 16:30:58 -0400 | [diff] [blame] | 25 | import time |
Brian Gernhardt | f733f6a | 2010-04-09 11:57:45 -0400 | [diff] [blame] | 26 | sys.path.insert(0, os.getenv("GITPYTHONLIB",".")) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 27 | |
| 28 | from git_remote_helpers.util import die, debug, warn |
| 29 | from git_remote_helpers.git.repo import GitRepo |
| 30 | from git_remote_helpers.git.exporter import GitExporter |
| 31 | from git_remote_helpers.git.importer import GitImporter |
| 32 | from git_remote_helpers.git.non_local import NonLocalGit |
| 33 | |
John Keeping | 0846b0c | 2013-01-20 13:15:36 +0000 | [diff] [blame] | 34 | if sys.hexversion < 0x02000000: |
| 35 | # string.encode() is the limiter |
| 36 | sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n") |
Eric S. Raymond | a33faf2 | 2012-12-28 11:40:59 -0500 | [diff] [blame] | 37 | sys.exit(1) |
| 38 | |
John Keeping | 3ac221a | 2013-01-27 14:50:56 +0000 | [diff] [blame] | 39 | |
| 40 | def encode_filepath(path): |
| 41 | """Encodes a Unicode file path to a byte string. |
| 42 | |
| 43 | On Python 2 this is a no-op; on Python 3 we encode the string as |
| 44 | suggested by [1] which allows an exact round-trip from the command line |
| 45 | to the filesystem. |
| 46 | |
| 47 | [1] http://docs.python.org/3/c-api/unicode.html#file-system-encoding |
| 48 | |
| 49 | """ |
| 50 | if sys.hexversion < 0x03000000: |
| 51 | return path |
| 52 | return path.encode(sys.getfilesystemencoding(), 'surrogateescape') |
| 53 | |
| 54 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 55 | def get_repo(alias, url): |
| 56 | """Returns a git repository object initialized for usage. |
| 57 | """ |
| 58 | |
| 59 | repo = GitRepo(url) |
| 60 | repo.get_revs() |
| 61 | repo.get_head() |
| 62 | |
Brandon Casey | 23b093e | 2010-06-09 19:24:54 -0500 | [diff] [blame] | 63 | hasher = _digest() |
John Keeping | 3ac221a | 2013-01-27 14:50:56 +0000 | [diff] [blame] | 64 | hasher.update(encode_filepath(repo.path)) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 65 | repo.hash = hasher.hexdigest() |
| 66 | |
| 67 | repo.get_base_path = lambda base: os.path.join( |
| 68 | base, 'info', 'fast-import', repo.hash) |
| 69 | |
| 70 | prefix = 'refs/testgit/%s/' % alias |
| 71 | debug("prefix: '%s'", prefix) |
| 72 | |
Dmitry Ivankov | e173587 | 2011-07-16 15:03:28 +0200 | [diff] [blame] | 73 | repo.gitdir = os.environ["GIT_DIR"] |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 74 | repo.alias = alias |
| 75 | repo.prefix = prefix |
| 76 | |
| 77 | repo.exporter = GitExporter(repo) |
| 78 | repo.importer = GitImporter(repo) |
| 79 | repo.non_local = NonLocalGit(repo) |
| 80 | |
| 81 | return repo |
| 82 | |
| 83 | |
| 84 | def local_repo(repo, path): |
| 85 | """Returns a git repository object initalized for usage. |
| 86 | """ |
| 87 | |
| 88 | local = GitRepo(path) |
| 89 | |
| 90 | local.non_local = None |
| 91 | local.gitdir = repo.gitdir |
| 92 | local.alias = repo.alias |
| 93 | local.prefix = repo.prefix |
| 94 | local.hash = repo.hash |
| 95 | local.get_base_path = repo.get_base_path |
| 96 | local.exporter = GitExporter(local) |
| 97 | local.importer = GitImporter(local) |
| 98 | |
| 99 | return local |
| 100 | |
| 101 | |
| 102 | def do_capabilities(repo, args): |
| 103 | """Prints the supported capabilities. |
| 104 | """ |
| 105 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 106 | print("import") |
| 107 | print("export") |
| 108 | print("refspec refs/heads/*:%s*" % repo.prefix) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 109 | |
Sverre Rabbelier | a515ebe | 2011-07-16 15:03:40 +0200 | [diff] [blame] | 110 | dirname = repo.get_base_path(repo.gitdir) |
| 111 | |
| 112 | if not os.path.exists(dirname): |
| 113 | os.makedirs(dirname) |
| 114 | |
Felipe Contreras | 3b70526 | 2012-11-24 04:17:02 +0100 | [diff] [blame] | 115 | path = os.path.join(dirname, 'git.marks') |
Sverre Rabbelier | a515ebe | 2011-07-16 15:03:40 +0200 | [diff] [blame] | 116 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 117 | print("*export-marks %s" % path) |
Sverre Rabbelier | a515ebe | 2011-07-16 15:03:40 +0200 | [diff] [blame] | 118 | if os.path.exists(path): |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 119 | print("*import-marks %s" % path) |
Sverre Rabbelier | a515ebe | 2011-07-16 15:03:40 +0200 | [diff] [blame] | 120 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 121 | print('') # end capabilities |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def do_list(repo, args): |
| 125 | """Lists all known references. |
| 126 | |
| 127 | Bug: This will always set the remote head to master for non-local |
| 128 | repositories, since we have no way of determining what the remote |
| 129 | head is at clone time. |
| 130 | """ |
| 131 | |
| 132 | for ref in repo.revs: |
| 133 | debug("? refs/heads/%s", ref) |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 134 | print("? refs/heads/%s" % ref) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 135 | |
| 136 | if repo.head: |
| 137 | debug("@refs/heads/%s HEAD" % repo.head) |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 138 | print("@refs/heads/%s HEAD" % repo.head) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 139 | else: |
| 140 | debug("@refs/heads/master HEAD") |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 141 | print("@refs/heads/master HEAD") |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 142 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 143 | print('') # end list |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def update_local_repo(repo): |
| 147 | """Updates (or clones) a local repo. |
| 148 | """ |
| 149 | |
| 150 | if repo.local: |
| 151 | return repo |
| 152 | |
| 153 | path = repo.non_local.clone(repo.gitdir) |
| 154 | repo.non_local.update(repo.gitdir) |
| 155 | repo = local_repo(repo, path) |
| 156 | return repo |
| 157 | |
| 158 | |
| 159 | def do_import(repo, args): |
| 160 | """Exports a fast-import stream from testgit for git to import. |
| 161 | """ |
| 162 | |
| 163 | if len(args) != 1: |
| 164 | die("Import needs exactly one ref") |
| 165 | |
| 166 | if not repo.gitdir: |
| 167 | die("Need gitdir to import") |
| 168 | |
Sverre Rabbelier | 9504bc9 | 2011-07-16 15:03:38 +0200 | [diff] [blame] | 169 | ref = args[0] |
| 170 | refs = [ref] |
| 171 | |
| 172 | while True: |
John Keeping | d04c94a | 2013-01-20 13:15:37 +0000 | [diff] [blame] | 173 | line = sys.stdin.readline().decode() |
Sverre Rabbelier | 9504bc9 | 2011-07-16 15:03:38 +0200 | [diff] [blame] | 174 | if line == '\n': |
| 175 | break |
| 176 | if not line.startswith('import '): |
| 177 | die("Expected import line.") |
| 178 | |
| 179 | # strip of leading 'import ' |
| 180 | ref = line[7:].strip() |
| 181 | refs.append(ref) |
| 182 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 183 | print("feature done") |
Felipe Contreras | 6c32332 | 2012-10-22 22:56:34 +0200 | [diff] [blame] | 184 | |
| 185 | if os.environ.get("GIT_REMOTE_TESTGIT_FAILURE"): |
| 186 | die('Told to fail') |
| 187 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 188 | repo = update_local_repo(repo) |
Sverre Rabbelier | 9504bc9 | 2011-07-16 15:03:38 +0200 | [diff] [blame] | 189 | repo.exporter.export_repo(repo.gitdir, refs) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 190 | |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 191 | print("done") |
Sverre Rabbelier | 1f25c50 | 2011-07-16 15:03:36 +0200 | [diff] [blame] | 192 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 193 | |
| 194 | def do_export(repo, args): |
| 195 | """Imports a fast-import stream from git to testgit. |
| 196 | """ |
| 197 | |
| 198 | if not repo.gitdir: |
| 199 | die("Need gitdir to export") |
| 200 | |
Felipe Contreras | 6c32332 | 2012-10-22 22:56:34 +0200 | [diff] [blame] | 201 | if os.environ.get("GIT_REMOTE_TESTGIT_FAILURE"): |
| 202 | die('Told to fail') |
| 203 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 204 | update_local_repo(repo) |
Sverre Rabbelier | 6c8151a | 2011-07-16 15:03:37 +0200 | [diff] [blame] | 205 | changed = repo.importer.do_import(repo.gitdir) |
Sverre Rabbelier | 0fb56ce | 2011-07-16 15:03:30 +0200 | [diff] [blame] | 206 | |
| 207 | if not repo.local: |
| 208 | repo.non_local.push(repo.gitdir) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 209 | |
Sverre Rabbelier | 6c8151a | 2011-07-16 15:03:37 +0200 | [diff] [blame] | 210 | for ref in changed: |
John Keeping | f9640ac | 2013-01-20 13:15:38 +0000 | [diff] [blame] | 211 | print("ok %s" % ref) |
| 212 | print('') |
Sverre Rabbelier | 6c8151a | 2011-07-16 15:03:37 +0200 | [diff] [blame] | 213 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 214 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 215 | COMMANDS = { |
| 216 | 'capabilities': do_capabilities, |
| 217 | 'list': do_list, |
| 218 | 'import': do_import, |
| 219 | 'export': do_export, |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
| 223 | def sanitize(value): |
| 224 | """Cleans up the url. |
| 225 | """ |
| 226 | |
| 227 | if value.startswith('testgit::'): |
| 228 | value = value[9:] |
| 229 | |
| 230 | return value |
| 231 | |
| 232 | |
| 233 | def read_one_line(repo): |
| 234 | """Reads and processes one command. |
| 235 | """ |
| 236 | |
Pete Wyckoff | 7fb8e16 | 2012-04-22 16:30:58 -0400 | [diff] [blame] | 237 | sleepy = os.environ.get("GIT_REMOTE_TESTGIT_SLEEPY") |
| 238 | if sleepy: |
| 239 | debug("Sleeping %d sec before readline" % int(sleepy)) |
| 240 | time.sleep(int(sleepy)) |
| 241 | |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 242 | line = sys.stdin.readline() |
| 243 | |
John Keeping | d04c94a | 2013-01-20 13:15:37 +0000 | [diff] [blame] | 244 | cmdline = line.decode() |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 245 | |
| 246 | if not cmdline: |
| 247 | warn("Unexpected EOF") |
| 248 | return False |
| 249 | |
| 250 | cmdline = cmdline.strip().split() |
| 251 | if not cmdline: |
| 252 | # Blank line means we're about to quit |
| 253 | return False |
| 254 | |
| 255 | cmd = cmdline.pop(0) |
| 256 | debug("Got command '%s' with args '%s'", cmd, ' '.join(cmdline)) |
| 257 | |
| 258 | if cmd not in COMMANDS: |
| 259 | die("Unknown command, %s", cmd) |
| 260 | |
| 261 | func = COMMANDS[cmd] |
| 262 | func(repo, cmdline) |
| 263 | sys.stdout.flush() |
| 264 | |
| 265 | return True |
| 266 | |
| 267 | |
| 268 | def main(args): |
| 269 | """Starts a new remote helper for the specified repository. |
| 270 | """ |
| 271 | |
| 272 | if len(args) != 3: |
| 273 | die("Expecting exactly three arguments.") |
| 274 | sys.exit(1) |
| 275 | |
| 276 | if os.getenv("GIT_DEBUG_TESTGIT"): |
| 277 | import git_remote_helpers.util |
| 278 | git_remote_helpers.util.DEBUG = True |
| 279 | |
| 280 | alias = sanitize(args[1]) |
| 281 | url = sanitize(args[2]) |
| 282 | |
| 283 | if not alias.isalnum(): |
| 284 | warn("non-alnum alias '%s'", alias) |
| 285 | alias = "tmp" |
| 286 | |
| 287 | args[1] = alias |
| 288 | args[2] = url |
| 289 | |
| 290 | repo = get_repo(alias, url) |
| 291 | |
| 292 | debug("Got arguments %s", args[1:]) |
| 293 | |
| 294 | more = True |
| 295 | |
John Keeping | d04c94a | 2013-01-20 13:15:37 +0000 | [diff] [blame] | 296 | # Use binary mode since Python 3 does not permit unbuffered I/O in text |
| 297 | # mode. Unbuffered I/O is required to avoid data that should be going |
| 298 | # to git-fast-import after an "export" command getting caught in our |
| 299 | # stdin buffer instead. |
| 300 | sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) |
Sverre Rabbelier | 7aeaa2f | 2010-03-29 11:48:28 -0500 | [diff] [blame] | 301 | while (more): |
| 302 | more = read_one_line(repo) |
| 303 | |
| 304 | if __name__ == '__main__': |
| 305 | sys.exit(main(sys.argv)) |