##// END OF EJS Templates
convert: return remote branches in git source...
Edouard Gomez -
r7243:a8e4e599 default
parent child Browse files
Show More
@@ -1,142 +1,142 b''
1 1 # git support for the convert extension
2 2
3 3 import os
4 4 from mercurial import util
5 5
6 6 from common import NoRepo, commit, converter_source, checktool
7 7
8 8 class convert_git(converter_source):
9 9 # Windows does not support GIT_DIR= construct while other systems
10 10 # cannot remove environment variable. Just assume none have
11 11 # both issues.
12 12 if hasattr(os, 'unsetenv'):
13 13 def gitcmd(self, s):
14 14 prevgitdir = os.environ.get('GIT_DIR')
15 15 os.environ['GIT_DIR'] = self.path
16 16 try:
17 17 return util.popen(s, 'rb')
18 18 finally:
19 19 if prevgitdir is None:
20 20 del os.environ['GIT_DIR']
21 21 else:
22 22 os.environ['GIT_DIR'] = prevgitdir
23 23 else:
24 24 def gitcmd(self, s):
25 25 return util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
26 26
27 27 def __init__(self, ui, path, rev=None):
28 28 super(convert_git, self).__init__(ui, path, rev=rev)
29 29
30 30 if os.path.isdir(path + "/.git"):
31 31 path += "/.git"
32 32 if not os.path.exists(path + "/objects"):
33 33 raise NoRepo("%s does not look like a Git repo" % path)
34 34
35 35 checktool('git', 'git')
36 36
37 37 self.path = path
38 38
39 39 def getheads(self):
40 40 if not self.rev:
41 return self.gitcmd('git rev-parse --branches').read().splitlines()
41 return self.gitcmd('git rev-parse --branches --remotes').read().splitlines()
42 42 else:
43 43 fh = self.gitcmd("git rev-parse --verify %s" % self.rev)
44 44 return [fh.read()[:-1]]
45 45
46 46 def catfile(self, rev, type):
47 47 if rev == "0" * 40: raise IOError()
48 48 fh = self.gitcmd("git cat-file %s %s" % (type, rev))
49 49 return fh.read()
50 50
51 51 def getfile(self, name, rev):
52 52 return self.catfile(rev, "blob")
53 53
54 54 def getmode(self, name, rev):
55 55 return self.modecache[(name, rev)]
56 56
57 57 def getchanges(self, version):
58 58 self.modecache = {}
59 59 fh = self.gitcmd("git diff-tree --root -m -r %s" % version)
60 60 changes = []
61 61 seen = {}
62 62 for l in fh:
63 63 if "\t" not in l:
64 64 continue
65 65 m, f = l[:-1].split("\t")
66 66 if f in seen:
67 67 continue
68 68 seen[f] = 1
69 69 m = m.split()
70 70 h = m[3]
71 71 p = (m[1] == "100755")
72 72 s = (m[1] == "120000")
73 73 self.modecache[(f, h)] = (p and "x") or (s and "l") or ""
74 74 changes.append((f, h))
75 75 return (changes, {})
76 76
77 77 def getcommit(self, version):
78 78 c = self.catfile(version, "commit") # read the commit hash
79 79 end = c.find("\n\n")
80 80 message = c[end+2:]
81 81 message = self.recode(message)
82 82 l = c[:end].splitlines()
83 83 manifest = l[0].split()[1]
84 84 parents = []
85 85 for e in l[1:]:
86 86 n, v = e.split(" ", 1)
87 87 if n == "author":
88 88 p = v.split()
89 89 tm, tz = p[-2:]
90 90 author = " ".join(p[:-2])
91 91 if author[0] == "<": author = author[1:-1]
92 92 author = self.recode(author)
93 93 if n == "committer":
94 94 p = v.split()
95 95 tm, tz = p[-2:]
96 96 committer = " ".join(p[:-2])
97 97 if committer[0] == "<": committer = committer[1:-1]
98 98 committer = self.recode(committer)
99 99 message += "\ncommitter: %s\n" % committer
100 100 if n == "parent": parents.append(v)
101 101
102 102 tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:]
103 103 tz = -int(tzs) * (int(tzh) * 3600 + int(tzm))
104 104 date = tm + " " + str(tz)
105 105
106 106 c = commit(parents=parents, date=date, author=author, desc=message,
107 107 rev=version)
108 108 return c
109 109
110 110 def gettags(self):
111 111 tags = {}
112 112 fh = self.gitcmd('git ls-remote --tags "%s"' % self.path)
113 113 prefix = 'refs/tags/'
114 114 for line in fh:
115 115 line = line.strip()
116 116 if not line.endswith("^{}"):
117 117 continue
118 118 node, tag = line.split(None, 1)
119 119 if not tag.startswith(prefix):
120 120 continue
121 121 tag = tag[len(prefix):-3]
122 122 tags[tag] = node
123 123
124 124 return tags
125 125
126 126 def getchangedfiles(self, version, i):
127 127 changes = []
128 128 if i is None:
129 129 fh = self.gitcmd("git diff-tree --root -m -r %s" % version)
130 130 for l in fh:
131 131 if "\t" not in l:
132 132 continue
133 133 m, f = l[:-1].split("\t")
134 134 changes.append(f)
135 135 fh.close()
136 136 else:
137 137 fh = self.gitcmd('git diff-tree --name-only --root -r %s "%s^%s" --'
138 138 % (version, version, i+1))
139 139 changes = [f.rstrip('\n') for f in fh]
140 140 fh.close()
141 141
142 142 return changes
General Comments 0
You need to be logged in to leave comments. Login now