i18n: git-shortlog basic messages
[git.git] / git_remote_helpers / git / repo.py
blob58e1cdb560fa0fe1a4745f971064e5e967408502
1 import os
2 import subprocess
4 def sanitize(rev, sep='\t'):
5 """Converts a for-each-ref line to a name/value pair.
6 """
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
16 def is_remote(url):
17 """Checks whether the specified value is a remote url.
18 """
20 prefixes = ["http", "file", "git"]
22 for prefix in prefixes:
23 if url.startswith(prefix):
24 return True
25 return False
27 class GitRepo(object):
28 """Repo object representing a repo.
29 """
31 def __init__(self, path):
32 """Initializes a new repo at the given path.
33 """
35 self.path = path
36 self.head = None
37 self.revmap = {}
38 self.local = not is_remote(self.path)
40 if(self.path.endswith('.git')):
41 self.gitpath = self.path
42 else:
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)
48 def get_revs(self):
49 """Fetches all revs from the remote.
50 """
52 args = ["git", "ls-remote", self.gitpath]
53 path = ".cached_revs"
54 ofile = open(path, "w")
56 child = subprocess.Popen(args, stdout=ofile)
57 if child.wait() != 0:
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()
64 ofile.close()
66 def get_head(self):
67 """Determines the head of a local repo.
68 """
70 if not self.local:
71 return
73 path = os.path.join(self.gitpath, "HEAD")
74 head = open(path).readline()
75 self.head, _ = sanitize(head, ' ')