aboutsummaryrefslogtreecommitdiff
path: root/git_remote_helpers/git/repo.py
diff options
context:
space:
mode:
authorSverre Rabbelier <srabbelier@gmail.com>2010-03-29 11:48:28 -0500
committerJunio C Hamano <gitster@pobox.com>2010-03-31 21:40:16 -0700
commit7aeaa2fc0abbf439534769e15b3a59a5814cc3d1 (patch)
treea3f957d0578ceefba288bbc0ddbec9cdf66f173a /git_remote_helpers/git/repo.py
parent73b49a759216bf33cc366eeac113ce00d8c49cc6 (diff)
downloadgit-7aeaa2fc0abbf439534769e15b3a59a5814cc3d1.tar.gz
git-7aeaa2fc0abbf439534769e15b3a59a5814cc3d1.tar.xz
remote-helpers: add testgit helper
Currently the remote helper infrastructure is only used by the curl helper, which does not give a good impression of how remote helpers can be used to interact with foreign repositories. Since implementing such a helper is non-trivial it would be good to have at least one easy-to-follow example demonstrating how to implement a helper that interacts with a foreign vcs using fast-import/fast-export. The testgit helper can be used to interact with remote git repositories by prefixing the url with "testgit::". Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git_remote_helpers/git/repo.py')
-rw-r--r--git_remote_helpers/git/repo.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/git_remote_helpers/git/repo.py b/git_remote_helpers/git/repo.py
new file mode 100644
index 000000000..82d5f78c7
--- /dev/null
+++ b/git_remote_helpers/git/repo.py
@@ -0,0 +1,70 @@
+import os
+import subprocess
+
+def sanitize(rev, sep='\t'):
+ """Converts a for-each-ref line to a name/value pair.
+ """
+
+ splitrev = rev.split(sep)
+ branchval = splitrev[0]
+ branchname = splitrev[1].strip()
+ if branchname.startswith("refs/heads/"):
+ branchname = branchname[11:]
+
+ return branchname, branchval
+
+def is_remote(url):
+ """Checks whether the specified value is a remote url.
+ """
+
+ prefixes = ["http", "file", "git"]
+
+ return any(url.startswith(i) for i in prefixes)
+
+class GitRepo(object):
+ """Repo object representing a repo.
+ """
+
+ def __init__(self, path):
+ """Initializes a new repo at the given path.
+ """
+
+ self.path = path
+ self.head = None
+ self.revmap = {}
+ self.local = not is_remote(self.path)
+
+ if(self.path.endswith('.git')):
+ self.gitpath = self.path
+ else:
+ self.gitpath = os.path.join(self.path, '.git')
+
+ if self.local and not os.path.exists(self.gitpath):
+ os.makedirs(self.gitpath)
+
+ def get_revs(self):
+ """Fetches all revs from the remote.
+ """
+
+ args = ["git", "ls-remote", self.gitpath]
+ path = ".cached_revs"
+ ofile = open(path, "w")
+
+ subprocess.check_call(args, stdout=ofile)
+ output = open(path).readlines()
+ self.revmap = dict(sanitize(i) for i in output)
+ if "HEAD" in self.revmap:
+ del self.revmap["HEAD"]
+ self.revs = self.revmap.keys()
+ ofile.close()
+
+ def get_head(self):
+ """Determines the head of a local repo.
+ """
+
+ if not self.local:
+ return
+
+ path = os.path.join(self.gitpath, "HEAD")
+ head = open(path).readline()
+ self.head, _ = sanitize(head, ' ')