Show More
@@ -1,243 +1,258 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # |
|
3 | 3 | # Minimal support for git commands on an hg repository |
|
4 | 4 | # |
|
5 | 5 | # Copyright 2005 Chris Mason <mason@suse.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms |
|
8 | 8 | # of the GNU General Public License, incorporated herein by reference. |
|
9 | 9 | |
|
10 | 10 | import time, sys, signal |
|
11 | 11 | from mercurial import hg, mdiff, fancyopts, commands, ui |
|
12 | 12 | |
|
13 | 13 | def difftree(args, repo): |
|
14 | 14 | def __difftree(repo, files = None, node1 = None, node2 = None): |
|
15 | 15 | def date(c): |
|
16 | 16 | return time.asctime(time.gmtime(float(c[2].split(' ')[0]))) |
|
17 | 17 | |
|
18 | 18 | if node2: |
|
19 | 19 | change = repo.changelog.read(node2) |
|
20 | 20 | mmap2 = repo.manifest.read(change[0]) |
|
21 | 21 | (c, a, d) = repo.diffrevs(node1, node2) |
|
22 | 22 | def read(f): return repo.file(f).read(mmap2[f]) |
|
23 | 23 | date2 = date(change) |
|
24 | 24 | else: |
|
25 | 25 | date2 = time.asctime() |
|
26 | 26 | (c, a, d, u) = repo.diffdir(repo.root, node1) |
|
27 | 27 | if not node1: |
|
28 | 28 | node1 = repo.dirstate.parents()[0] |
|
29 | 29 | def read(f): return file(os.path.join(repo.root, f)).read() |
|
30 | 30 | |
|
31 | 31 | change = repo.changelog.read(node1) |
|
32 | 32 | mmap = repo.manifest.read(change[0]) |
|
33 | 33 | date1 = date(change) |
|
34 | 34 | empty = "0" * 40; |
|
35 | 35 | |
|
36 | 36 | if files: |
|
37 | 37 | c, a, d = map(lambda x: filterfiles(files, x), (c, a, d)) |
|
38 | 38 | |
|
39 | 39 | for f in c: |
|
40 | 40 | # TODO get file permissions |
|
41 | 41 | print ":100664 100664 %s %s %s %s" % (hg.hex(mmap[f]), |
|
42 | 42 | hg.hex(mmap2[f]), f, f) |
|
43 | 43 | for f in a: |
|
44 | 44 | print ":000000 100664 %s %s %s %s" % (empty, hg.hex(mmap2[f]), f, f) |
|
45 | 45 | for f in d: |
|
46 | 46 | print ":100664 000000 %s %s %s %s" % (hg.hex(mmap[f]), empty, f, f) |
|
47 | 47 | ## |
|
48 | 48 | |
|
49 | 49 | revs = [] |
|
50 | 50 | if args: |
|
51 | 51 | doptions = {} |
|
52 | 52 | opts = [('p', 'patch', None, 'patch'), |
|
53 | 53 | ('r', 'recursive', None, 'recursive')] |
|
54 | 54 | args = fancyopts.fancyopts(args, opts, doptions, |
|
55 | 55 | 'hg diff-tree [options] sha1 sha1') |
|
56 | 56 | |
|
57 | 57 | if len(args) < 2: |
|
58 | 58 | help() |
|
59 | 59 | sys.exit(1) |
|
60 | 60 | revs.append(repo.lookup(args[0])) |
|
61 | 61 | revs.append(repo.lookup(args[1])) |
|
62 | 62 | args = args[2:] |
|
63 | 63 | if doptions['patch']: |
|
64 | 64 | commands.dodiff(repo, args, *revs) |
|
65 | 65 | else: |
|
66 | 66 | __difftree(repo, args, *revs) |
|
67 | 67 | |
|
68 | 68 | def catcommit(repo, n, prefix): |
|
69 | 69 | nlprefix = '\n' + prefix; |
|
70 | 70 | changes = repo.changelog.read(n) |
|
71 | 71 | (p1, p2) = repo.changelog.parents(n) |
|
72 | 72 | (h, h1, h2) = map(hg.hex, (n, p1, p2)) |
|
73 | 73 | (i1, i2) = map(repo.changelog.rev, (p1, p2)) |
|
74 | 74 | print "tree %s" % (h) |
|
75 | 75 | if i1 != -1: print "%sparent %s" % (prefix, h1) |
|
76 | 76 | if i2 != -1: print "%sparent %s" % (prefix, h2) |
|
77 | 77 | date_ar = changes[2].split(' ') |
|
78 | 78 | date = int(float(date_ar[0])) |
|
79 | 79 | print "%sauthor <%s> %s %s" % (prefix, changes[1], date, date_ar[1]) |
|
80 | 80 | print "%scommitter <%s> %s %s" % (prefix, changes[1], date, date_ar[1]) |
|
81 | 81 | print prefix |
|
82 | 82 | if prefix != "": |
|
83 | 83 | print "%s%s" % (prefix, changes[4].replace('\n', nlprefix).strip()) |
|
84 | 84 | else: |
|
85 | 85 | print changes[4] |
|
86 | 86 | |
|
87 | 87 | def catfile(args, ui, repo): |
|
88 | 88 | doptions = {} |
|
89 | 89 | opts = [('s', 'stdin', None, 'stdin')] |
|
90 | 90 | args = fancyopts.fancyopts(args, opts, doptions, |
|
91 | 91 | 'hg cat-file type sha1') |
|
92 | 92 | |
|
93 | 93 | # in stdin mode, every line except the commit is prefixed with two |
|
94 | 94 | # spaces. This way the our caller can find the commit without magic |
|
95 | 95 | # strings |
|
96 | 96 | # |
|
97 | 97 | prefix = "" |
|
98 | 98 | if doptions['stdin']: |
|
99 | 99 | try: |
|
100 | 100 | (type, r) = raw_input().split(' '); |
|
101 | 101 | prefix = " " |
|
102 | 102 | except EOFError: |
|
103 | 103 | return |
|
104 | 104 | |
|
105 | 105 | else: |
|
106 | 106 | if len(args) < 2: |
|
107 | 107 | help() |
|
108 | 108 | sys.exit(1) |
|
109 | 109 | type = args[0] |
|
110 | 110 | r = args[1] |
|
111 | 111 | |
|
112 | 112 | while r: |
|
113 | 113 | if type != "commit": |
|
114 | 114 | sys.stderr.write("aborting hg cat-file only understands commits\n") |
|
115 | 115 | sys.exit(1); |
|
116 | 116 | n = repo.changelog.lookup(r) |
|
117 | 117 | catcommit(repo, n, prefix) |
|
118 | 118 | if doptions['stdin']: |
|
119 | 119 | try: |
|
120 | 120 | (type, r) = raw_input().split(' '); |
|
121 | 121 | except EOFError: |
|
122 | 122 | break |
|
123 | 123 | else: |
|
124 | 124 | break |
|
125 | 125 | |
|
126 | 126 | # git rev-tree is a confusing thing. You can supply a number of |
|
127 | 127 | # commit sha1s on the command line, and it walks the commit history |
|
128 | 128 | # telling you which commits are reachable from the supplied ones via |
|
129 | 129 | # a bitmask based on arg position. |
|
130 | 130 | # you can specify a commit to stop at by starting the sha1 with ^ |
|
131 | def revtree(args, repo): | |
|
131 | def revtree(args, repo, full="tree", maxnr=0): | |
|
132 | 132 | # calculate and return the reachability bitmask for sha |
|
133 | 133 | def is_reachable(ar, reachable, sha): |
|
134 | 134 | if len(ar) == 0: |
|
135 | 135 | return 1 |
|
136 | 136 | mask = 0 |
|
137 | 137 | for i in range(len(ar)): |
|
138 | 138 | if sha in reachable[i]: |
|
139 | 139 | mask |= 1 << i |
|
140 | 140 | |
|
141 | 141 | return mask |
|
142 | 142 | |
|
143 | 143 | reachable = [] |
|
144 | 144 | stop_sha1 = [] |
|
145 | 145 | want_sha1 = [] |
|
146 | count = 0 | |
|
146 | 147 | |
|
147 | 148 | # figure out which commits they are asking for and which ones they |
|
148 | 149 | # want us to stop on |
|
149 | 150 | for i in range(len(args)): |
|
150 | 151 | if args[i].count('^'): |
|
151 | 152 | s = args[i].split('^')[1] |
|
152 | 153 | stop_sha1.append(repo.changelog.lookup(s)) |
|
153 | 154 | want_sha1.append(s) |
|
154 | 155 | elif args[i] != 'HEAD': |
|
155 | 156 | want_sha1.append(args[i]) |
|
157 | ||
|
156 | 158 | # calculate the graph for the supplied commits |
|
157 | 159 | for i in range(len(want_sha1)): |
|
158 | 160 | reachable.append({}); |
|
159 | 161 | n = repo.changelog.lookup(want_sha1[i]); |
|
160 | 162 | visit = [n]; |
|
161 | 163 | reachable[i][n] = 1 |
|
162 | 164 | while visit: |
|
163 | 165 | n = visit.pop(0) |
|
164 | 166 | if n in stop_sha1: |
|
165 | 167 | break |
|
166 | 168 | for p in repo.changelog.parents(n): |
|
167 | 169 | if p not in reachable[i]: |
|
168 | 170 | reachable[i][p] = 1 |
|
169 | 171 | visit.append(p) |
|
170 | 172 | if p in stop_sha1: |
|
171 | 173 | break |
|
174 | ||
|
172 | 175 | # walk the repository looking for commits that are in our |
|
173 | 176 | # reachability graph |
|
174 | for i in range(repo.changelog.count()): | |
|
177 | for i in range(repo.changelog.count()-1, -1, -1): | |
|
175 | 178 | n = repo.changelog.node(i) |
|
176 | 179 | mask = is_reachable(want_sha1, reachable, n) |
|
177 | 180 | if mask: |
|
181 | if not full: | |
|
182 | print hg.hex(n) | |
|
183 | elif full is "commit": | |
|
184 | print hg.hex(n) | |
|
185 | catcommit(repo, n, ' ') | |
|
186 | else: | |
|
178 | 187 | changes = repo.changelog.read(n) |
|
179 | 188 | (p1, p2) = repo.changelog.parents(n) |
|
180 | 189 | (h, h1, h2) = map(hg.hex, (n, p1, p2)) |
|
181 | 190 | (i1, i2) = map(repo.changelog.rev, (p1, p2)) |
|
182 | 191 | |
|
183 | 192 | date = changes[2].split(' ')[0] |
|
184 | 193 | print "%s %s:%s" % (date, h, mask), |
|
185 | 194 | mask = is_reachable(want_sha1, reachable, p1) |
|
186 | 195 | if i1 != -1 and mask > 0: |
|
187 | 196 | print "%s:%s " % (h1, mask), |
|
188 | 197 | mask = is_reachable(want_sha1, reachable, p2) |
|
189 | 198 | if i2 != -1 and mask > 0: |
|
190 | 199 | print "%s:%s " % (h2, mask), |
|
191 | 200 | print "" |
|
201 | if maxnr and count >= maxnr: | |
|
202 | break | |
|
203 | count += 1 | |
|
192 | 204 | |
|
193 | 205 | # git rev-list tries to order things by date, and has the ability to stop |
|
194 | 206 | # at a given commit without walking the whole repo. TODO add the stop |
|
195 | 207 | # parameter |
|
196 | 208 | def revlist(args, repo): |
|
197 | 209 | doptions = {} |
|
198 |
opts = [('c', 'commit', None, 'commit') |
|
|
210 | opts = [('c', 'commit', None, 'commit'), | |
|
211 | ('n', 'max-nr', 0, 'max-nr')] | |
|
199 | 212 | args = fancyopts.fancyopts(args, opts, doptions, |
|
200 | 213 | 'hg rev-list') |
|
201 | for i in range(repo.changelog.count()): | |
|
202 | n = repo.changelog.node(i) | |
|
203 | print hg.hex(n) | |
|
204 | 214 |
|
|
205 | catcommit(repo, n, ' ') | |
|
215 | full = "commit" | |
|
216 | else: | |
|
217 | full = None | |
|
218 | for i in range(1, len(args)): | |
|
219 | args[i] = '^' + args[i] | |
|
220 | revtree(args, repo, full, doptions['max-nr']) | |
|
206 | 221 | |
|
207 | 222 | def catchterm(*args): |
|
208 | 223 | raise SignalInterrupt |
|
209 | 224 | |
|
210 | 225 | def help(): |
|
211 | 226 | sys.stderr.write("commands:\n") |
|
212 | 227 | sys.stderr.write(" hgit cat-file [type] sha1\n") |
|
213 | 228 | sys.stderr.write(" hgit diff-tree [-p] [-r] sha1 sha1\n") |
|
214 | 229 | sys.stderr.write(" hgit rev-tree [sha1 ... [^stop sha1]]\n") |
|
215 | 230 | sys.stderr.write(" hgit rev-list [-c]\n") |
|
216 | 231 | |
|
217 | 232 | cmd = sys.argv[1] |
|
218 | 233 | args = sys.argv[2:] |
|
219 | 234 | u = ui.ui() |
|
220 | 235 | signal.signal(signal.SIGTERM, catchterm) |
|
221 | 236 | repo = hg.repository(ui = u) |
|
222 | 237 | |
|
223 | 238 | if cmd == "diff-tree": |
|
224 | 239 | difftree(args, repo) |
|
225 | 240 | |
|
226 | 241 | elif cmd == "cat-file": |
|
227 | 242 | catfile(args, ui, repo) |
|
228 | 243 | |
|
229 | 244 | elif cmd == "rev-tree": |
|
230 | 245 | revtree(args, repo) |
|
231 | 246 | |
|
232 | 247 | elif cmd == "rev-list": |
|
233 | 248 | revlist(args, repo) |
|
234 | 249 | |
|
235 | 250 | elif cmd == "help": |
|
236 | 251 | help() |
|
237 | 252 | |
|
238 | 253 | else: |
|
239 | 254 | if cmd: sys.stderr.write("unknown command\n\n") |
|
240 | 255 | help() |
|
241 | 256 | sys.exit(1) |
|
242 | 257 | |
|
243 | 258 | sys.exit(0) |
General Comments 0
You need to be logged in to leave comments.
Login now