git_remote_helpers / git / importer.pyon commit Merge branch 'ap/maint-diff-rename-avoid-overlap' into maint-1.8.1 (8f780ca)
   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        args = ["git", "--git-dir=" + gitdir, "for-each-ref", "refs/heads"]
  23        lines = check_output(args).strip().split('\n')
  24        refs = {}
  25        for line in lines:
  26            value, name = line.split(' ')
  27            name = name.strip('commit\t')
  28            refs[name] = value
  29        return refs
  30
  31    def do_import(self, base):
  32        """Imports a fast-import stream to the given directory.
  33
  34        Simply delegates to git fast-import.
  35        """
  36
  37        dirname = self.repo.get_base_path(base)
  38        if self.repo.local:
  39            gitdir = self.repo.gitpath
  40        else:
  41            gitdir = os.path.abspath(os.path.join(dirname, '.git'))
  42        path = os.path.abspath(os.path.join(dirname, 'git.marks'))
  43
  44        if not os.path.exists(dirname):
  45            os.makedirs(dirname)
  46
  47        refs_before = self.get_refs(gitdir)
  48
  49        args = ["git", "--git-dir=" + gitdir, "fast-import", "--quiet", "--export-marks=" + path]
  50
  51        if os.path.exists(path):
  52            args.append("--import-marks=" + path)
  53
  54        check_call(args)
  55
  56        refs_after = self.get_refs(gitdir)
  57
  58        changed = {}
  59
  60        for name, value in refs_after.iteritems():
  61            if refs_before.get(name) == value:
  62                continue
  63
  64            changed[name] = value
  65
  66        return changed