~bzr-pqm/bzr/bzr.dev

1534.7.130 by Aaron Bentley
More conflict handling, test porting
1
import errno
493 by Martin Pool
- Merge aaron's merge command
2
import os
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
3
from subprocess import Popen, PIPE
1534.7.130 by Aaron Bentley
More conflict handling, test porting
4
5
from bzrlib.errors import NoDiff3
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
6
from bzrlib.textfile import check_text_path
493 by Martin Pool
- Merge aaron's merge command
7
"""
8
Diff and patch functionality
9
"""
10
__docformat__ = "restructuredtext"
11
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
12
def write_to_cmd(args, input=""):
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
13
    if os.name != 'nt':
14
        process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
15
                        stderr=PIPE, close_fds=True)
16
    else:
17
        process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
18
                        stderr=PIPE)
19
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
20
    stdout, stderr = process.communicate(input)
21
    status = process.wait()
22
    if status < 0:
23
        raise Exception("%s killed by signal %i" (args[0], -status))
24
    return stdout, stderr, status
25
    
26
493 by Martin Pool
- Merge aaron's merge command
27
def patch(patch_contents, filename, output_filename=None, reverse=False):
28
    """Apply a patch to a file, to produce another output file.  This is should
29
    be suitable for our limited purposes.
30
31
    :param patch_contents: The contents of the patch to apply
32
    :type patch_contents: str
33
    :param filename: the name of the file to apply the patch to
34
    :type filename: str
35
    :param output_filename: The filename to produce.  If None, file is \
36
    modified in-place
37
    :type output_filename: str or NoneType
38
    :param reverse: If true, apply the patch in reverse
39
    :type reverse: bool
40
    :return: 0 on success, 1 if some hunks failed
41
    """
42
    args = ["patch", "-f", "-s", "--posix", "--binary"]
43
    if reverse:
44
        args.append("--reverse")
45
    if output_filename is not None:
46
        args.extend(("-o", output_filename))
47
    args.append(filename)
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
48
    stdout, stderr, status = write_to_cmd(args, patch_contents)
493 by Martin Pool
- Merge aaron's merge command
49
    return status 
50
51
52
def diff3(out_file, mine_path, older_path, yours_path):
53
    def add_label(args, label):
54
        args.extend(("-L", label))
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
55
    check_text_path(mine_path)
56
    check_text_path(older_path)
57
    check_text_path(yours_path)
493 by Martin Pool
- Merge aaron's merge command
58
    args = ['diff3', "-E", "--merge"]
59
    add_label(args, "TREE")
60
    add_label(args, "ANCESTOR")
61
    add_label(args, "MERGE-SOURCE")
62
    args.extend((mine_path, older_path, yours_path))
1534.7.130 by Aaron Bentley
More conflict handling, test porting
63
    try:
64
        output, stderr, status = write_to_cmd(args)
65
    except OSError, e:
66
        if e.errno == errno.ENOENT:
67
            raise NoDiff3
68
        else:
69
            raise
493 by Martin Pool
- Merge aaron's merge command
70
    if status not in (0, 1):
974.1.2 by Aaron Bentley
Replaced popen with subprocess in patch..py
71
        raise Exception(stderr)
493 by Martin Pool
- Merge aaron's merge command
72
    file(out_file, "wb").write(output)
73
    return status