http: provide effective url to callers
[git.git] / git_remote_helpers / git / importer.py
blobd3f90e1024a1b3ac8e7b4204b873e0ac2fffeaf9
1 import os
2 import subprocess
4 from git_remote_helpers.util import check_call, check_output
7 class GitImporter(object):
8 """An importer for testgit repositories.
10 This importer simply delegates to git fast-import.
11 """
13 def __init__(self, repo):
14 """Creates a new importer for the specified repo.
15 """
17 self.repo = repo
19 def get_refs(self, gitdir):
20 """Returns a dictionary with refs.
22 Note that the keys in the returned dictionary are byte strings as
23 read from git.
24 """
25 args = ["git", "--git-dir=" + gitdir, "for-each-ref", "refs/heads"]
26 lines = check_output(args).strip().split('\n'.encode('ascii'))
27 refs = {}
28 for line in lines:
29 value, name = line.split(' '.encode('ascii'))
30 name = name.strip('commit\t'.encode('ascii'))
31 refs[name] = value
32 return refs
34 def do_import(self, base):
35 """Imports a fast-import stream to the given directory.
37 Simply delegates to git fast-import.
38 """
40 dirname = self.repo.get_base_path(base)
41 if self.repo.local:
42 gitdir = self.repo.gitpath
43 else:
44 gitdir = os.path.abspath(os.path.join(dirname, '.git'))
45 path = os.path.abspath(os.path.join(dirname, 'testgit.marks'))
47 if not os.path.exists(dirname):
48 os.makedirs(dirname)
50 refs_before = self.get_refs(gitdir)
52 args = ["git", "--git-dir=" + gitdir, "fast-import", "--quiet", "--export-marks=" + path]
54 if os.path.exists(path):
55 args.append("--import-marks=" + path)
57 check_call(args)
59 refs_after = self.get_refs(gitdir)
61 changed = {}
63 for name, value in refs_after.iteritems():
64 if refs_before.get(name) == value:
65 continue
67 changed[name] = value
69 return changed