comparison hgext/hgk.py @ 5878:d39af2eabb8c

transform a bunch of print statements to appropriate ui calls
author Matt Mackall <mpm@selenic.com>
date Fri, 18 Jan 2008 10:48:25 -0600
parents e0f86c1e3ae5
children e75aab656f46
comparison
equal deleted inserted replaced
5877:5692bed8230b 5878:d39af2eabb8c
43 # vdiff=vdiff 43 # vdiff=vdiff
44 # 44 #
45 # Revisions context menu will now display additional entries to fire 45 # Revisions context menu will now display additional entries to fire
46 # vdiff on hovered and selected revisions. 46 # vdiff on hovered and selected revisions.
47 47
48 import sys, os 48 import os
49 from mercurial import hg, fancyopts, commands, ui, util, patch, revlog 49 from mercurial import hg, fancyopts, commands, ui, util, patch, revlog
50 50
51 def difftree(ui, repo, node1=None, node2=None, *files, **opts): 51 def difftree(ui, repo, node1=None, node2=None, *files, **opts):
52 """diff trees from two commits""" 52 """diff trees from two commits"""
53 def __difftree(repo, node1, node2, files=[]): 53 def __difftree(repo, node1, node2, files=[]):
59 59
60 empty = hg.short(hg.nullid) 60 empty = hg.short(hg.nullid)
61 61
62 for f in modified: 62 for f in modified:
63 # TODO get file permissions 63 # TODO get file permissions
64 print ":100664 100664 %s %s M\t%s\t%s" % (hg.short(mmap[f]), 64 ui.write(":100664 100664 %s %s M\t%s\t%s\n" %
65 hg.short(mmap2[f]), 65 (hg.short(mmap[f]), hg.short(mmap2[f]), f, f))
66 f, f)
67 for f in added: 66 for f in added:
68 print ":000000 100664 %s %s N\t%s\t%s" % (empty, 67 ui.write(":000000 100664 %s %s N\t%s\t%s\n" %
69 hg.short(mmap2[f]), 68 (empty, hg.short(mmap2[f]), f, f))
70 f, f)
71 for f in removed: 69 for f in removed:
72 print ":100664 000000 %s %s D\t%s\t%s" % (hg.short(mmap[f]), 70 ui.write(":100664 000000 %s %s D\t%s\t%s\n" %
73 empty, 71 (hg.short(mmap[f]), empty, f, f))
74 f, f)
75 ## 72 ##
76 73
77 while True: 74 while True:
78 if opts['stdin']: 75 if opts['stdin']:
79 try: 76 try:
91 else: 88 else:
92 node2 = node1 89 node2 = node1
93 node1 = repo.changelog.parents(node1)[0] 90 node1 = repo.changelog.parents(node1)[0]
94 if opts['patch']: 91 if opts['patch']:
95 if opts['pretty']: 92 if opts['pretty']:
96 catcommit(repo, node2, "") 93 catcommit(ui, repo, node2, "")
97 patch.diff(repo, node1, node2, 94 patch.diff(repo, node1, node2,
98 files=files, 95 files=files,
99 opts=patch.diffopts(ui, {'git': True})) 96 opts=patch.diffopts(ui, {'git': True}))
100 else: 97 else:
101 __difftree(repo, node1, node2, files=files) 98 __difftree(repo, node1, node2, files=files)
102 if not opts['stdin']: 99 if not opts['stdin']:
103 break 100 break
104 101
105 def catcommit(repo, n, prefix, ctx=None): 102 def catcommit(ui, repo, n, prefix, ctx=None):
106 nlprefix = '\n' + prefix; 103 nlprefix = '\n' + prefix;
107 if ctx is None: 104 if ctx is None:
108 ctx = repo.changectx(n) 105 ctx = repo.changectx(n)
109 (p1, p2) = ctx.parents() 106 (p1, p2) = ctx.parents()
110 print "tree %s" % (hg.short(ctx.changeset()[0])) # use ctx.node() instead ?? 107 ui.write("tree %s\n" % hg.short(ctx.changeset()[0])) # use ctx.node() instead ??
111 if p1: print "parent %s" % (hg.short(p1.node())) 108 if p1: ui.write("parent %s\n" % hg.short(p1.node()))
112 if p2: print "parent %s" % (hg.short(p2.node())) 109 if p2: ui.write("parent %s\n" % hg.short(p2.node()))
113 date = ctx.date() 110 date = ctx.date()
114 description = ctx.description().replace("\0", "") 111 description = ctx.description().replace("\0", "")
115 lines = description.splitlines() 112 lines = description.splitlines()
116 if lines and lines[-1].startswith('committer:'): 113 if lines and lines[-1].startswith('committer:'):
117 committer = lines[-1].split(': ')[1].rstrip() 114 committer = lines[-1].split(': ')[1].rstrip()
118 else: 115 else:
119 committer = ctx.user() 116 committer = ctx.user()
120 117
121 print "author %s %s %s" % (ctx.user(), int(date[0]), date[1]) 118 ui.write("author %s %s %s\n" % (ctx.user(), int(date[0]), date[1]))
122 print "committer %s %s %s" % (committer, int(date[0]), date[1]) 119 ui.write("committer %s %s %s\n" % (committer, int(date[0]), date[1]))
123 print "revision %d" % ctx.rev() 120 ui.write("revision %d\n" % ctx.rev())
124 print "branch %s" % ctx.branch() 121 ui.write("branch %s\n\n" % ctx.branch())
125 print "" 122
126 if prefix != "": 123 if prefix != "":
127 print "%s%s" % (prefix, description.replace('\n', nlprefix).strip()) 124 ui.write("%s%s\n" % (prefix, description.replace('\n', nlprefix).strip()))
128 else: 125 else:
129 print description 126 ui.write(description + "\n")
130 if prefix: 127 if prefix:
131 sys.stdout.write('\0') 128 ui.write('\0')
132 129
133 def base(ui, repo, node1, node2): 130 def base(ui, repo, node1, node2):
134 """Output common ancestor information""" 131 """Output common ancestor information"""
135 node1 = repo.lookup(node1) 132 node1 = repo.lookup(node1)
136 node2 = repo.lookup(node2) 133 node2 = repo.lookup(node2)
137 n = repo.changelog.ancestor(node1, node2) 134 n = repo.changelog.ancestor(node1, node2)
138 print hg.short(n) 135 ui.write(hg.short(n) + "\n")
139 136
140 def catfile(ui, repo, type=None, r=None, **opts): 137 def catfile(ui, repo, type=None, r=None, **opts):
141 """cat a specific revision""" 138 """cat a specific revision"""
142 # in stdin mode, every line except the commit is prefixed with two 139 # in stdin mode, every line except the commit is prefixed with two
143 # spaces. This way the our caller can find the commit without magic 140 # spaces. This way the our caller can find the commit without magic
156 ui.warn("cat-file: type or revision not supplied\n") 153 ui.warn("cat-file: type or revision not supplied\n")
157 commands.help_(ui, 'cat-file') 154 commands.help_(ui, 'cat-file')
158 155
159 while r: 156 while r:
160 if type != "commit": 157 if type != "commit":
161 sys.stderr.write("aborting hg cat-file only understands commits\n") 158 ui.warn("aborting hg cat-file only understands commits\n")
162 sys.exit(1); 159 return 1;
163 n = repo.lookup(r) 160 n = repo.lookup(r)
164 catcommit(repo, n, prefix) 161 catcommit(ui, repo, n, prefix)
165 if opts['stdin']: 162 if opts['stdin']:
166 try: 163 try:
167 (type, r) = raw_input().split(' '); 164 (type, r) = raw_input().split(' ');
168 except EOFError: 165 except EOFError:
169 break 166 break
173 # git rev-tree is a confusing thing. You can supply a number of 170 # git rev-tree is a confusing thing. You can supply a number of
174 # commit sha1s on the command line, and it walks the commit history 171 # commit sha1s on the command line, and it walks the commit history
175 # telling you which commits are reachable from the supplied ones via 172 # telling you which commits are reachable from the supplied ones via
176 # a bitmask based on arg position. 173 # a bitmask based on arg position.
177 # you can specify a commit to stop at by starting the sha1 with ^ 174 # you can specify a commit to stop at by starting the sha1 with ^
178 def revtree(args, repo, full="tree", maxnr=0, parents=False): 175 def revtree(ui, args, repo, full="tree", maxnr=0, parents=False):
179 def chlogwalk(): 176 def chlogwalk():
180 count = repo.changelog.count() 177 count = repo.changelog.count()
181 i = count 178 i = count
182 l = [0] * 100 179 l = [0] * 100
183 chunk = 100 180 chunk = 100
258 if pp[0] != hg.nullid: 255 if pp[0] != hg.nullid:
259 parentstr += " " + hg.short(pp[0]) 256 parentstr += " " + hg.short(pp[0])
260 if pp[1] != hg.nullid: 257 if pp[1] != hg.nullid:
261 parentstr += " " + hg.short(pp[1]) 258 parentstr += " " + hg.short(pp[1])
262 if not full: 259 if not full:
263 print hg.short(n) + parentstr 260 ui.write("%s%s\n" % (hg.short(n), parentstr))
264 elif full == "commit": 261 elif full == "commit":
265 print hg.short(n) + parentstr 262 ui.write("%s%s\n" % (hg.short(n), parentstr))
266 catcommit(repo, n, ' ', ctx) 263 catcommit(ui, repo, n, ' ', ctx)
267 else: 264 else:
268 (p1, p2) = repo.changelog.parents(n) 265 (p1, p2) = repo.changelog.parents(n)
269 (h, h1, h2) = map(hg.short, (n, p1, p2)) 266 (h, h1, h2) = map(hg.short, (n, p1, p2))
270 (i1, i2) = map(repo.changelog.rev, (p1, p2)) 267 (i1, i2) = map(repo.changelog.rev, (p1, p2))
271 268
272 date = ctx.date()[0] 269 date = ctx.date()[0]
273 print "%s %s:%s" % (date, h, mask), 270 ui.write("%s %s:%s" % (date, h, mask))
274 mask = is_reachable(want_sha1, reachable, p1) 271 mask = is_reachable(want_sha1, reachable, p1)
275 if i1 != hg.nullrev and mask > 0: 272 if i1 != hg.nullrev and mask > 0:
276 print "%s:%s " % (h1, mask), 273 ui.write("%s:%s " % (h1, mask)),
277 mask = is_reachable(want_sha1, reachable, p2) 274 mask = is_reachable(want_sha1, reachable, p2)
278 if i2 != hg.nullrev and mask > 0: 275 if i2 != hg.nullrev and mask > 0:
279 print "%s:%s " % (h2, mask), 276 ui.write("%s:%s " % (h2, mask))
280 print "" 277 ui.write("\n")
281 if maxnr and count >= maxnr: 278 if maxnr and count >= maxnr:
282 break 279 break
283 count += 1 280 count += 1
284 281
285 def revparse(ui, repo, *revs, **opts): 282 def revparse(ui, repo, *revs, **opts):
303 if opts['header']: 300 if opts['header']:
304 full = "commit" 301 full = "commit"
305 else: 302 else:
306 full = None 303 full = None
307 copy = [x for x in revs] 304 copy = [x for x in revs]
308 revtree(copy, repo, full, opts['max_count'], opts['parents']) 305 revtree(ui, copy, repo, full, opts['max_count'], opts['parents'])
309 306
310 def config(ui, repo, **opts): 307 def config(ui, repo, **opts):
311 """print extension options""" 308 """print extension options"""
312 def writeopt(name, value): 309 def writeopt(name, value):
313 ui.write('k=%s\nv=%s\n' % (name, value)) 310 ui.write('k=%s\nv=%s\n' % (name, value))