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