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