~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patch.py

  • Committer: Aaron Bentley
  • Date: 2006-05-19 16:47:39 UTC
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: abentley@panoramicfeedback.com-20060519164739-4d0e8f7d54197bea
Get symlink modification, renames and deletion under test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import errno
1
2
import os
2
3
from subprocess import Popen, PIPE
 
4
 
 
5
from bzrlib.errors import NoDiff3
 
6
from bzrlib.textfile import check_text_path
3
7
"""
4
8
Diff and patch functionality
5
9
"""
6
10
__docformat__ = "restructuredtext"
7
11
 
8
12
def write_to_cmd(args, input=""):
9
 
    process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
10
 
                    stderr=PIPE, close_fds=True)
 
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
 
11
20
    stdout, stderr = process.communicate(input)
12
21
    status = process.wait()
13
22
    if status < 0:
40
49
    return status 
41
50
 
42
51
 
43
 
def diff(orig_file, mod_str, orig_label=None, mod_label=None):
44
 
    """Compare two files, and produce a patch.
45
 
 
46
 
    :param orig_file: path to the old file
47
 
    :type orig_file: str
48
 
    :param mod_str: Contents of the new file
49
 
    :type mod_str: str
50
 
    :param orig_label: The label to use for the old file
51
 
    :type orig_label: str
52
 
    :param mod_label: The label to use for the new file
53
 
    :type mod_label: str
54
 
    """
55
 
    args = ["diff", "-u" ]
56
 
    if orig_label is not None and mod_label is not None:
57
 
        args.extend(("-L", orig_label, "-L", mod_label))
58
 
    args.extend(("--", orig_file, "-"))
59
 
    patch, stderr, status = write_to_cmd(args, mod_str)
60
 
    if status == 0:
61
 
        return None
62
 
    else:
63
 
        return patch
64
 
 
65
52
def diff3(out_file, mine_path, older_path, yours_path):
66
53
    def add_label(args, label):
67
54
        args.extend(("-L", label))
 
55
    check_text_path(mine_path)
 
56
    check_text_path(older_path)
 
57
    check_text_path(yours_path)
68
58
    args = ['diff3', "-E", "--merge"]
69
59
    add_label(args, "TREE")
70
60
    add_label(args, "ANCESTOR")
71
61
    add_label(args, "MERGE-SOURCE")
72
62
    args.extend((mine_path, older_path, yours_path))
73
 
    output, stderr, status = write_to_cmd(args)
 
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
74
70
    if status not in (0, 1):
75
71
        raise Exception(stderr)
76
72
    file(out_file, "wb").write(output)