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