tests/test-filelog.py
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 18 Jan 2022 13:05:21 -0800
changeset 48981 f3aafd785e65
parent 48875 6000f5b25c9b
permissions -rwxr-xr-x
filemerge: add support for partial conflict resolution by external tool A common class of merge conflicts is in imports/#includes/etc. It's relatively easy to write a tool that can resolve these conflicts, perhaps by naively just unioning the statements and leaving any cleanup to other tools to do later [1]. Such specialized tools cannot generally resolve all conflicts in a file, of course. Let's therefore call them "partial merge tools". Note that the internal simplemerge algorithm is such a partial merge tool - one that only resolves trivial "conflicts" where one side is unchanged or both sides change in the same way. One can also imagine having smarter language-aware partial tools that merge the AST. It may be useful for such tools to interactively let the user resolve any conflicts it can't resolve itself. However, having the option of implementing it as a partial merge tool means that the developer doesn't *need* to create a UI for it. Instead, the user can resolve any remaining conflicts with their regular merge tool (e.g. `:merge3` or `meld). We don't currently have a way to let the user define such partial merge tools. That's what this patch addresses. It lets the user configure partial merge tools to run. Each tool can be configured to run only on files matching certain patterns (e.g. "*.py"). The tool takes three inputs (local, base, other) and resolves conflicts by updating these in place. For example, let's say the inputs are these: base: ``` import sys def main(): print('Hello') ``` local: ``` import os import sys def main(): print('Hi') ``` other: ``` import re import sys def main(): print('Howdy') ``` A partial merge tool could now resolve the conflicting imports by replacing the import statements in *all* files by the following snippet, while leaving the remainder of the files unchanged. ``` import os import re import sys ``` As a result, simplemerge and any regular merge tool that runs after the partial merge tool(s) will consider the imports to be non-conflicting and will only present the conflict in `main()` to the user. Differential Revision: https://phab.mercurial-scm.org/D12356
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47500
23f5ed6dbcb1 run-tests: stop writing a `python3` symlink pointing to python2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47012
diff changeset
     1
#!/usr/bin/env python
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     2
"""
26098
ce26928cbe41 spelling: behaviour -> behavior
timeless@mozdev.org
parents: 20684
diff changeset
     3
Tests the behavior of filelog w.r.t. data starting with '\1\n'
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
     4
"""
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28805
diff changeset
     5
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45830
diff changeset
     6
from mercurial.node import hex
28743
83373fc2b287 py3: use absolute_import in test-filelog.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 26098
diff changeset
     7
from mercurial import (
83373fc2b287 py3: use absolute_import in test-filelog.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 26098
diff changeset
     8
    hg,
28805
efc739551c17 test-filelog: alias ui as uimod
Yuya Nishihara <yuya@tcha.org>
parents: 28744
diff changeset
     9
    ui as uimod,
28743
83373fc2b287 py3: use absolute_import in test-filelog.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 26098
diff changeset
    10
)
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    11
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29205
diff changeset
    12
myui = uimod.ui.load()
37933
f71c97d9b97b tests: port test-filelog.py to Python 3
Augie Fackler <augie@google.com>
parents: 30559
diff changeset
    13
repo = hg.repository(myui, path=b'.', create=True)
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    14
37933
f71c97d9b97b tests: port test-filelog.py to Python 3
Augie Fackler <augie@google.com>
parents: 30559
diff changeset
    15
fl = repo.file(b'foobar')
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    17
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    18
def addrev(text, renamed=False):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    19
    if renamed:
17486
73e3e368bd42 spelling: doesn't/does not
timeless@mozdev.org
parents: 16498
diff changeset
    20
        # data doesn't matter. Just make sure filelog.renamed() returns True
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45830
diff changeset
    21
        meta = {b'copyrev': hex(repo.nullid), b'copy': b'bar'}
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    22
    else:
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    23
        meta = {}
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    24
15876
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    25
    lock = t = None
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    26
    try:
15876
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    27
        lock = repo.lock()
37933
f71c97d9b97b tests: port test-filelog.py to Python 3
Augie Fackler <augie@google.com>
parents: 30559
diff changeset
    28
        t = repo.transaction(b'commit')
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45830
diff changeset
    29
        node = fl.add(text, meta, t, 0, repo.nullid, repo.nullid)
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    30
        return node
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    31
    finally:
15876
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    32
        if t:
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    33
            t.close()
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    34
        if lock:
2de1244361aa tests: lock before creating transaction in test-filelog
Mads Kiilerich <mads@kiilerich.com>
parents: 11540
diff changeset
    35
            lock.release()
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    36
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    37
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    38
def error(text):
28744
6537e14301ef py3: use print_function in test-filelog.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28743
diff changeset
    39
    print('ERROR: ' + text)
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    41
37933
f71c97d9b97b tests: port test-filelog.py to Python 3
Augie Fackler <augie@google.com>
parents: 30559
diff changeset
    42
textwith = b'\1\nfoo'
f71c97d9b97b tests: port test-filelog.py to Python 3
Augie Fackler <augie@google.com>
parents: 30559
diff changeset
    43
without = b'foo'
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    44
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    45
node = addrev(textwith)
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    46
if not textwith == fl.read(node):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    47
    error('filelog.read for data starting with \\1\\n')
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    48
if fl.cmp(node, textwith) or not fl.cmp(node, without):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    49
    error('filelog.cmp for data starting with \\1\\n')
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    50
if fl.size(0) != len(textwith):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    51
    error(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    52
        'FIXME: This is a known failure of filelog.size for data starting '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    53
        'with \\1\\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37933
diff changeset
    54
    )
11540
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    55
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    56
node = addrev(textwith, renamed=True)
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    57
if not textwith == fl.read(node):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    58
    error('filelog.read for a renaming + data starting with \\1\\n')
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    59
if fl.cmp(node, textwith) or not fl.cmp(node, without):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    60
    error('filelog.cmp for a renaming + data starting with \\1\\n')
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    61
if fl.size(1) != len(textwith):
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    62
    error('filelog.size for a renaming + data starting with \\1\\n')
2370e270a29a filelog: test behaviour for data starting with "\1\n"
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents:
diff changeset
    63
28744
6537e14301ef py3: use print_function in test-filelog.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28743
diff changeset
    64
print('OK.')