1
# Copyright (C) 2005, 2006 Canonical Ltd
2
# Copyright (C) 2005, 2008 Aaron Bentley, 2006 Michael Ellerman
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from __future__ import absolute_import
20
"""Diff and patch functionality"""
3
24
from subprocess import Popen, PIPE
5
from bzrlib.errors import NoDiff3
7
Diff and patch functionality
9
__docformat__ = "restructuredtext"
27
from .errors import NoDiff3, BzrError
28
from .textfile import check_text_path
30
class PatchFailed(BzrError):
32
_fmt = """Patch application failed"""
35
class PatchInvokeError(BzrError):
37
_fmt = """Error invoking patch: %(errstr)s%(stderr)s"""
38
internal_error = False
40
def __init__(self, e, stderr=''):
42
self.errstr = os.strerror(e.errno)
43
self.stderr = '\n' + stderr
11
51
def write_to_cmd(args, input=""):
13
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
14
stderr=PIPE, close_fds=True)
16
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
52
"""Spawn a process, and wait for the result
54
If the process is killed, an exception is raised
56
:param args: The command line, the first entry should be the program name
57
:param input: [optional] The text to send the process on stdin
58
:return: (stdout, stderr, status)
60
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
61
stderr=PIPE, close_fds=_do_close_fds)
19
62
stdout, stderr = process.communicate(input)
20
63
status = process.wait()
22
65
raise Exception("%s killed by signal %i" (args[0], -status))
23
66
return stdout, stderr, status
26
69
def patch(patch_contents, filename, output_filename=None, reverse=False):
27
70
"""Apply a patch to a file, to produce another output file. This is should
45
88
args.extend(("-o", output_filename))
46
89
args.append(filename)
47
90
stdout, stderr, status = write_to_cmd(args, patch_contents)
51
94
def diff3(out_file, mine_path, older_path, yours_path):
52
95
def add_label(args, label):
53
96
args.extend(("-L", label))
97
check_text_path(mine_path)
98
check_text_path(older_path)
99
check_text_path(yours_path)
54
100
args = ['diff3', "-E", "--merge"]
55
101
add_label(args, "TREE")
56
102
add_label(args, "ANCESTOR")
58
104
args.extend((mine_path, older_path, yours_path))
60
106
output, stderr, status = write_to_cmd(args)
62
108
if e.errno == errno.ENOENT:
66
112
if status not in (0, 1):
67
113
raise Exception(stderr)
68
file(out_file, "wb").write(output)
114
with open(out_file, 'wb') as f:
119
def patch_tree(tree, patches, strip=0, reverse=False, dry_run=False,
120
quiet=False, out=None):
121
"""Apply a patch to a tree.
124
tree: A MutableTree object
125
patches: list of patches as bytes
126
strip: Strip X segments of paths
127
reverse: Apply reversal of patch
130
return run_patch(tree.basedir, patches, strip, reverse, dry_run,
134
def run_patch(directory, patches, strip=0, reverse=False, dry_run=False,
135
quiet=False, _patch_cmd='patch', target_file=None, out=None):
136
args = [_patch_cmd, '-d', directory, '-s', '-p%d' % strip, '-f']
138
args.append('--quiet')
140
if sys.platform == "win32":
141
args.append('--binary')
146
if sys.platform.startswith('freebsd'):
147
args.append('--check')
149
args.append('--dry-run')
153
if target_file is not None:
154
args.append(target_file)
157
process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=stderr)
159
raise PatchInvokeError(e)
161
for patch in patches:
162
process.stdin.write(bytes(patch))
163
process.stdin.close()
166
raise PatchInvokeError(e, process.stderr.read())
168
result = process.wait()
171
out.write(process.stdout.read())
173
process.stdout.read()