annotate mercurial/mdiff.py @ 52245:d57d1606049c stable

clone: properly exclude rev-branch-cache from post clone cache warming When adding "CACHE_REV_BRANCH" to "CACHES_ALL" in e51161b12c7e, I did not expected it to impact the clone steps. However the "CACHES_POST_CLONE" set is created rather creatively. (we should fix that, but not on stable) The benchmark caught a quite significant slowdown one hardlink and ssh-stream clones. Such slow down can be reduced to around ~5% by fully warming the cache before the clone. However keeping this expensive step away from the clone operation fully fix the slowdown and preserve the initial intend. Example slowdow for hardlink clone ### benchmark.name = hg.command.clone # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.explicit-rev = none # benchmark.variants.issue6528 = default # benchmark.variants.protocol = local-hardlink # benchmark.variants.pulled-delta-reuse-policy = default # benchmark.variants.resource-usage = default # benchmark.variants.validate = default ## data-env-vars.name = netbeans-2018-08-01-zstd-sparse-revlog 6.8.2: 19.799752 6.9rc0: 29.017493 (+46.55%, +9.22) after: 19.929341 ## data-env-vars.name = mercurial-public-2018-08-01-zstd-sparse-revlog 6.8.2: 0.468020 6.9rc0: 1.701294 (+263.51%, +1.23) after: 0.471934 ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog 6.8.2: 2.397564 6.9rc0: 5.666641 (+137.41%, +3.28) after: 2.428085
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 13 Nov 2024 13:33:36 +0100
parents d7f17819ae9e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
239
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
1 # mdiff.py - diff and patch routines for mercurial
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
2 #
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
3 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
239
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
4 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7436
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10185
diff changeset
6 # GNU General Public License version 2 or any later version.
239
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
7
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50929
diff changeset
8 from __future__ import annotations
27484
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
9
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
10 import re
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
11 import struct
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
12 import typing
27484
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
13 import zlib
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
14
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
15 from typing import (
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
16 Iterable,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
17 Iterator,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
18 List,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
19 Optional,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
20 Sequence,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
21 Tuple,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
22 Union,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
23 cast,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
24 )
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
25
27484
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
26 from .i18n import _
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
27 from . import (
45154
10f48720ef95 diff: move no-eol text constant to a common location
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 44452
diff changeset
28 diffhelper,
36414
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
29 encoding,
27484
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
30 error,
32369
3b88a7fa97d8 bdiff: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents: 32202
diff changeset
31 policy,
31631
a7acda2de4b8 diff: use pycompat.{byteskwargs, strkwargs} to switch opts b/w bytes and str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31273
diff changeset
32 pycompat,
27484
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
33 util,
0d7635dca691 mdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26587
diff changeset
34 )
51933
f2832de2a46c interfaces: introduce and use a protocol class for the `bdiff` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51932
diff changeset
35 from .interfaces import (
f2832de2a46c interfaces: introduce and use a protocol class for the `bdiff` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51932
diff changeset
36 modules as intmod,
f2832de2a46c interfaces: introduce and use a protocol class for the `bdiff` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51932
diff changeset
37 )
f2832de2a46c interfaces: introduce and use a protocol class for the `bdiff` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51932
diff changeset
38
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36414
diff changeset
39 from .utils import dateutil
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
40
51933
f2832de2a46c interfaces: introduce and use a protocol class for the `bdiff` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51932
diff changeset
41 bdiff: intmod.BDiff = policy.importmod('bdiff')
51964
d7f17819ae9e interfaces: introduce and use a protocol class for the `mpatch` module
Matt Harbison <matt_harbison@yahoo.com>
parents: 51936
diff changeset
42 mpatch: intmod.MPatch = policy.importmod('mpatch')
32369
3b88a7fa97d8 bdiff: switch to policy importer
Yuya Nishihara <yuya@tcha.org>
parents: 32202
diff changeset
43
32202
ded48ad55146 bdiff: proxy through mdiff module
Yuya Nishihara <yuya@tcha.org>
parents: 32201
diff changeset
44 blocks = bdiff.blocks
ded48ad55146 bdiff: proxy through mdiff module
Yuya Nishihara <yuya@tcha.org>
parents: 32201
diff changeset
45 fixws = bdiff.fixws
32200
2d84947cd85d mdiff: move re-exports to top
Yuya Nishihara <yuya@tcha.org>
parents: 31808
diff changeset
46 patches = mpatch.patches
2d84947cd85d mdiff: move re-exports to top
Yuya Nishihara <yuya@tcha.org>
parents: 31808
diff changeset
47 patchedsize = mpatch.patchedsize
36655
68026dd7c4f9 cext: accept arguments as Py_buffer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36625
diff changeset
48 textdiff = bdiff.bdiff
36146
29dd37a418aa bdiff: write a native version of splitnewlines
Augie Fackler <augie@google.com>
parents: 35952
diff changeset
49 splitnewlines = bdiff.splitnewlines
2248
b914f0557832 fix diffs containing embedded "\r".
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2078
diff changeset
50
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
51 if typing.TYPE_CHECKING:
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
52 HunkLines = List[bytes]
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
53 """Lines of a hunk- a header, followed by line additions and deletions."""
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
54
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
55 HunkRange = Tuple[int, int, int, int]
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
56 """HunkRange represents the range information of a hunk.
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
57
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
58 The tuple (s1, l1, s2, l2) forms the header '@@ -s1,l1 +s2,l2 @@'."""
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
59
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
60 Range = Tuple[int, int]
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
61 """A (lowerbound, upperbound) range tuple."""
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
62
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
63 TypedBlock = Tuple[intmod.BDiffBlock, bytes]
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
64 """A bdiff block with its type."""
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
65
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
66
43483
f2f460cdb4f5 mdiff: mark diffopts as having dynamic attributes
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
67 # TODO: this looks like it could be an attrs, which might help pytype
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
68 class diffopts:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45873
diff changeset
69 """context is the number of context lines
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
70 text treats all files as text
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
71 showfunc enables diff -p output
2907
8b02af865990 Add diff --git option
Brendan Cully <brendan@kublai.com>
parents: 2874
diff changeset
72 git enables the git extended patch format
3199
096f1c73cdc3 Add -D/--nodates options to hg diff/export that removes dates from diff headers
Stephen Darnell <stephen@darnell.plus.com>
parents: 3026
diff changeset
73 nodates removes dates from diff headers
23293
b89856abf4e2 mdiff.diffopts: add doc comment for nobinary
Siddharth Agarwal <sid0@fb.com>
parents: 21790
diff changeset
74 nobinary ignores binary files
23294
ec8c73b02e2e mdiff.diffopts: add a new noprefix option
Siddharth Agarwal <sid0@fb.com>
parents: 23293
diff changeset
75 noprefix disables the 'a/' and 'b/' prefixes (ignored in plain mode)
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
76 ignorews ignores all whitespace changes in the diff
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
77 ignorewsamount ignores changes in the amount of whitespace
10189
e451e599fbcf patch: support diff data loss detection and upgrade
Patrick Mezard <pmezard@gmail.com>
parents: 10185
diff changeset
78 ignoreblanklines ignores changes whose lines are all blank
e451e599fbcf patch: support diff data loss detection and upgrade
Patrick Mezard <pmezard@gmail.com>
parents: 10185
diff changeset
79 upgrade generates git diffs to avoid data loss
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45873
diff changeset
80 """
396
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
81
43483
f2f460cdb4f5 mdiff: mark diffopts as having dynamic attributes
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
82 _HAS_DYNAMIC_ATTRIBUTES = True
f2f460cdb4f5 mdiff: mark diffopts as having dynamic attributes
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
83
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
84 defaults = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
85 b'context': 3,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
86 b'text': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
87 b'showfunc': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
88 b'git': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
89 b'nodates': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
90 b'nobinary': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
91 b'noprefix': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
92 b'index': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
93 b'ignorews': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
94 b'ignorewsamount': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
95 b'ignorewseol': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
96 b'ignoreblanklines': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
97 b'upgrade': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
98 b'showsimilarity': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
99 b'worddiff': False,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
100 b'xdiff': False,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
101 }
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
102
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
103 def __init__(self, **opts):
31631
a7acda2de4b8 diff: use pycompat.{byteskwargs, strkwargs} to switch opts b/w bytes and str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31273
diff changeset
104 opts = pycompat.byteskwargs(opts)
29416
30789efb1e5e mdiff: remove use of __slots__
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27711
diff changeset
105 for k in self.defaults.keys():
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
106 v = opts.get(k)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
107 if v is None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
108 v = self.defaults[k]
50914
e586a7eb380a diff-option: move attributes handling to sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50589
diff changeset
109 setattr(self, pycompat.sysstr(k), v)
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
110
6467
65029a3aafc2 Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents: 5863
diff changeset
111 try:
65029a3aafc2 Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents: 5863
diff changeset
112 self.context = int(self.context)
65029a3aafc2 Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents: 5863
diff changeset
113 except ValueError:
48365
67064c238ae7 errors: use detailed exit code for non-integer number of diff context lines
Martin von Zweigbergk <martinvonz@google.com>
parents: 46819
diff changeset
114 raise error.InputError(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43089
diff changeset
115 _(b'diff context lines count must be an integer, not %r')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
116 % pycompat.bytestr(self.context)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
117 )
6467
65029a3aafc2 Let --unified default to diff.unified (issue 1076)
Patrick Mezard <pmezard@gmail.com>
parents: 5863
diff changeset
118
10185
7637fe4f525d mq: preserve --git flag when merging patches
Patrick Mezard <pmezard@gmail.com>
parents: 9827
diff changeset
119 def copy(self, **kwargs):
50914
e586a7eb380a diff-option: move attributes handling to sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50589
diff changeset
120 opts = {k: getattr(self, pycompat.sysstr(k)) for k in self.defaults}
33102
1b6946f87c50 py3: use pycompat.strkwargs() to convert kwargs keys to str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32371
diff changeset
121 opts = pycompat.strkwargs(opts)
10185
7637fe4f525d mq: preserve --git flag when merging patches
Patrick Mezard <pmezard@gmail.com>
parents: 9827
diff changeset
122 opts.update(kwargs)
7637fe4f525d mq: preserve --git flag when merging patches
Patrick Mezard <pmezard@gmail.com>
parents: 9827
diff changeset
123 return diffopts(**opts)
7637fe4f525d mq: preserve --git flag when merging patches
Patrick Mezard <pmezard@gmail.com>
parents: 9827
diff changeset
124
49897
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
125 def __bytes__(self):
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
126 return b", ".join(
50914
e586a7eb380a diff-option: move attributes handling to sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50589
diff changeset
127 b"%s: %r" % (k, getattr(self, pycompat.sysstr(k)))
e586a7eb380a diff-option: move attributes handling to sysstr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50589
diff changeset
128 for k in self.defaults
49897
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
129 )
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
130
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
131 __str__ = encoding.strmethod(__bytes__)
a78dfb1ad60e mdiff: add a __str__ method to diffopts
Matt Harbison <matt_harbison@yahoo.com>
parents: 49284
diff changeset
132
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
133
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
134 defaultopts = diffopts()
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
135
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
136
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
137 def wsclean(opts: diffopts, text: bytes, blank: bool = True) -> bytes:
4878
372d93f03d3a diff: correctly handle combinations of whitespace options
Matt Mackall <mpm@selenic.com>
parents: 4679
diff changeset
138 if opts.ignorews:
51932
d94e21b5b693 mdiff: tweak calls into `bdiff.fixws` to match its type hints
Matt Harbison <matt_harbison@yahoo.com>
parents: 51863
diff changeset
139 text = bdiff.fixws(text, True)
4878
372d93f03d3a diff: correctly handle combinations of whitespace options
Matt Mackall <mpm@selenic.com>
parents: 4679
diff changeset
140 elif opts.ignorewsamount:
51932
d94e21b5b693 mdiff: tweak calls into `bdiff.fixws` to match its type hints
Matt Harbison <matt_harbison@yahoo.com>
parents: 51863
diff changeset
141 text = bdiff.fixws(text, False)
9827
4fe9ca519637 mdiff: fix diff -b/B/w on mixed whitespace hunks (issue127)
Patrick Mezard <pmezard@gmail.com>
parents: 8632
diff changeset
142 if blank and opts.ignoreblanklines:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
143 text = re.sub(b'\n+', b'\n', text).strip(b'\n')
34013
da07367d683b mdiff: add a --ignore-space-at-eol option
David Soria Parra <davidsp@fb.com>
parents: 33102
diff changeset
144 if opts.ignorewseol:
37371
d3286dd2ca2f py3: add missing b'' prefix in mdiff.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37370
diff changeset
145 text = re.sub(br'[ \t\r\f]+\n', br'\n', text)
4878
372d93f03d3a diff: correctly handle combinations of whitespace options
Matt Mackall <mpm@selenic.com>
parents: 4679
diff changeset
146 return text
372d93f03d3a diff: correctly handle combinations of whitespace options
Matt Mackall <mpm@selenic.com>
parents: 4679
diff changeset
147
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
148
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
149 def splitblock(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
150 base1: int,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
151 lines1: Iterable[bytes],
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
152 base2: int,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
153 lines2: Iterable[bytes],
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
154 opts: diffopts,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
155 ) -> Iterable[TypedBlock]:
15528
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
156 # The input lines matches except for interwoven blank lines. We
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
157 # transform it into a sequence of matching blocks and blank blocks.
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
158 lines1 = [(wsclean(opts, l) and 1 or 0) for l in lines1]
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
159 lines2 = [(wsclean(opts, l) and 1 or 0) for l in lines2]
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
160 s1, e1 = 0, len(lines1)
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
161 s2, e2 = 0, len(lines2)
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
162 while s1 < e1 or s2 < e2:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
163 i1, i2, btype = s1, s2, b'='
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
164 if i1 >= e1 or lines1[i1] == 0 or i2 >= e2 or lines2[i2] == 0:
15528
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
165 # Consume the block of blank lines
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
166 btype = b'~'
15528
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
167 while i1 < e1 and lines1[i1] == 0:
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
168 i1 += 1
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
169 while i2 < e2 and lines2[i2] == 0:
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
170 i2 += 1
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
171 else:
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
172 # Consume the matching lines
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
173 while i1 < e1 and lines1[i1] == 1 and lines2[i2] == 1:
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
174 i1 += 1
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
175 i2 += 1
51935
77e2994bd617 mdiff: convert a few block definitions from lists to tuples
Matt Harbison <matt_harbison@yahoo.com>
parents: 51934
diff changeset
176 yield (base1 + s1, base1 + i1, base2 + s2, base2 + i2), btype
15528
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
177 s1 = i1
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
178 s2 = i2
a84698badf0b annotate: support diff whitespace filtering flags (issue3030)
Patrick Mezard <pmezard@gmail.com>
parents: 15526
diff changeset
179
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
180
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
181 def hunkinrange(hunk: Tuple[int, int], linerange: Range) -> bool:
31808
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
182 """Return True if `hunk` defined as (start, length) is in `linerange`
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
183 defined as (lowerbound, upperbound).
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
184
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
185 >>> hunkinrange((5, 10), (2, 7))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
186 True
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
187 >>> hunkinrange((5, 10), (6, 12))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
188 True
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
189 >>> hunkinrange((5, 10), (13, 17))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
190 True
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
191 >>> hunkinrange((5, 10), (3, 17))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
192 True
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
193 >>> hunkinrange((5, 10), (1, 3))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
194 False
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
195 >>> hunkinrange((5, 10), (18, 20))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
196 False
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
197 >>> hunkinrange((5, 10), (1, 5))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
198 False
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
199 >>> hunkinrange((5, 10), (15, 27))
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
200 False
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
201 """
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
202 start, length = hunk
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
203 lowerbound, upperbound = linerange
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
204 return lowerbound < start + length and start < upperbound
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
205
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
206
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
207 def blocksinrange(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
208 blocks: Iterable[TypedBlock], rangeb: Range
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
209 ) -> Tuple[List[TypedBlock], Range]:
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
210 """filter `blocks` like (a1, a2, b1, b2) from items outside line range
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
211 `rangeb` from ``(b1, b2)`` point of view.
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
212
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
213 Return `filteredblocks, rangea` where:
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
214
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
215 * `filteredblocks` is list of ``block = (a1, a2, b1, b2), stype`` items of
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
216 `blocks` that are inside `rangeb` from ``(b1, b2)`` point of view; a
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
217 block ``(b1, b2)`` being inside `rangeb` if
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
218 ``rangeb[0] < b2 and b1 < rangeb[1]``;
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
219 * `rangea` is the line range w.r.t. to ``(a1, a2)`` parts of `blocks`.
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
220 """
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
221 lbb, ubb = rangeb
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
222 lba, uba = None, None
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
223 filteredblocks = []
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
224 for block in blocks:
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
225 (a1, a2, b1, b2), stype = block
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
226 if lbb >= b1 and ubb <= b2 and stype == b'=':
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
227 # rangeb is within a single "=" hunk, restrict back linerange1
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
228 # by offsetting rangeb
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
229 lba = lbb - b1 + a1
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
230 uba = ubb - b1 + a1
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
231 else:
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
232 if b1 <= lbb < b2:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
233 if stype == b'=':
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
234 lba = a2 - (b2 - lbb)
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
235 else:
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
236 lba = a1
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
237 if b1 < ubb <= b2:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
238 if stype == b'=':
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
239 uba = a1 + (ubb - b1)
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
240 else:
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
241 uba = a2
31808
ca3b4a2b7e54 mdiff: add a hunkinrange helper function
Denis Laxalde <denis@laxalde.org>
parents: 31715
diff changeset
242 if hunkinrange((b1, (b2 - b1)), rangeb):
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
243 filteredblocks.append(block)
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
244 if lba is None or uba is None or uba < lba:
45873
c8860a212770 errors: raise InputError when line range to followlines() is out of bounds
Martin von Zweigbergk <martinvonz@google.com>
parents: 45154
diff changeset
245 raise error.InputError(_(b'line range exceeds file size'))
30717
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
246 return filteredblocks, (lba, uba)
3eeb8e138e5c mdiff: add a "blocksinrange" function to filter diff blocks by line range
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30023
diff changeset
247
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
248
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
249 def chooseblocksfunc(opts: Optional[diffopts] = None) -> intmod.BDiffBlocksFnc:
51934
09f3a6790e56 interfaces: add the optional `bdiff.xdiffblocks()` method
Matt Harbison <matt_harbison@yahoo.com>
parents: 51933
diff changeset
250 if (
09f3a6790e56 interfaces: add the optional `bdiff.xdiffblocks()` method
Matt Harbison <matt_harbison@yahoo.com>
parents: 51933
diff changeset
251 opts is None
09f3a6790e56 interfaces: add the optional `bdiff.xdiffblocks()` method
Matt Harbison <matt_harbison@yahoo.com>
parents: 51933
diff changeset
252 or not opts.xdiff
09f3a6790e56 interfaces: add the optional `bdiff.xdiffblocks()` method
Matt Harbison <matt_harbison@yahoo.com>
parents: 51933
diff changeset
253 or not getattr(bdiff, 'xdiffblocks', None)
09f3a6790e56 interfaces: add the optional `bdiff.xdiffblocks()` method
Matt Harbison <matt_harbison@yahoo.com>
parents: 51933
diff changeset
254 ):
36676
c6a61298ac32 mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents: 36655
diff changeset
255 return bdiff.blocks
c6a61298ac32 mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents: 36655
diff changeset
256 else:
c6a61298ac32 mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents: 36655
diff changeset
257 return bdiff.xdiffblocks
c6a61298ac32 mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents: 36655
diff changeset
258
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
259
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
260 def allblocks(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
261 text1: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
262 text2: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
263 opts: Optional[diffopts] = None,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
264 lines1: Optional[Sequence[bytes]] = None,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
265 lines2: Optional[Sequence[bytes]] = None,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
266 ) -> Iterable[TypedBlock]:
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
267 """Return (block, type) tuples, where block is an mdiff.blocks
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
268 line entry. type is '=' for blocks matching exactly one another
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
269 (bdiff blocks), '!' for non-matching blocks and '~' for blocks
30023
ff17dff99295 mdiff: remove unused parameter 'refine' from allblocks()
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 29416
diff changeset
270 matching only after having filtered blank lines.
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
271 line1 and line2 are text1 and text2 split with splitnewlines() if
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
272 they are already available.
15525
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
273 """
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
274 if opts is None:
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
275 opts = defaultopts
34013
da07367d683b mdiff: add a --ignore-space-at-eol option
David Soria Parra <davidsp@fb.com>
parents: 33102
diff changeset
276 if opts.ignorews or opts.ignorewsamount or opts.ignorewseol:
15525
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
277 text1 = wsclean(opts, text1, False)
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
278 text2 = wsclean(opts, text2, False)
36676
c6a61298ac32 mdiff: add a config option to use xdiff algorithm
Jun Wu <quark@fb.com>
parents: 36655
diff changeset
279 diff = chooseblocksfunc(opts)(text1, text2)
15525
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
280 for i, s1 in enumerate(diff):
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
281 # The first match is special.
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
282 # we've either found a match starting at line 0 or a match later
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
283 # in the file. If it starts later, old and new below will both be
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
284 # empty and we'll continue to the next match.
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
285 if i > 0:
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
286 s = diff[i - 1]
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
287 else:
51935
77e2994bd617 mdiff: convert a few block definitions from lists to tuples
Matt Harbison <matt_harbison@yahoo.com>
parents: 51934
diff changeset
288 s = (0, 0, 0, 0)
77e2994bd617 mdiff: convert a few block definitions from lists to tuples
Matt Harbison <matt_harbison@yahoo.com>
parents: 51934
diff changeset
289 s = (s[1], s1[0], s[3], s1[2])
15525
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
290
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
291 # bdiff sometimes gives huge matches past eof, this check eats them,
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
292 # and deals with the special first match case described above
15529
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
293 if s[0] != s[1] or s[2] != s[3]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
294 type = b'!'
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
295 if opts.ignoreblanklines:
15529
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
296 if lines1 is None:
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
297 lines1 = splitnewlines(text1)
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
298 if lines2 is None:
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
299 lines2 = splitnewlines(text2)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
300 old = wsclean(opts, b"".join(lines1[s[0] : s[1]]))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
301 new = wsclean(opts, b"".join(lines2[s[2] : s[3]]))
15529
b35cf47286a6 mdiff: split lines in allblocks() only when necessary
Patrick Mezard <pmezard@gmail.com>
parents: 15528
diff changeset
302 if old == new:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
303 type = b'~'
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
304 yield s, type
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
305 yield s1, b'='
15525
935bf2e7dbc5 mdiff: extract blocks whitespace normalization in diffblocks()
Patrick Mezard <pmezard@gmail.com>
parents: 15513
diff changeset
306
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
307
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
308 def unidiff(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
309 a: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
310 ad: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
311 b: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
312 bd: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
313 fn1: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
314 fn2: bytes,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
315 binary: bool,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
316 opts: diffopts = defaultopts,
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
317 ) -> Tuple[List[bytes], Iterable[Tuple[Optional[HunkRange], HunkLines]]]:
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
318 """Return a unified diff as a (headers, hunks) tuple.
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
319
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
320 If the diff is not null, `headers` is a list with unified diff header
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
321 lines "--- <original>" and "+++ <new>" and `hunks` is a generator yielding
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
322 (hunkrange, hunklines) coming from _unidiff().
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
323 Otherwise, `headers` and `hunks` are empty.
35850
079b27b5a869 patch: avoid repeated binary checks if all files in a patch are text
Joerg Sonnenberger <joerg@bec.de>
parents: 35584
diff changeset
324
35951
8b6dd3922f70 patch: unify check_binary and binary flags
Yuya Nishihara <yuya@tcha.org>
parents: 35943
diff changeset
325 Set binary=True if either a or b should be taken as a binary file.
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
326 """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
327
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
328 def datetag(date: bytes, fn: Optional[bytes] = None):
4679
826659bd8053 git patches: correct handling of filenames with spaces
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4361
diff changeset
329 if not opts.git and not opts.nodates:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
330 return b'\t%s' % date
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
331 if fn and b' ' in fn:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
332 return b'\t'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
333 return b''
3026
d838bfac668d Remove dates from git export file lines - they confuse git-apply
Brendan Cully <brendan@kublai.com>
parents: 2907
diff changeset
334
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
335 sentinel = [], ()
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
336 if not a and not b:
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
337 return sentinel
23299
1f510efcd5f3 mdiff.unidiff: add support for noprefix
Siddharth Agarwal <sid0@fb.com>
parents: 23294
diff changeset
338
1f510efcd5f3 mdiff.unidiff: add support for noprefix
Siddharth Agarwal <sid0@fb.com>
parents: 23294
diff changeset
339 if opts.noprefix:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
340 aprefix = bprefix = b''
23299
1f510efcd5f3 mdiff.unidiff: add support for noprefix
Siddharth Agarwal <sid0@fb.com>
parents: 23294
diff changeset
341 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
342 aprefix = b'a/'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
343 bprefix = b'b/'
23299
1f510efcd5f3 mdiff.unidiff: add support for noprefix
Siddharth Agarwal <sid0@fb.com>
parents: 23294
diff changeset
344
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36414
diff changeset
345 epoch = dateutil.datestr((0, 0))
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
346
15437
8f08b635cdce diff: always use / in paths in diff
Mads Kiilerich <mads@kiilerich.com>
parents: 15141
diff changeset
347 fn1 = util.pconvert(fn1)
8f08b635cdce diff: always use / in paths in diff
Mads Kiilerich <mads@kiilerich.com>
parents: 15141
diff changeset
348 fn2 = util.pconvert(fn2)
8f08b635cdce diff: always use / in paths in diff
Mads Kiilerich <mads@kiilerich.com>
parents: 15141
diff changeset
349
35951
8b6dd3922f70 patch: unify check_binary and binary flags
Yuya Nishihara <yuya@tcha.org>
parents: 35943
diff changeset
350 if binary:
6871
13fe85fe396b mdiff: compare content of binary files directly
Martin Geisler <mg@daimi.au.dk>
parents: 6470
diff changeset
351 if a and b and len(a) == len(b) and a == b:
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
352 return sentinel
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
353 headerlines = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
354 hunks = ((None, [b'Binary file %s has changed\n' % fn1]),)
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
355 elif not a:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
356 without_newline = not b.endswith(b'\n')
2251
35fb62a3a673 fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2248
diff changeset
357 b = splitnewlines(b)
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
358 if a is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
359 l1 = b'--- /dev/null%s' % datetag(epoch)
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
360 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
361 l1 = b"--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
362 l2 = b"+++ %s%s" % (bprefix + fn2, datetag(bd, fn2))
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
363 headerlines = [l1, l2]
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
364 size = len(b)
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
365 hunkrange = (0, 0, 1, size)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
366 hunklines = [b"@@ -0,0 +1,%d @@\n" % size] + [b"+" + e for e in b]
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
367 if without_newline:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
368 hunklines[-1] += b'\n'
45154
10f48720ef95 diff: move no-eol text constant to a common location
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 44452
diff changeset
369 hunklines.append(diffhelper.MISSING_NEWLINE_MARKER)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
370 hunks = ((hunkrange, hunklines),)
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
371 elif not b:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
372 without_newline = not a.endswith(b'\n')
2251
35fb62a3a673 fix speed regression in mdiff caused by line split bugfix.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2248
diff changeset
373 a = splitnewlines(a)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
374 l1 = b"--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
375 if b is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
376 l2 = b'+++ /dev/null%s' % datetag(epoch)
1723
fde8fb2cbede Fix diff against an empty file (issue124) and add a test for this.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1637
diff changeset
377 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
378 l2 = b"+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2))
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
379 headerlines = [l1, l2]
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
380 size = len(a)
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
381 hunkrange = (1, size, 0, 0)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
382 hunklines = [b"@@ -1,%d +0,0 @@\n" % size] + [b"-" + e for e in a]
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
383 if without_newline:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
384 hunklines[-1] += b'\n'
45154
10f48720ef95 diff: move no-eol text constant to a common location
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 44452
diff changeset
385 hunklines.append(diffhelper.MISSING_NEWLINE_MARKER)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
386 hunks = ((hunkrange, hunklines),)
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
387 else:
35852
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
388 hunks = _unidiff(a, b, opts=opts)
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
389 if not next(hunks):
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
390 return sentinel
10614
d0050f36e688 remove header handling out of mdiff.bunidiff, rename it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
391
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
392 headerlines = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
393 b"--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
394 b"+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)),
31271
b3861be6aa6c mdiff: distinguish diff headers from hunks in unidiff()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31269
diff changeset
395 ]
31273
92714858dd3e mdiff: let unidiff return (diffheader, hunks)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31272
diff changeset
396
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
397 # The possible bool is consumed from the iterator above in the `next()`
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
398 # call.
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
399 return headerlines, cast(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
400 "Iterable[Tuple[Optional[HunkRange], HunkLines]]", hunks
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
401 )
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
402
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
403
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
404 def _unidiff(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
405 t1: bytes, t2: bytes, opts: diffopts = defaultopts
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
406 ) -> Iterator[Union[bool, Tuple[HunkRange, HunkLines]]]:
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
407 """Yield hunks of a headerless unified diff from t1 and t2 texts.
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
408
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
409 Each hunk consists of a (hunkrange, hunklines) tuple where `hunkrange` is a
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
410 tuple (s1, l1, s2, l2) representing the range information of the hunk to
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
411 form the '@@ -s1,l1 +s2,l2 @@' header and `hunklines` is a list of lines
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
412 of the hunk combining said header followed by line additions and
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
413 deletions.
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
414
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
415 The hunks are prefixed with a bool.
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
416 """
31267
881ed6a4cf87 mdiff: compute newlines-splitted texts within _unidiff
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30806
diff changeset
417 l1 = splitnewlines(t1)
881ed6a4cf87 mdiff: compute newlines-splitted texts within _unidiff
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30806
diff changeset
418 l2 = splitnewlines(t2)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
419
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
420 def contextend(l, len):
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
421 ret = l + opts.context
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
422 if ret > len:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
423 ret = len
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
424 return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
425
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
426 def contextstart(l):
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
427 ret = l - opts.context
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
428 if ret < 0:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
429 return 0
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
430 return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
431
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
432 lastfunc = [0, b'']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
433
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
434 def yieldhunk(
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
435 hunk: Tuple[int, int, int, int, List[bytes]]
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
436 ) -> Iterable[Tuple[HunkRange, HunkLines]]:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
437 (astart, a2, bstart, b2, delta) = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
438 aend = contextend(a2, len(l1))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
439 alen = aend - astart
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
440 blen = b2 - bstart + aend - a2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
441
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
442 func = b""
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
443 if opts.showfunc:
15141
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
444 lastpos, func = lastfunc
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
445 # walk backwards from the start of the context up to the start of
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
446 # the previous hunk context until we find a line starting with an
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
447 # alphanumeric char.
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48946
diff changeset
448 for i in range(astart - 1, lastpos - 1, -1):
35583
2f123f309f61 py3: slice on bytes instead of indexing
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
449 if l1[i][0:1].isalnum():
36414
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
450 func = b' ' + l1[i].rstrip()
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
451 # split long function name if ASCII. otherwise we have no
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
452 # idea where the multi-byte boundary is, so just leave it.
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
453 if encoding.isasciistr(func):
44c4a38bf563 diff: do not split function name if character encoding is unknown
Yuya Nishihara <yuya@tcha.org>
parents: 36146
diff changeset
454 func = func[:41]
15141
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
455 lastfunc[1] = func
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
456 break
15141
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
457 # by recording this hunk's starting point as the next place to
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
458 # start looking for function lines, we avoid reading any line in
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
459 # the file more than once.
16dc9a32ca04 mdiff: speed up showfunc for large diffs
Brodie Rao <brodie@bitheap.org>
parents: 12751
diff changeset
460 lastfunc[0] = astart
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
461
15462
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
462 # zero-length hunk ranges report their start line as one less
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
463 if alen:
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
464 astart += 1
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
465 if blen:
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
466 bstart += 1
2b1ec74c961f mdiff/patch: fix bad hunk handling for unified diffs with zero context
Nicolas Venegas <nvenegas@atlassian.com>
parents: 15141
diff changeset
467
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
468 hunkrange = astart, alen, bstart, blen
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
469 hunklines = (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
470 [b"@@ -%d,%d +%d,%d @@%s\n" % (hunkrange + (func,))]
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
471 + delta
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48946
diff changeset
472 + [b' ' + l1[x] for x in range(a2, aend)]
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
473 )
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
474 # If either file ends without a newline and the last line of
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
475 # that file is part of a hunk, a marker is printed. If the
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
476 # last line of both files is identical and neither ends in
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
477 # a newline, print only one marker. That's the only case in
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
478 # which the hunk can end in a shared line without a newline.
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
479 skip = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
480 if not t1.endswith(b'\n') and astart + alen == len(l1) + 1:
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48946
diff changeset
481 for i in range(len(hunklines) - 1, -1, -1):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
482 if hunklines[i].startswith((b'-', b' ')):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
483 if hunklines[i].startswith(b' '):
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
484 skip = True
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
485 hunklines[i] += b'\n'
45154
10f48720ef95 diff: move no-eol text constant to a common location
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 44452
diff changeset
486 hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
487 break
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
488 if not skip and not t2.endswith(b'\n') and bstart + blen == len(l2) + 1:
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48946
diff changeset
489 for i in range(len(hunklines) - 1, -1, -1):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
490 if hunklines[i].startswith(b'+'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
491 hunklines[i] += b'\n'
45154
10f48720ef95 diff: move no-eol text constant to a common location
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 44452
diff changeset
492 hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
35851
a9d07bd8f758 mdiff: explicitly compute places for the newline marker
Joerg Sonnenberger <joerg@bec.de>
parents: 35850
diff changeset
493 break
31269
5e7fd3a0b17f mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31268
diff changeset
494 yield hunkrange, hunklines
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
495
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
496 # bdiff.blocks gives us the matching sequences in the files. The loop
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
497 # below finds the spaces between those matching sequences and translates
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
498 # them into diff output.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
499 #
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
500 hunk = None
16089
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
501 ignoredlines = 0
35852
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
502 has_hunks = False
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
503 for s, stype in allblocks(t1, t2, opts, l1, l2):
16089
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
504 a1, a2, b1, b2 = s
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
505 if stype != b'!':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
506 if stype == b'~':
16089
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
507 # The diff context lines are based on t1 content. When
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
508 # blank lines are ignored, the new lines offsets must
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
509 # be adjusted as if equivalent blocks ('~') had the
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
510 # same sizes on both sides.
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
511 ignoredlines += (b2 - b1) - (a2 - a1)
15526
e6519c628454 mdiff: make diffblocks() return all blocks, matching and changed
Patrick Mezard <pmezard@gmail.com>
parents: 15525
diff changeset
512 continue
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
513 delta = []
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
514 old = l1[a1:a2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
515 new = l2[b1:b2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
516
16089
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
517 b1 -= ignoredlines
2e8f4b82c551 mdiff: adjust hunk offsets with --ignore-blank-lines (issue3234)
Patrick Mezard <patrick@mezard.eu>
parents: 15657
diff changeset
518 b2 -= ignoredlines
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
519 astart = contextstart(a1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
520 bstart = contextstart(b1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
521 prev = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
522 if hunk:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
523 # join with the previous hunk if it falls inside the context
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2859
diff changeset
524 if astart < hunk[1] + opts.context + 1:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
525 prev = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
526 astart = hunk[1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
527 bstart = hunk[3]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
528 else:
35852
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
529 if not has_hunks:
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
530 has_hunks = True
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
531 yield True
10614
d0050f36e688 remove header handling out of mdiff.bunidiff, rename it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
532 for x in yieldhunk(hunk):
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
533 yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
534 if prev:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
535 # we've joined the previous hunk, record the new ending points.
51935
77e2994bd617 mdiff: convert a few block definitions from lists to tuples
Matt Harbison <matt_harbison@yahoo.com>
parents: 51934
diff changeset
536 hunk = (hunk[0], a2, hunk[2], b2, hunk[4])
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
537 delta = hunk[4]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
538 else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
539 # create a new hunk
51935
77e2994bd617 mdiff: convert a few block definitions from lists to tuples
Matt Harbison <matt_harbison@yahoo.com>
parents: 51934
diff changeset
540 hunk = (astart, a2, bstart, b2, delta)
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
541
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
542 delta[len(delta) :] = [b' ' + x for x in l1[astart:a1]]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
543 delta[len(delta) :] = [b'-' + x for x in old]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
544 delta[len(delta) :] = [b'+' + x for x in new]
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
545
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
546 if hunk:
35852
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
547 if not has_hunks:
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
548 has_hunks = True
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
549 yield True
10614
d0050f36e688 remove header handling out of mdiff.bunidiff, rename it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
550 for x in yieldhunk(hunk):
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
551 yield x
35852
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
552 elif not has_hunks:
6a33e81e4c5e mdiff: remove rewindhunk by yielding a bool first to indicate data
Joerg Sonnenberger <joerg@bec.de>
parents: 35851
diff changeset
553 yield False
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
554
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
555
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
556 def b85diff(to: Optional[bytes], tn: Optional[bytes]) -> bytes:
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
557 '''print base85-encoded binary diff'''
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
558
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
559 def fmtline(line):
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
560 l = len(line)
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
561 if l <= 26:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
562 l = pycompat.bytechr(ord(b'A') + l - 1)
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
563 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
564 l = pycompat.bytechr(l - 26 + ord(b'a') - 1)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
565 return b'%c%s\n' % (l, util.b85encode(line, True))
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
566
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
567 def chunk(text, csize=52):
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
568 l = len(text)
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
569 i = 0
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
570 while i < l:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
571 yield text[i : i + csize]
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
572 i += csize
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
573
17946
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
574 if to is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
575 to = b''
17946
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
576 if tn is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
577 tn = b''
17946
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
578
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
579 if to == tn:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
580 return b''
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
581
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
582 # TODO: deltas
17946
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
583 ret = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
584 ret.append(b'GIT binary patch\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
585 ret.append(b'literal %d\n' % len(tn))
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
586 for l in chunk(zlib.compress(tn)):
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
587 ret.append(fmtline(l))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
588 ret.append(b'\n')
17946
1e13b1184292 diff: move index header generation to patch
Guillermo Pérez <bisho@fb.com>
parents: 17941
diff changeset
589
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
590 return b''.join(ret)
17939
d587925680d9 diff: move b85diff to mdiff module
Guillermo Pérez <bisho at fb.com>
parents: 16362
diff changeset
591
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
592
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
593 def patchtext(bin: bytes) -> bytes:
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
594 pos = 0
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
595 t = []
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
596 while pos < len(bin):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
597 p1, p2, l = struct.unpack(b">lll", bin[pos : pos + 12])
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
598 pos += 12
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
599 t.append(bin[pos : pos + l])
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
600 pos += l
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
601 return b"".join(t)
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
602
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
603
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
604 def patch(a, bin):
12025
2315a95ee887 mdiff.patch(): add a special case for when the base text is empty
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10614
diff changeset
605 if len(a) == 0:
2315a95ee887 mdiff.patch(): add a special case for when the base text is empty
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10614
diff changeset
606 # skip over trivial delta header
15657
d976b1ef6760 util: don't mess with builtins to emulate buffer()
Matt Mackall <mpm@selenic.com>
parents: 15530
diff changeset
607 return util.buffer(bin, 12)
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
608 return mpatch.patches(a, [bin])
432
3b9e3d3d2810 Start using bdiff for generating deltas
mpm@selenic.com
parents: 396
diff changeset
609
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
610
4361
99c853a1408c add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4108
diff changeset
611 # similar to difflib.SequenceMatcher.get_matching_blocks
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
612 def get_matching_blocks(a: bytes, b: bytes) -> List[Tuple[int, int, int]]:
4361
99c853a1408c add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4108
diff changeset
613 return [(d[0], d[2], d[1] - d[0]) for d in bdiff.blocks(a, b)]
99c853a1408c add mdiff.get_matching_blocks
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4108
diff changeset
614
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
615
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
616 def trivialdiffheader(length: int) -> bytes:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
617 return struct.pack(b">lll", 0, 0, length) if length else b''
5367
7530334bf301 revlog: generate trivial deltas against null revision
Matt Mackall <mpm@selenic.com>
parents: 4878
diff changeset
618
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
619
51936
c6899b334d56 typing: add type annotations to `mercurial/mdiff.py`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51935
diff changeset
620 def replacediffheader(oldlen: int, newlen: int) -> bytes:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
621 return struct.pack(b">lll", 0, oldlen, newlen)