git_remote_helpers / git / importer.pyon commit Merge branch 'bc/http-keep-memory-given-to-curl' into maint (29b2f05)
   1import os
   2import subprocess
   3
   4from git_remote_helpers.util import check_call, check_output
   5
   6
   7class GitImporter(object):
   8    """An importer for testgit repositories.
   9
  10    This importer simply delegates to git fast-import.
  11    """
  12
  13    def __init__(self, repo):
  14        """Creates a new importer for the specified repo.
  15        """
  16
  17        self.repo = repo
  18
  19    def get_refs(self, gitdir):
  20        """Returns a dictionary with refs.
  21
  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
  33
  34    def do_import(self, base):
  35        """Imports a fast-import stream to the given directory.
  36
  37        Simply delegates to git fast-import.
  38        """
  39
  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'))
  46
  47        if not os.path.exists(dirname):
  48            os.makedirs(dirname)
  49
  50        refs_before = self.get_refs(gitdir)
  51
  52        args = ["git", "--git-dir=" + gitdir, "fast-import", "--quiet", "--export-marks=" + path]
  53
  54        if os.path.exists(path):
  55            args.append("--import-marks=" + path)
  56
  57        check_call(args)
  58
  59        refs_after = self.get_refs(gitdir)
  60
  61        changed = {}
  62
  63        for name, value in refs_after.iteritems():
  64            if refs_before.get(name) == value:
  65                continue
  66
  67            changed[name] = value
  68
  69        return changed