FIXME: hotpatch for compatibility with latest hg
[git] / git-remote-testgit.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 sys.path.insert(0, os.getenv("GITPYTHONLIB","."))
6
7 from git_remote_helpers.helper import RemoteHelper
8 from git_remote_helpers.util import check_output, debug
9 from git_remote_helpers.git.repo import GitRepo
10 from git_remote_helpers.git.exporter import GitExporter
11 from git_remote_helpers.git.importer import GitImporter
12 from git_remote_helpers.git.non_local import NonLocalGit
13
14
15 class TestgitRemoteHelper(RemoteHelper):
16     def get_repo(self, alias, url):
17         """Returns a git repository object initialized for usage.
18         """
19
20         repo = GitRepo(url)
21         repo.get_revs()
22         repo.get_head()
23
24         prefix = 'refs/testgit/%s/' % alias
25         debug("prefix: '%s'", prefix)
26
27         repo.marksfile = 'testgit.marks'
28         repo.prefix = prefix
29
30         self.setup_repo(repo, alias)
31
32         repo.exporter = GitExporter(repo)
33         repo.importer = GitImporter(repo)
34         repo.non_local = NonLocalGit(repo)
35
36         return repo
37
38     def local_repo(self, repo, path):
39         """Returns a git repository object initalized for usage.
40         """
41
42         local = GitRepo(path)
43
44         self.setup_local_repo(local, repo)
45
46         local.exporter = GitExporter(local)
47         local.importer = GitImporter(local)
48
49         return local
50
51     def do_list(self, repo, args):
52         """Lists all known references.
53
54         Bug: This will always set the remote head to master for non-local
55         repositories, since we have no way of determining what the remote
56         head is at clone time.
57         """
58
59         for ref in repo.revs:
60             debug("? refs/heads/%s", ref)
61             print "? refs/heads/%s" % ref
62
63         if repo.head:
64             debug("@refs/heads/%s HEAD" % repo.head)
65             print "@refs/heads/%s HEAD" % repo.head
66         else:
67             debug("@refs/heads/master HEAD")
68             print "@refs/heads/master HEAD"
69
70         print # end list
71
72     def sanitize(self, value):
73         """Cleans up the url.
74         """
75
76         if value.startswith('testgit::'):
77             value = value[9:]
78
79         return value
80
81     def get_refs(self, repo, gitdir):
82         """Returns a dictionary with refs.
83         """
84         args = ["git", "--git-dir=" + gitdir, "for-each-ref", "refs/heads"]
85         lines = check_output(args).strip().split('\n')
86         refs = {}
87         for line in lines:
88             value, name = line.split(' ')
89             name = name.strip('commit\t')
90             refs[name] = value
91         return refs
92
93
94 if __name__ == '__main__':
95     sys.exit(TestgitRemoteHelper().main(sys.argv))