1import os
   2import subprocess
   3from git_remote_helpers.util import die, warn
   5class NonLocalGit(object):
   8    """Handler to interact with non-local repos.
   9    """
  10    def __init__(self, repo):
  12        """Creates a new non-local handler for the specified repo.
  13        """
  14        self.repo = repo
  16    def clone(self, base):
  18        """Clones the non-local repo to base.
  19        Does nothing if a clone already exists.
  21        """
  22        path = os.path.join(self.repo.get_base_path(base), '.git')
  24        # already cloned
  26        if os.path.exists(path):
  27            return path
  28        os.makedirs(path)
  30        args = ["git", "clone", "--bare", "--quiet", self.repo.gitpath, path]
  31        child = subprocess.Popen(args)
  33        if child.wait() != 0:
  34            raise CalledProcessError
  35        return path
  37    def update(self, base):
  39        """Updates checkout of the non-local repo in base.
  40        """
  41        path = os.path.join(self.repo.get_base_path(base), '.git')
  43        if not os.path.exists(path):
  45            die("could not find repo at %s", path)
  46        args = ["git", "--git-dir=" + path, "fetch", "--quiet", self.repo.gitpath]
  48        child = subprocess.Popen(args)
  49        if child.wait() != 0:
  50            raise CalledProcessError
  51        args = ["git", "--git-dir=" + path, "update-ref", "refs/heads/master", "FETCH_HEAD"]
  53        child = subprocess.Popen(args)
  54        if child.wait() != 0:
  55            raise CalledProcessError
  56    def push(self, base):
  58        """Pushes from the non-local repo to base.
  59        """
  60        path = os.path.join(self.repo.get_base_path(base), '.git')
  62        if not os.path.exists(path):
  64            die("could not find repo at %s", path)
  65        args = ["git", "--git-dir=" + path, "push", "--quiet", self.repo.gitpath]
  67        child = subprocess.Popen(args)
  68        if child.wait() != 0:
  69            raise CalledProcessError