1import os
   2import subprocess
   3from git_remote_helpers.util import check_call, 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        check_call(args)
  33        return path
  35    def update(self, base):
  37        """Updates checkout of the non-local repo in base.
  38        """
  39        path = os.path.join(self.repo.get_base_path(base), '.git')
  41        if not os.path.exists(path):
  43            die("could not find repo at %s", path)
  44        args = ["git", "--git-dir=" + path, "fetch", "--quiet", self.repo.gitpath]
  46        check_call(args)
  47        args = ["git", "--git-dir=" + path, "update-ref", "refs/heads/master", "FETCH_HEAD"]
  49        child = check_call(args)
  50    def push(self, base):
  52        """Pushes from the non-local repo to base.
  53        """
  54        path = os.path.join(self.repo.get_base_path(base), '.git')
  56        if not os.path.exists(path):
  58            die("could not find repo at %s", path)
  59        args = ["git", "--git-dir=" + path, "push", "--quiet", self.repo.gitpath, "--all"]
  61        child = check_call(args)