##// END OF EJS Templates
patch: turn patch.diff() into a generator...
Dirkjan Ochtman -
r7308:b6f5490e default
parent child Browse files
Show More
@@ -44,9 +44,7 b' def maketemplater(ui, repo, tmpl):'
44
44
45 def changedlines(ui, repo, ctx1, ctx2):
45 def changedlines(ui, repo, ctx1, ctx2):
46 lines = 0
46 lines = 0
47 ui.pushbuffer()
47 diff = ''.join(patch.diff(repo, ctx1.node(), ctx2.node()))
48 patch.diff(repo, ctx1.node(), ctx2.node())
49 diff = ui.popbuffer()
50 for l in diff.split('\n'):
48 for l in diff.split('\n'):
51 if (l.startswith("+") and not l.startswith("+++ ") or
49 if (l.startswith("+") and not l.startswith("+++ ") or
52 l.startswith("-") and not l.startswith("--- ")):
50 l.startswith("-") and not l.startswith("--- ")):
@@ -92,8 +92,10 b' def difftree(ui, repo, node1=None, node2'
92 if opts['pretty']:
92 if opts['pretty']:
93 catcommit(ui, repo, node2, "")
93 catcommit(ui, repo, node2, "")
94 m = cmdutil.match(repo, files)
94 m = cmdutil.match(repo, files)
95 patch.diff(repo, node1, node2, match=m,
95 chunks = patch.diff(repo, node1, node2, match=m,
96 opts=patch.diffopts(ui, {'git': True}))
96 opts=patch.diffopts(ui, {'git': True}))
97 for chunk in chunks:
98 repo.ui.write(chunk)
97 else:
99 else:
98 __difftree(repo, node1, node2, files=files)
100 __difftree(repo, node1, node2, files=files)
99 if not opts['stdin']:
101 if not opts['stdin']:
@@ -501,15 +501,15 b' def reposetup(ui, repo):'
501 # shrink keywords read from working dir
501 # shrink keywords read from working dir
502 self.lines = kwt.shrinklines(self.fname, self.lines)
502 self.lines = kwt.shrinklines(self.fname, self.lines)
503
503
504 def kw_diff(orig, repo, node1=None, node2=None, match=None,
504 def kw_diff(orig, repo, node1=None, node2=None, match=None, changes=None,
505 fp=None, changes=None, opts=None):
505 opts=None):
506 '''Monkeypatch patch.diff to avoid expansion except when
506 '''Monkeypatch patch.diff to avoid expansion except when
507 comparing against working dir.'''
507 comparing against working dir.'''
508 if node2 is not None:
508 if node2 is not None:
509 kwt.matcher = util.never
509 kwt.matcher = util.never
510 elif node1 is not None and node1 != repo['.'].node():
510 elif node1 is not None and node1 != repo['.'].node():
511 kwt.restrict = True
511 kwt.restrict = True
512 orig(repo, node1, node2, match, fp, changes, opts)
512 return orig(repo, node1, node2, match, changes, opts)
513
513
514 def kwweb_skip(orig, web, req, tmpl):
514 def kwweb_skip(orig, web, req, tmpl):
515 '''Wraps webcommands.x turning off keyword expansion.'''
515 '''Wraps webcommands.x turning off keyword expansion.'''
@@ -321,7 +321,10 b' class queue:'
321 def printdiff(self, repo, node1, node2=None, files=None,
321 def printdiff(self, repo, node1, node2=None, files=None,
322 fp=None, changes=None, opts={}):
322 fp=None, changes=None, opts={}):
323 m = cmdutil.match(repo, files, opts)
323 m = cmdutil.match(repo, files, opts)
324 patch.diff(repo, node1, node2, m, fp, changes, self.diffopts())
324 chunks = patch.diff(repo, node1, node2, m, changes, self.diffopts())
325 write = fp is None and repo.ui.write or fp.write
326 for chunk in chunks:
327 write(chunk)
325
328
326 def mergeone(self, repo, mergeq, head, patch, rev):
329 def mergeone(self, repo, mergeq, head, patch, rev):
327 # first try just applying the patch
330 # first try just applying the patch
@@ -697,8 +700,10 b' class queue:'
697 diffopts = self.diffopts()
700 diffopts = self.diffopts()
698 if opts.get('git'): diffopts.git = True
701 if opts.get('git'): diffopts.git = True
699 parent = self.qparents(repo, n)
702 parent = self.qparents(repo, n)
700 patch.diff(repo, node1=parent, node2=n, fp=p,
703 chunks = patch.diff(repo, node1=parent, node2=n,
701 match=match, opts=diffopts)
704 match=match, opts=diffopts)
705 for chunk in chunks:
706 p.write(chunk)
702 p.close()
707 p.close()
703 wlock = None
708 wlock = None
704 r = self.qrepo()
709 r = self.qrepo()
@@ -1139,8 +1144,10 b' class queue:'
1139 a = util.unique(aa)
1144 a = util.unique(aa)
1140 c = [filter(matchfn, l) for l in (m, a, r)]
1145 c = [filter(matchfn, l) for l in (m, a, r)]
1141 match = cmdutil.matchfiles(repo, util.unique(c[0] + c[1] + c[2]))
1146 match = cmdutil.matchfiles(repo, util.unique(c[0] + c[1] + c[2]))
1142 patch.diff(repo, patchparent, match=match,
1147 chunks = patch.diff(repo, patchparent, match=match,
1143 fp=patchf, changes=c, opts=self.diffopts())
1148 changes=c, opts=self.diffopts())
1149 for chunk in chunks:
1150 patchf.write(chunk)
1144 patchf.close()
1151 patchf.close()
1145
1152
1146 repo.dirstate.setparents(*cparents)
1153 repo.dirstate.setparents(*cparents)
@@ -238,9 +238,8 b' class notifier(object):'
238 maxdiff = int(self.ui.config('notify', 'maxdiff', 300))
238 maxdiff = int(self.ui.config('notify', 'maxdiff', 300))
239 prev = self.repo.changelog.parents(node)[0]
239 prev = self.repo.changelog.parents(node)[0]
240
240
241 self.ui.pushbuffer()
241 chunks = patch.diff(self.repo, prev, ref, opts=patch.diffopts(self.ui))
242 patch.diff(self.repo, prev, ref, opts=patch.diffopts(self.ui))
242 difflines = ''.join(chunks).splitlines()
243 difflines = self.ui.popbuffer().splitlines()
244
243
245 if self.ui.configbool('notify', 'diffstat', True):
244 if self.ui.configbool('notify', 'diffstat', True):
246 s = patch.diffstat(difflines)
245 s = patch.diffstat(difflines)
@@ -413,9 +413,10 b' def dorecord(ui, repo, committer, *pats,'
413 modified, added, removed = changes
413 modified, added, removed = changes
414 match = cmdutil.matchfiles(repo, modified + added + removed)
414 match = cmdutil.matchfiles(repo, modified + added + removed)
415 diffopts = mdiff.diffopts(git=True, nodates=True)
415 diffopts = mdiff.diffopts(git=True, nodates=True)
416 chunks = patch.diff(repo, repo.dirstate.parents()[0], match=match,
417 changes=changes, opts=diffopts)
416 fp = cStringIO.StringIO()
418 fp = cStringIO.StringIO()
417 patch.diff(repo, repo.dirstate.parents()[0], match=match,
419 fp.write(''.join(chunks))
418 changes=changes, opts=diffopts, fp=fp)
419 fp.seek(0)
420 fp.seek(0)
420
421
421 # 1. filter patch, so we have intending-to apply subset of it
422 # 1. filter patch, so we have intending-to apply subset of it
@@ -138,7 +138,9 b' class transplanter:'
138 else:
138 else:
139 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
139 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
140 fp = os.fdopen(fd, 'w')
140 fp = os.fdopen(fd, 'w')
141 patch.diff(source, parents[0], node, fp=fp, opts=diffopts)
141 gen = patch.diff(source, parents[0], node, opts=diffopts)
142 for chunk in gen:
143 fp.write(chunk)
142 fp.close()
144 fp.close()
143
145
144 del revmap[rev]
146 del revmap[rev]
@@ -405,7 +407,8 b' def browserevs(ui, repo, nodes, opts):'
405 action = None
407 action = None
406 elif action == 'p':
408 elif action == 'p':
407 parent = repo.changelog.parents(node)[0]
409 parent = repo.changelog.parents(node)[0]
408 patch.diff(repo, parent, node)
410 for chunk in patch.diff(repo, parent, node):
411 repo.ui.write(chunk)
409 action = None
412 action = None
410 elif action not in ('y', 'n', 'm', 'c', 'q'):
413 elif action not in ('y', 'n', 'm', 'c', 'q'):
411 ui.write('no such option\n')
414 ui.write('no such option\n')
@@ -673,8 +673,10 b' class changeset_printer(object):'
673 def showpatch(self, node):
673 def showpatch(self, node):
674 if self.patch:
674 if self.patch:
675 prev = self.repo.changelog.parents(node)[0]
675 prev = self.repo.changelog.parents(node)[0]
676 patch.diff(self.repo, prev, node, match=self.patch, fp=self.ui,
676 chunks = patch.diff(self.repo, prev, node, match=self.patch,
677 opts=patch.diffopts(self.ui))
677 opts=patch.diffopts(self.ui))
678 for chunk in chunks:
679 self.ui.write(chunk)
678 self.ui.write("\n")
680 self.ui.write("\n")
679
681
680 def _meaningful_parentrevs(self, log, rev):
682 def _meaningful_parentrevs(self, log, rev):
@@ -1010,7 +1010,9 b' def diff(ui, repo, *pats, **opts):'
1010 node1, node2 = cmdutil.revpair(repo, opts.get('rev'))
1010 node1, node2 = cmdutil.revpair(repo, opts.get('rev'))
1011
1011
1012 m = cmdutil.match(repo, pats, opts)
1012 m = cmdutil.match(repo, pats, opts)
1013 patch.diff(repo, node1, node2, match=m, opts=patch.diffopts(ui, opts))
1013 it = patch.diff(repo, node1, node2, match=m, opts=patch.diffopts(ui, opts))
1014 for chunk in it:
1015 repo.ui.write(chunk)
1014
1016
1015 def export(ui, repo, *changesets, **opts):
1017 def export(ui, repo, *changesets, **opts):
1016 """dump the header and diffs for one or more changesets
1018 """dump the header and diffs for one or more changesets
@@ -1169,9 +1169,8 b' def _addmodehdr(header, omode, nmode):'
1169 header.append('old mode %s\n' % omode)
1169 header.append('old mode %s\n' % omode)
1170 header.append('new mode %s\n' % nmode)
1170 header.append('new mode %s\n' % nmode)
1171
1171
1172 def diff(repo, node1=None, node2=None, match=None,
1172 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None):
1173 fp=None, changes=None, opts=None):
1173 '''yields diff of changes to files between two nodes, or node and
1174 '''print diff of changes to files between two nodes, or node and
1175 working directory.
1174 working directory.
1176
1175
1177 if node1 is None, use first dirstate parent instead.
1176 if node1 is None, use first dirstate parent instead.
@@ -1182,8 +1181,6 b' def diff(repo, node1=None, node2=None, m'
1182
1181
1183 if opts is None:
1182 if opts is None:
1184 opts = mdiff.defaultopts
1183 opts = mdiff.defaultopts
1185 if fp is None:
1186 fp = repo.ui
1187
1184
1188 if not node1:
1185 if not node1:
1189 node1 = repo.dirstate.parents()[0]
1186 node1 = repo.dirstate.parents()[0]
@@ -1274,9 +1271,10 b' def diff(repo, node1=None, node2=None, m'
1274 # ctx2 date may be dynamic
1271 # ctx2 date may be dynamic
1275 tn, util.datestr(ctx2.date()),
1272 tn, util.datestr(ctx2.date()),
1276 a, b, r, opts=opts)
1273 a, b, r, opts=opts)
1277 if text or len(header) > 1:
1274 if header and (text or len(header) > 1):
1278 fp.write(''.join(header))
1275 yield ''.join(header)
1279 fp.write(text)
1276 if text:
1277 yield text
1280
1278
1281 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
1279 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
1282 opts=None):
1280 opts=None):
@@ -1312,7 +1310,8 b" def export(repo, revs, template='hg-%h.p"
1312 fp.write(ctx.description().rstrip())
1310 fp.write(ctx.description().rstrip())
1313 fp.write("\n\n")
1311 fp.write("\n\n")
1314
1312
1315 diff(repo, prev, node, fp=fp, opts=opts)
1313 for chunk in diff(repo, prev, node, opts=opts):
1314 fp.write(chunk)
1316 if fp not in (sys.stdout, repo.ui):
1315 if fp not in (sys.stdout, repo.ui):
1317 fp.close()
1316 fp.close()
1318
1317
General Comments 0
You need to be logged in to leave comments. Login now