Show More
@@ -1,142 +1,142 | |||
|
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 |
|
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 os.popen(s) |
|
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 os.popen('GIT_DIR=%s %s' % (self.path, s)) |
|
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("couldn't open GIT repo %s" % path) |
|
34 | 34 | self.path = path |
|
35 | 35 | |
|
36 | 36 | def getheads(self): |
|
37 | 37 | if not self.rev: |
|
38 | 38 | return self.gitcmd('git-rev-parse --branches').read().splitlines() |
|
39 | 39 | else: |
|
40 | 40 | fh = self.gitcmd("git-rev-parse --verify %s" % self.rev) |
|
41 | 41 | return [fh.read()[:-1]] |
|
42 | 42 | |
|
43 | 43 | def catfile(self, rev, type): |
|
44 | 44 | if rev == "0" * 40: raise IOError() |
|
45 | 45 | fh = self.gitcmd("git-cat-file %s %s 2>%s" % (type, rev, |
|
46 | 46 | util.nulldev)) |
|
47 | 47 | return fh.read() |
|
48 | 48 | |
|
49 | 49 | def getfile(self, name, rev): |
|
50 | 50 | return self.catfile(rev, "blob") |
|
51 | 51 | |
|
52 | 52 | def getmode(self, name, rev): |
|
53 | 53 | return self.modecache[(name, rev)] |
|
54 | 54 | |
|
55 | 55 | def getchanges(self, version): |
|
56 | 56 | self.modecache = {} |
|
57 | 57 | fh = self.gitcmd("git-diff-tree --root -m -r %s" % version) |
|
58 | 58 | changes = [] |
|
59 | 59 | seen = {} |
|
60 | 60 | for l in fh: |
|
61 | 61 | if "\t" not in l: |
|
62 | 62 | continue |
|
63 | 63 | m, f = l[:-1].split("\t") |
|
64 | 64 | if f in seen: |
|
65 | 65 | continue |
|
66 | 66 | seen[f] = 1 |
|
67 | 67 | m = m.split() |
|
68 | 68 | h = m[3] |
|
69 | 69 | p = (m[1] == "100755") |
|
70 | 70 | s = (m[1] == "120000") |
|
71 | 71 | self.modecache[(f, h)] = (p and "x") or (s and "l") or "" |
|
72 | 72 | changes.append((f, h)) |
|
73 | 73 | return (changes, {}) |
|
74 | 74 | |
|
75 | 75 | def getcommit(self, version): |
|
76 | 76 | c = self.catfile(version, "commit") # read the commit hash |
|
77 | 77 | end = c.find("\n\n") |
|
78 | 78 | message = c[end+2:] |
|
79 | 79 | message = self.recode(message) |
|
80 | 80 | l = c[:end].splitlines() |
|
81 | 81 | manifest = l[0].split()[1] |
|
82 | 82 | parents = [] |
|
83 | 83 | for e in l[1:]: |
|
84 | 84 | n, v = e.split(" ", 1) |
|
85 | 85 | if n == "author": |
|
86 | 86 | p = v.split() |
|
87 | 87 | tm, tz = p[-2:] |
|
88 | 88 | author = " ".join(p[:-2]) |
|
89 | 89 | if author[0] == "<": author = author[1:-1] |
|
90 | 90 | author = self.recode(author) |
|
91 | 91 | if n == "committer": |
|
92 | 92 | p = v.split() |
|
93 | 93 | tm, tz = p[-2:] |
|
94 | 94 | committer = " ".join(p[:-2]) |
|
95 | 95 | if committer[0] == "<": committer = committer[1:-1] |
|
96 | 96 | committer = self.recode(committer) |
|
97 | 97 | message += "\ncommitter: %s\n" % committer |
|
98 | 98 | if n == "parent": parents.append(v) |
|
99 | 99 | |
|
100 | 100 | tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] |
|
101 | 101 | tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) |
|
102 | 102 | date = tm + " " + str(tz) |
|
103 | 103 | author = author or "unknown" |
|
104 | 104 | |
|
105 | 105 | c = commit(parents=parents, date=date, author=author, desc=message, |
|
106 | 106 | rev=version) |
|
107 | 107 | return c |
|
108 | 108 | |
|
109 | 109 | def gettags(self): |
|
110 | 110 | tags = {} |
|
111 | 111 | fh = self.gitcmd('git-ls-remote --tags "%s" 2>%s' % (self.path, |
|
112 | 112 | util.nulldev)) |
|
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 |
fh = self.gitcmd( |
|
|
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