git_remote_helpers / git / exporter.pyon commit Merge branch 'dk/maint-t5150-dirname' (9a3f598)
   1import os
   2import subprocess
   3import sys
   4
   5from git_remote_helpers.util import check_call
   6
   7
   8class GitExporter(object):
   9    """An exporter for testgit repositories.
  10
  11    The exporter simply delegates to git fast-export.
  12    """
  13
  14    def __init__(self, repo):
  15        """Creates a new exporter for the specified repo.
  16        """
  17
  18        self.repo = repo
  19
  20    def export_repo(self, base, refs=None):
  21        """Exports a fast-export stream for the given directory.
  22
  23        Simply delegates to git fast-epxort and pipes it through sed
  24        to make the refs show up under the prefix rather than the
  25        default refs/heads. This is to demonstrate how the export
  26        data can be stored under it's own ref (using the refspec
  27        capability).
  28
  29        If None, refs defaults to ["HEAD"].
  30        """
  31
  32        if not refs:
  33            refs = ["HEAD"]
  34
  35        dirname = self.repo.get_base_path(base)
  36        path = os.path.abspath(os.path.join(dirname, 'testgit.marks'))
  37
  38        if not os.path.exists(dirname):
  39            os.makedirs(dirname)
  40
  41        print "feature relative-marks"
  42        if os.path.exists(os.path.join(dirname, 'git.marks')):
  43            print "feature import-marks=%s/git.marks" % self.repo.hash
  44        print "feature export-marks=%s/git.marks" % self.repo.hash
  45        sys.stdout.flush()
  46
  47        args = ["git", "--git-dir=" + self.repo.gitpath, "fast-export", "--export-marks=" + path]
  48
  49        if os.path.exists(path):
  50            args.append("--import-marks=" + path)
  51
  52        args.extend(refs)
  53
  54        p1 = subprocess.Popen(args, stdout=subprocess.PIPE)
  55
  56        args = ["sed", "s_refs/heads/_" + self.repo.prefix + "_g"]
  57
  58        check_call(args, stdin=p1.stdout)