4 def sanitize(rev, sep='\t'):
5 """Converts a for-each-ref line to a name/value pair.
8 splitrev = rev.split(sep)
9 branchval = splitrev[0]
10 branchname = splitrev[1].strip()
11 if branchname.startswith("refs/heads/"):
12 branchname = branchname[11:]
14 return branchname, branchval
17 """Checks whether the specified value is a remote url.
20 prefixes = ["http", "file", "git"]
22 for prefix in prefixes:
23 if url.startswith(prefix):
27 class GitRepo(object):
28 """Repo object representing a repo.
31 def __init__(self, path):
32 """Initializes a new repo at the given path.
38 self.local = not is_remote(self.path)
40 if(self.path.endswith('.git')):
41 self.gitpath = self.path
43 self.gitpath = os.path.join(self.path, '.git')
45 if self.local and not os.path.exists(self.gitpath):
46 os.makedirs(self.gitpath)
49 """Fetches all revs from the remote.
52 args = ["git", "ls-remote", self.gitpath]
54 ofile = open(path, "w")
56 child = subprocess.Popen(args, stdout=ofile)
58 raise CalledProcessError
59 output = open(path).readlines()
60 self.revmap = dict(sanitize(i) for i in output)
61 if "HEAD" in self.revmap:
62 del self.revmap["HEAD"]
63 self.revs = self.revmap.keys()
67 """Determines the head of a local repo.
73 path = os.path.join(self.gitpath, "HEAD")
74 head = open(path).readline()
75 self.head, _ = sanitize(head, ' ')