[PATCH 01 of 12 V2] context: explicitly take diffopts in `context.diff` (API)

Boris Feld boris.feld at octobus.net
Tue Jul 3 10:32:20 UTC 2018


# HG changeset patch
# User Boris Feld <boris.feld at octobus.net>
# Date 1526994172 -7200
#      Tue May 22 15:02:52 2018 +0200
# Node ID ba68e0459f1fc2d1d86496c4ae42ab99adf08d11
# Parent  1a05e205832a929374c010b2dc6a1c65a60186a3
# EXP-Topic diff-cleanup
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r ba68e0459f1f
context: explicitly take diffopts in `context.diff` (API)

To provide a proper replacement for the `patch.diff(…)` function, the
`context.diff(…)` method needs to be able to take more parameters. To
distinguish the diff options from the new other arguments, we upgrade the diff
options to its own explicit argument.

diff --git a/contrib/synthrepo.py b/contrib/synthrepo.py
--- a/contrib/synthrepo.py
+++ b/contrib/synthrepo.py
@@ -196,7 +196,8 @@ def analyze(ui, repo, *revs, **opts):
             if lastctx.rev() != nullrev:
                 timedelta = ctx.date()[0] - lastctx.date()[0]
                 interarrival[roundto(timedelta, 300)] += 1
-            diff = sum((d.splitlines() for d in ctx.diff(pctx, git=True)), [])
+            diff = sum((d.splitlines()
+                       for d in ctx.diff(pctx, opts={'git': True})), [])
             fileadds, diradds, fileremoves, filechanges = 0, 0, 0, 0
             for filename, mar, lineadd, lineremove, isbin in parsegitdiff(diff):
                 if isbin:
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -294,13 +294,13 @@ class basectx(object):
                               auditor=r.nofsauditor, ctx=self,
                               listsubrepos=listsubrepos, badfn=badfn)
 
-    def diff(self, ctx2=None, match=None, **opts):
+    def diff(self, ctx2=None, match=None, opts=None):
         """Returns a diff generator for the given contexts and matcher"""
         if ctx2 is None:
             ctx2 = self.p1()
         if ctx2 is not None:
             ctx2 = self._repo[ctx2]
-        diffopts = patch.diffopts(self._repo.ui, pycompat.byteskwargs(opts))
+        diffopts = patch.diffopts(self._repo.ui, opts)
         return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
 
     def dirs(self):
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -658,7 +658,7 @@ def diffstatgen(ctx, basectx):
     '''Generator function that provides the diffstat data.'''
 
     stats = patch.diffstatdata(
-        util.iterlines(ctx.diff(basectx, noprefix=False)))
+        util.iterlines(ctx.diff(basectx, opts={'noprefix': False})))
     maxname, maxtotal, addtotal, removetotal, binary = patch.diffstatsum(stats)
     while True:
         yield stats, maxname, maxtotal, addtotal, removetotal, binary
diff --git a/mercurial/obsutil.py b/mercurial/obsutil.py
--- a/mercurial/obsutil.py
+++ b/mercurial/obsutil.py
@@ -396,9 +396,9 @@ def _cmpdiff(leftctx, rightctx):
     # Leftctx or right ctx might be filtered, so we need to use the contexts
     # with an unfiltered repository to safely compute the diff
     leftunfi = leftctx._repo.unfiltered()[leftctx.rev()]
-    leftdiff = leftunfi.diff(git=1)
+    leftdiff = leftunfi.diff(opts={'git': True})
     rightunfi = rightctx._repo.unfiltered()[rightctx.rev()]
-    rightdiff = rightunfi.diff(git=1)
+    rightdiff = rightunfi.diff(opts={'git': True})
 
     left, right = (0, 0)
     while None not in (left, right):
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1802,7 +1802,7 @@ def matching(repo, subset, x):
         'phase': lambda r: repo[r].phase(),
         'substate': lambda r: repo[r].substate,
         'summary': lambda r: repo[r].description().splitlines()[0],
-        'diff': lambda r: list(repo[r].diff(git=True),)
+        'diff': lambda r: list(repo[r].diff(opts={'git': True}),)
     }
     for info in fields:
         getfield = _funcs.get(info, None)
diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -262,7 +262,8 @@ def showdiffstat(context, mapping):
     "modified files: +added/-removed lines"
     """
     ctx = context.resource(mapping, 'ctx')
-    stats = patch.diffstatdata(util.iterlines(ctx.diff(noprefix=False)))
+    diff = ctx.diff(opts={'noprefix': False})
+    stats = patch.diffstatdata(util.iterlines(diff))
     maxname, maxtotal, adds, removes, binary = patch.diffstatsum(stats)
     return '%d: +%d/-%d' % (len(stats), adds, removes)
 
diff --git a/tests/test-context.py b/tests/test-context.py
--- a/tests/test-context.py
+++ b/tests/test-context.py
@@ -77,7 +77,7 @@ print(ctxb.status(ctxa))
 
 # test performing a diff on a memctx
 
-for d in ctxb.diff(ctxa, git=True):
+for d in ctxb.diff(ctxa, opts={'git': True}):
     printb(d, end=b'')
 
 # test safeness and correctness of "ctx.status()"


More information about the Mercurial-devel mailing list