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