Show More
@@ -1,339 +1,339 | |||
|
1 | 1 | # Minimal support for git commands on an hg repository |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 Chris Mason <mason@suse.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | import time, sys, signal, os |
|
9 | 9 | from mercurial import hg, mdiff, fancyopts, commands, ui, util |
|
10 | 10 | |
|
11 | 11 | def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always, |
|
12 | 12 | changes=None, text=False): |
|
13 | 13 | def date(c): |
|
14 | 14 | return time.asctime(time.gmtime(c[2][0])) |
|
15 | 15 | |
|
16 | 16 | if not changes: |
|
17 | 17 | changes = repo.changes(node1, node2, files, match=match) |
|
18 | 18 | modified, added, removed, deleted, unknown = changes |
|
19 | 19 | if files: |
|
20 | 20 | modified, added, removed = map(lambda x: filterfiles(files, x), |
|
21 | 21 | (modified, added, removed)) |
|
22 | 22 | |
|
23 | 23 | if not modified and not added and not removed: |
|
24 | 24 | return |
|
25 | 25 | |
|
26 | 26 | if node2: |
|
27 | 27 | change = repo.changelog.read(node2) |
|
28 | 28 | mmap2 = repo.manifest.read(change[0]) |
|
29 | 29 | date2 = date(change) |
|
30 | 30 | def read(f): |
|
31 | 31 | return repo.file(f).read(mmap2[f]) |
|
32 | 32 | else: |
|
33 | 33 | date2 = time.asctime() |
|
34 | 34 | if not node1: |
|
35 | 35 | node1 = repo.dirstate.parents()[0] |
|
36 | 36 | def read(f): |
|
37 | 37 | return repo.wfile(f).read() |
|
38 | 38 | |
|
39 | 39 | change = repo.changelog.read(node1) |
|
40 | 40 | mmap = repo.manifest.read(change[0]) |
|
41 | 41 | date1 = date(change) |
|
42 | 42 | |
|
43 | 43 | for f in modified: |
|
44 | 44 | to = None |
|
45 | 45 | if f in mmap: |
|
46 | 46 | to = repo.file(f).read(mmap[f]) |
|
47 | 47 | tn = read(f) |
|
48 | 48 | fp.write("diff --git a/%s b/%s\n" % (f, f)) |
|
49 | 49 | fp.write(mdiff.unidiff(to, date1, tn, date2, f, None, text=text)) |
|
50 | 50 | for f in added: |
|
51 | 51 | to = None |
|
52 | 52 | tn = read(f) |
|
53 | 53 | fp.write("diff --git /dev/null b/%s\n" % (f)) |
|
54 | 54 | fp.write(mdiff.unidiff(to, date1, tn, date2, f, None, text=text)) |
|
55 | 55 | for f in removed: |
|
56 | 56 | to = repo.file(f).read(mmap[f]) |
|
57 | 57 | tn = None |
|
58 | 58 | fp.write("diff --git a/%s /dev/null\n" % (f)) |
|
59 | 59 | fp.write(mdiff.unidiff(to, date1, tn, date2, f, None, text=text)) |
|
60 | 60 | |
|
61 | 61 | def difftree(ui, repo, node1=None, node2=None, **opts): |
|
62 | 62 | """diff trees from two commits""" |
|
63 | 63 | def __difftree(repo, node1, node2): |
|
64 | 64 | def date(c): |
|
65 | 65 | return time.asctime(time.gmtime(c[2][0])) |
|
66 | 66 | |
|
67 | 67 | if node2: |
|
68 | 68 | change = repo.changelog.read(node2) |
|
69 | 69 | mmap2 = repo.manifest.read(change[0]) |
|
70 | 70 | modified, added, removed, deleted, unknown = repo.changes(node1, node2) |
|
71 | 71 | def read(f): return repo.file(f).read(mmap2[f]) |
|
72 | 72 | date2 = date(change) |
|
73 | 73 | else: |
|
74 | 74 | date2 = time.asctime() |
|
75 | 75 | modified, added, removed, deleted, unknown = repo.changes(node1) |
|
76 | 76 | if not node1: |
|
77 | 77 | node1 = repo.dirstate.parents()[0] |
|
78 | 78 | def read(f): return file(os.path.join(repo.root, f)).read() |
|
79 | 79 | |
|
80 | 80 | change = repo.changelog.read(node1) |
|
81 | 81 | mmap = repo.manifest.read(change[0]) |
|
82 | 82 | date1 = date(change) |
|
83 | 83 | empty = "0" * 40; |
|
84 | 84 | |
|
85 | 85 | for f in modified: |
|
86 | 86 | # TODO get file permissions |
|
87 | 87 | print ":100664 100664 %s %s M\t%s\t%s" % (hg.hex(mmap[f]), |
|
88 | 88 | hg.hex(mmap2[f]), f, f) |
|
89 | 89 | for f in added: |
|
90 | 90 | print ":000000 100664 %s %s N\t%s\t%s" % (empty, hg.hex(mmap2[f]), f, f) |
|
91 | 91 | for f in removed: |
|
92 | 92 | print ":100664 000000 %s %s D\t%s\t%s" % (hg.hex(mmap[f]), empty, f, f) |
|
93 | 93 | ## |
|
94 | 94 | |
|
95 | 95 | while True: |
|
96 | 96 | if opts['stdin']: |
|
97 | 97 | try: |
|
98 | 98 | line = raw_input().split(' ') |
|
99 | 99 | node1 = line[0] |
|
100 | 100 | if len(line) > 1: |
|
101 | 101 | node2 = line[1] |
|
102 | 102 | else: |
|
103 | 103 | node2 = None |
|
104 | 104 | except EOFError: |
|
105 | 105 | break |
|
106 | 106 | node1 = repo.lookup(node1) |
|
107 | 107 | if node2: |
|
108 | 108 | node2 = repo.lookup(node2) |
|
109 | 109 | else: |
|
110 | 110 | node2 = node1 |
|
111 | 111 | node1 = repo.changelog.parents(node1)[0] |
|
112 | 112 | if opts['patch']: |
|
113 | 113 | if opts['pretty']: |
|
114 | 114 | catcommit(repo, node2, "") |
|
115 | 115 | dodiff(sys.stdout, ui, repo, node1, node2) |
|
116 | 116 | else: |
|
117 | 117 | __difftree(repo, node1, node2) |
|
118 | 118 | if not opts['stdin']: |
|
119 | 119 | break |
|
120 | 120 | |
|
121 | 121 | def catcommit(repo, n, prefix, changes=None): |
|
122 | 122 | nlprefix = '\n' + prefix; |
|
123 | 123 | (p1, p2) = repo.changelog.parents(n) |
|
124 | 124 | (h, h1, h2) = map(hg.hex, (n, p1, p2)) |
|
125 | 125 | (i1, i2) = map(repo.changelog.rev, (p1, p2)) |
|
126 | 126 | if not changes: |
|
127 | 127 | changes = repo.changelog.read(n) |
|
128 | 128 | print "tree %s" % (hg.hex(changes[0])) |
|
129 | 129 | if i1 != -1: print "parent %s" % (h1) |
|
130 | 130 | if i2 != -1: print "parent %s" % (h2) |
|
131 | 131 | date_ar = changes[2] |
|
132 | 132 | date = int(float(date_ar[0])) |
|
133 | 133 | lines = changes[4].splitlines() |
|
134 | 134 | if lines[-1].startswith('committer:'): |
|
135 | 135 | committer = lines[-1].split(': ')[1].rstrip() |
|
136 | 136 | else: |
|
137 |
committer = |
|
|
137 | committer = changes[1] | |
|
138 | 138 | |
|
139 | 139 | print "author %s %s %s" % (changes[1], date, date_ar[1]) |
|
140 | print "committer %s" % (committer) | |
|
140 | print "committer %s %s %s" % (committer, date, date_ar[1]) | |
|
141 | 141 | print "" |
|
142 | 142 | if prefix != "": |
|
143 | 143 | print "%s%s" % (prefix, changes[4].replace('\n', nlprefix).strip()) |
|
144 | 144 | else: |
|
145 | 145 | print changes[4] |
|
146 | 146 | if prefix: |
|
147 | 147 | sys.stdout.write('\0') |
|
148 | 148 | |
|
149 | 149 | def base(ui, repo, node1, node2): |
|
150 | 150 | """Output common ancestor information""" |
|
151 | 151 | node1 = repo.lookup(node1) |
|
152 | 152 | node2 = repo.lookup(node2) |
|
153 | 153 | n = repo.changelog.ancestor(node1, node2) |
|
154 | 154 | print hg.hex(n) |
|
155 | 155 | |
|
156 | 156 | def catfile(ui, repo, type=None, r=None, **opts): |
|
157 | 157 | """cat a specific revision""" |
|
158 | 158 | # in stdin mode, every line except the commit is prefixed with two |
|
159 | 159 | # spaces. This way the our caller can find the commit without magic |
|
160 | 160 | # strings |
|
161 | 161 | # |
|
162 | 162 | prefix = "" |
|
163 | 163 | if opts['stdin']: |
|
164 | 164 | try: |
|
165 | 165 | (type, r) = raw_input().split(' '); |
|
166 | 166 | prefix = " " |
|
167 | 167 | except EOFError: |
|
168 | 168 | return |
|
169 | 169 | |
|
170 | 170 | else: |
|
171 | 171 | if not type or not r: |
|
172 | 172 | ui.warn("cat-file: type or revision not supplied\n") |
|
173 | 173 | commands.help_(ui, 'cat-file') |
|
174 | 174 | |
|
175 | 175 | while r: |
|
176 | 176 | if type != "commit": |
|
177 | 177 | sys.stderr.write("aborting hg cat-file only understands commits\n") |
|
178 | 178 | sys.exit(1); |
|
179 | 179 | n = repo.lookup(r) |
|
180 | 180 | catcommit(repo, n, prefix) |
|
181 | 181 | if opts['stdin']: |
|
182 | 182 | try: |
|
183 | 183 | (type, r) = raw_input().split(' '); |
|
184 | 184 | except EOFError: |
|
185 | 185 | break |
|
186 | 186 | else: |
|
187 | 187 | break |
|
188 | 188 | |
|
189 | 189 | # git rev-tree is a confusing thing. You can supply a number of |
|
190 | 190 | # commit sha1s on the command line, and it walks the commit history |
|
191 | 191 | # telling you which commits are reachable from the supplied ones via |
|
192 | 192 | # a bitmask based on arg position. |
|
193 | 193 | # you can specify a commit to stop at by starting the sha1 with ^ |
|
194 | 194 | def revtree(args, repo, full="tree", maxnr=0, parents=False): |
|
195 | 195 | def chlogwalk(): |
|
196 | 196 | ch = repo.changelog |
|
197 | 197 | count = ch.count() |
|
198 | 198 | i = count |
|
199 | 199 | l = [0] * 100 |
|
200 | 200 | chunk = 100 |
|
201 | 201 | while True: |
|
202 | 202 | if chunk > i: |
|
203 | 203 | chunk = i |
|
204 | 204 | i = 0 |
|
205 | 205 | else: |
|
206 | 206 | i -= chunk |
|
207 | 207 | |
|
208 | 208 | for x in xrange(0, chunk): |
|
209 | 209 | if i + x >= count: |
|
210 | 210 | l[chunk - x:] = [0] * (chunk - x) |
|
211 | 211 | break |
|
212 | 212 | if full != None: |
|
213 | 213 | l[x] = ch.read(ch.node(i + x)) |
|
214 | 214 | else: |
|
215 | 215 | l[x] = 1 |
|
216 | 216 | for x in xrange(chunk-1, -1, -1): |
|
217 | 217 | if l[x] != 0: |
|
218 | 218 | yield (i + x, full != None and l[x] or None) |
|
219 | 219 | if i == 0: |
|
220 | 220 | break |
|
221 | 221 | |
|
222 | 222 | # calculate and return the reachability bitmask for sha |
|
223 | 223 | def is_reachable(ar, reachable, sha): |
|
224 | 224 | if len(ar) == 0: |
|
225 | 225 | return 1 |
|
226 | 226 | mask = 0 |
|
227 | 227 | for i in range(len(ar)): |
|
228 | 228 | if sha in reachable[i]: |
|
229 | 229 | mask |= 1 << i |
|
230 | 230 | |
|
231 | 231 | return mask |
|
232 | 232 | |
|
233 | 233 | reachable = [] |
|
234 | 234 | stop_sha1 = [] |
|
235 | 235 | want_sha1 = [] |
|
236 | 236 | count = 0 |
|
237 | 237 | |
|
238 | 238 | # figure out which commits they are asking for and which ones they |
|
239 | 239 | # want us to stop on |
|
240 | 240 | for i in range(len(args)): |
|
241 | 241 | if args[i].startswith('^'): |
|
242 | 242 | s = repo.lookup(args[i][1:]) |
|
243 | 243 | stop_sha1.append(s) |
|
244 | 244 | want_sha1.append(s) |
|
245 | 245 | elif args[i] != 'HEAD': |
|
246 | 246 | want_sha1.append(repo.lookup(args[i])) |
|
247 | 247 | |
|
248 | 248 | # calculate the graph for the supplied commits |
|
249 | 249 | for i in range(len(want_sha1)): |
|
250 | 250 | reachable.append({}); |
|
251 | 251 | n = want_sha1[i]; |
|
252 | 252 | visit = [n]; |
|
253 | 253 | reachable[i][n] = 1 |
|
254 | 254 | while visit: |
|
255 | 255 | n = visit.pop(0) |
|
256 | 256 | if n in stop_sha1: |
|
257 | 257 | continue |
|
258 | 258 | for p in repo.changelog.parents(n): |
|
259 | 259 | if p not in reachable[i]: |
|
260 | 260 | reachable[i][p] = 1 |
|
261 | 261 | visit.append(p) |
|
262 | 262 | if p in stop_sha1: |
|
263 | 263 | continue |
|
264 | 264 | |
|
265 | 265 | # walk the repository looking for commits that are in our |
|
266 | 266 | # reachability graph |
|
267 | 267 | #for i in range(repo.changelog.count()-1, -1, -1): |
|
268 | 268 | for i, changes in chlogwalk(): |
|
269 | 269 | n = repo.changelog.node(i) |
|
270 | 270 | mask = is_reachable(want_sha1, reachable, n) |
|
271 | 271 | if mask: |
|
272 | 272 | parentstr = "" |
|
273 | 273 | if parents: |
|
274 | 274 | pp = repo.changelog.parents(n) |
|
275 | 275 | if pp[0] != hg.nullid: |
|
276 | 276 | parentstr += " " + hg.hex(pp[0]) |
|
277 | 277 | if pp[1] != hg.nullid: |
|
278 | 278 | parentstr += " " + hg.hex(pp[1]) |
|
279 | 279 | if not full: |
|
280 | 280 | print hg.hex(n) + parentstr |
|
281 | 281 | elif full is "commit": |
|
282 | 282 | print hg.hex(n) + parentstr |
|
283 | 283 | catcommit(repo, n, ' ', changes) |
|
284 | 284 | else: |
|
285 | 285 | (p1, p2) = repo.changelog.parents(n) |
|
286 | 286 | (h, h1, h2) = map(hg.hex, (n, p1, p2)) |
|
287 | 287 | (i1, i2) = map(repo.changelog.rev, (p1, p2)) |
|
288 | 288 | |
|
289 | 289 | date = changes[2][0] |
|
290 | 290 | print "%s %s:%s" % (date, h, mask), |
|
291 | 291 | mask = is_reachable(want_sha1, reachable, p1) |
|
292 | 292 | if i1 != -1 and mask > 0: |
|
293 | 293 | print "%s:%s " % (h1, mask), |
|
294 | 294 | mask = is_reachable(want_sha1, reachable, p2) |
|
295 | 295 | if i2 != -1 and mask > 0: |
|
296 | 296 | print "%s:%s " % (h2, mask), |
|
297 | 297 | print "" |
|
298 | 298 | if maxnr and count >= maxnr: |
|
299 | 299 | break |
|
300 | 300 | count += 1 |
|
301 | 301 | |
|
302 | 302 | # git rev-list tries to order things by date, and has the ability to stop |
|
303 | 303 | # at a given commit without walking the whole repo. TODO add the stop |
|
304 | 304 | # parameter |
|
305 | 305 | def revlist(ui, repo, *revs, **opts): |
|
306 | 306 | """print revisions""" |
|
307 | 307 | if opts['header']: |
|
308 | 308 | full = "commit" |
|
309 | 309 | else: |
|
310 | 310 | full = None |
|
311 | 311 | copy = [x for x in revs] |
|
312 | 312 | revtree(copy, repo, full, opts['max_count'], opts['parents']) |
|
313 | 313 | |
|
314 | 314 | def view(ui, repo, *etc): |
|
315 | 315 | "start interactive history viewer" |
|
316 | 316 | os.chdir(repo.root) |
|
317 | 317 | os.system(ui.config("hgk", "path", "hgk") + " " + " ".join(etc)) |
|
318 | 318 | |
|
319 | 319 | cmdtable = { |
|
320 | 320 | "view": (view, [], 'hg view'), |
|
321 | 321 | "debug-diff-tree": (difftree, [('p', 'patch', None, 'generate patch'), |
|
322 | 322 | ('r', 'recursive', None, 'recursive'), |
|
323 | 323 | ('P', 'pretty', None, 'pretty'), |
|
324 | 324 | ('s', 'stdin', None, 'stdin'), |
|
325 | 325 | ('C', 'copy', None, 'detect copies'), |
|
326 | 326 | ('S', 'search', "", 'search')], |
|
327 | 327 | "hg git-diff-tree [options] node1 node2"), |
|
328 | 328 | "debug-cat-file": (catfile, [('s', 'stdin', None, 'stdin')], |
|
329 | 329 | "hg debug-cat-file [options] type file"), |
|
330 | 330 | "debug-merge-base": (base, [], "hg debug-merge-base node node"), |
|
331 | 331 | "debug-rev-list": (revlist, [('H', 'header', None, 'header'), |
|
332 | 332 | ('t', 'topo-order', None, 'topo-order'), |
|
333 | 333 | ('p', 'parents', None, 'parents'), |
|
334 | 334 | ('n', 'max-count', 0, 'max-count')], |
|
335 | 335 | "hg debug-rev-list [options] revs"), |
|
336 | 336 | } |
|
337 | 337 | |
|
338 | 338 | def reposetup(ui, repo): |
|
339 | 339 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now