-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
401 lines (328 loc) · 13.4 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
'''tweak the mq extension -- add some convenience commands and parameters
Note that many if not all of these changes should really be made to the
upstream project. I just haven't gotten around to it.
Commands added:
qshow - Display a single patch (similar to 'export')
qexport - Write all patches to an output directory, with minor renaming
qtouched - See what patches modify which files
Commands not related to mq:
lineage - Dump out the revision history leading up to a particular revision
The following mq commands are modified to add options that autocommit any
changes made to your patch queue to the queue repository
(a la hg commit --mq):
qrefresh
qnew
qrename
qdelete
qimport
qfinish
The expected usage is to add the -Q option to all relevant commands in your
~/.hgrc so that all changes are autocommitted:
[defaults]
qnew = -Q
qrefresh = -Q
qrename = -Q
qdelete = -Q
qimport = -Q
qfinish = -Q
'''
# TODO:
# [ ] Make 'show' dispatch to export?diff? for eg --stat
# - Hmm. No. export is all about generating a patch from adjacent revisions
# We already have a patch.
# [ ] Make qexport rediff with new options (eg -U 8)
# - For this, dispatching to export would make a lot of sense, but I guess
# I'd have to restrict it to applied patches then (b/c I don't really
# want to construct the revs to feed to export by applying patches...)
import os
from subprocess import call, check_call
import errno
from mercurial.node import hex, nullrev, nullid, short
from mercurial import commands, util, cmdutil, mdiff, error
from mercurial.patch import diffstatdata
from hgext import mq
import StringIO
def qshow(ui, repo, patch=None, **opts):
'''display a patch
If no patch is given, the top of the applied stack is shown.'''
q = repo.mq
if patch is None:
try:
patch = q.applied[-1].name
except:
ui.write("No patches in series\n");
return
# Try to interpret the patch as an index into the full stack
try:
idx = int(patch)
patch = q.series[idx]
except:
pass
# This should probably dispatch to export, so that all of its
# options could be used.
if opts['stat']:
check_call(["diffstat", "-p1", q.join(patch)])
else:
try:
ui.write(file(q.join(patch)).read())
except:
ui.write("Invalid patch name '%s'\n" % (patch,))
# TODO: I would really like to be able to export with a different
# amount of context than is stored in the patch. (I'd like to store
# less context so that the patches are more likely to apply, but
# export with more context to make reviews easier)
#
# Without that feature, I'm not sure this command is useful. Your
# patches are already stored in a directory.
def qexport(ui, repo, outdir, **opts):
'''Save all applied patches into a directory'''
try:
os.mkdir(outdir)
except OSError, inst:
if inst.errno != errno.EEXIST:
raise
suffix = ''
if opts['extension']:
suffix += '.' + opts['extension']
if opts['patch']:
suffix += '.patch'
q = repo.mq
n = 0
numlen = len(str(len(q.applied)))
for p in q.applied:
stem = p.name + suffix
if opts['numbered']:
stem = (('%0' + str(numlen) + 'd-') % n) + stem
n += 1
filename = os.path.join(outdir, stem)
# This should really call p.export(...) instead of writing the
# file directly...
open(filename, 'w').write(file(q.join(p.name)).read())
if ui.verbose:
ui.write("Wrote %s\n" % filename)
def lineage(ui, repo, rev='.', limit=None, stop=None, **opts):
'''Show ancestors of a revision'''
log = repo.changelog
n = 0
if ui.verbose:
ui.write("rev=%s limit=%s stop=%s\n" % (rev,limit,stop))
# TODO: If no limit is given, ask after 100 or so whether to continue
if limit is None or limit == '':
limit = 100
else:
limit = int(limit)
if not (stop is None or stop == ''):
stop = repo.lookup(stop)
if ui.verbose:
ui.write(" stop = %s\n" % hex(stop))
current = repo.lookup(rev)
while n < limit or limit == 0:
ctx = repo.changectx(current)
parents = [ p for p in log.parents(current) if p != nullid ]
header = short(current)
if len(parents) != 1:
header += " (%d parents)" % len(parents)
desc = ctx.description().strip()
desc = desc.replace("\n", " ")
ui.write("%s: %s\n" % (header, desc))
if len(parents) > 2:
break
if current == stop:
break
if len(parents) == 0:
break
current = parents[0]
n += 1
def touched(ui, repo, sourcefile=None, **opts):
'''Show what files are touched by what patches
If no file is given, print out a series of lines containing a
patch and a file changed by that patch, for all files changed by
all patches. This is mainly intended for easy grepping.
If a file is given, print out the list of patches that touch that file.'''
q = repo.mq
if opts['patch'] and opts['applied']:
raise util.Abort(_('Cannot use both -a and -p options'))
if opts['patch']:
patches = [ q.lookup(opts['patch']) ]
elif opts['applied']:
patches = [ p.name for p in q.applied ]
else:
patches = q.series
for patch in [ q.lookup(p) for p in patches ]:
lines = file(q.join(patch)).read().splitlines()
for filename, adds, removes, isbinary in diffstatdata(lines):
if sourcefile is None:
ui.write(patch + "\t" + filename + "\n")
elif sourcefile == filename:
ui.write(patch + "\n")
def mqcommit_info(ui, repo, opts):
mqcommit = opts.pop('mqcommit', None)
try:
auto = ui.configbool('mqext', 'qcommit', None)
if auto is None:
raise error.ConfigError()
except error.ConfigError:
auto = ui.config('mqext', 'qcommit', 'auto').lower()
if mqcommit is None and auto:
if auto == 'auto':
if repo.mq and repo.mq.qrepo():
mqcommit = True
else:
mqcommit = True
if mqcommit is None:
return (None, None, None)
q = repo.mq
if q is None:
raise util.Abort("-Q option given but mq extension not installed")
r = q.qrepo()
if r is None:
raise util.Abort("-Q option given but patch directory is not versioned")
return mqcommit, q, r
# Monkeypatch qrefresh in mq command table
def qrefresh_wrapper(self, repo, *pats, **opts):
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
diffstat = ""
if mqcommit and mqmessage:
if mqmessage.find("%s") != -1:
buffer = StringIO.StringIO()
m = cmdutil.match(repo, None, {})
diffopts = mdiff.diffopts()
cmdutil.diffordiffstat(self, repo, diffopts,
repo.dirstate.parents()[0], None, m,
stat=True, fp = buffer)
diffstat = buffer.getvalue()
buffer.close()
mq.refresh(self, repo, *pats, **opts)
if mqcommit and len(q.applied) > 0:
patch = q.applied[-1].name
if r is None:
raise util.Abort("no patch repository found when using -Q option")
mqmessage = mqmessage.replace("%p", patch)
mqmessage = mqmessage.replace("%a", 'UPDATE')
mqmessage = mqmessage.replace("%s", diffstat)
commands.commit(r.ui, r, message=mqmessage)
# Monkeypatch qnew in mq command table
def qnew_wrapper(self, repo, patchfn, *pats, **opts):
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
mq.new(self, repo, patchfn, *pats, **opts)
if mqcommit and mqmessage:
mqmessage = mqmessage.replace("%p", patchfn)
mqmessage = mqmessage.replace("%a", 'NEW')
commands.commit(r.ui, r, message=mqmessage)
# Monkeypatch qimport in mq command table
def qimport_wrapper(self, repo, *filename, **opts):
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
mq.qimport(self, repo, *filename, **opts)
if mqcommit and mqmessage:
if len(filename) == 0:
fname = q.full_series[0] # FIXME - can be multiple
else:
fname = filename[0] # FIXME - can be multiple
mqmessage = mqmessage.replace("%p", fname)
mqmessage = mqmessage.replace("%a", 'IMPORT')
commands.commit(r.ui, r, message=mqmessage)
# Monkeypatch qrename in mq command table
def qrename_wrapper(self, repo, patch, name=None, **opts):
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
mq.rename(self, repo, patch, name, **opts)
if mqcommit and mqmessage:
if not name:
name = patch
patch = q.lookup('qtip')
mqmessage = mqmessage.replace("%p", patch)
mqmessage = mqmessage.replace("%n", name)
mqmessage = mqmessage.replace("%a", 'RENAME')
commands.commit(r.ui, r, message=mqmessage)
# Monkeypatch qdelete in mq command table
def qdelete_wrapper(self, repo, *patches, **opts):
'''This function takes a list of patches, which makes the message
substitution rather weird. %a and %p will be replaced with the
action ('DELETE') and patch name, as usual, but one line per patch
will be generated. In addition the %m (multi?) replacement string,
if given, will be replaced with a prefix message "DELETE (n)
patches: patch1 patch2..." that is NOT repeated per patch. It
probably would have been cleaner to give two formats, one for the
header and one for the per-patch lines.'''
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
if mqcommit and mqmessage:
patchnames = [ q.lookup(p) for p in patches ]
mq.delete(self, repo, *patches, **opts)
if mqcommit and mqmessage:
mqmessage = mqmessage.replace("%a", 'DELETE')
if (len(patches) > 1) and (mqmessage.find("%m") != -1):
rep_message = mqmessage.replace("%m", "")
mqmessage = "DELETE %d patches: %s\n" % (len(patches), " ".join(patchnames))
mqmessage += "\n".join([ rep_message.replace("%p", p) for p in patchnames ])
else:
mqmessage = mqmessage.replace("%m", "")
mqmessage = "\n".join([ mqmessage.replace("%p", p) for p in patchnames ])
commands.commit(r.ui, r, message=mqmessage)
# Monkeypatch qnew in mq command table
def qfinish_wrapper(self, repo, *revrange, **opts):
mqmessage = opts.pop('mqmessage', None)
mqcommit, q, r = mqcommit_info(self, repo, opts)
mq.finish(self, repo, *revrange, **opts)
if mqcommit and mqmessage:
commands.commit(r.ui, r, message=mqmessage)
def wrap_mq_function(orig, wrapper, newparams):
for key,info in mq.cmdtable.iteritems():
if info[0] == orig:
wrapper.__doc__ = info[0].__doc__
mq.cmdtable[key] = (wrapper, info[1] + newparams, info[2])
return
wrap_mq_function(mq.refresh,
qrefresh_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', '%a: %p\n%s', 'commit message for patch update')])
wrap_mq_function(mq.new,
qnew_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', '%a: %p', 'commit message for patch creation')])
wrap_mq_function(mq.qimport,
qimport_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', 'IMPORT: %p', 'commit message for patch import')])
wrap_mq_function(mq.delete,
qdelete_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', '%m%a: %p', 'commit message for patch deletion')])
wrap_mq_function(mq.rename,
qrename_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', '%a: %p -> %n', 'commit message for patch rename')])
wrap_mq_function(mq.finish,
qfinish_wrapper,
[('Q', 'mqcommit', None, 'commit change to patch queue'),
('M', 'mqmessage', 'FINISHED', 'commit message for patch finishing')])
cmdtable = {
'qshow': (qshow,
[('', 'stat', None, 'output diffstat-style summary of changes'),
],
('hg qshow [patch]')),
'qexport':
(qexport,
[('p', 'patch', None, 'add .patch suffix to exported patches'),
('e', 'extension', '', 'append .EXTENSION to exported patches'),
('n', 'numbered', None, 'prefix patch names with order numbers'),
('d', 'outdir', '/tmp/patches', 'directory to write patches into'),
],
('hg qexport [-p] [-e EXTENSION] [-n]')),
'lineage':
(lineage,
[('r', 'rev', '.', 'Revision to start at', 'REV'),
('l', 'limit', '', 'Max revisions to display', 'LIMIT'),
('s', 'stop', '', 'Stop at this revision', 'REV'),
],
('hg lineage -r REV [-l LIMIT] [-s REV]')),
'qtouched':
(touched,
[('a', 'applied', None, 'Only consider applied patches'),
('p', 'patch', '', 'Restrict to given patch')
],
('hg touched [-a] [-p PATCH] [FILE]')),
}