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
"""Diff and patch functionality"""
22
2
from subprocess import Popen, PIPE
26
from .errors import NoDiff3, BzrError
27
from .textfile import check_text_path
29
class PatchFailed(BzrError):
31
_fmt = """Patch application failed"""
34
class PatchInvokeError(BzrError):
36
_fmt = """Error invoking patch: %(errstr)s%(stderr)s"""
37
internal_error = False
39
def __init__(self, e, stderr=''):
41
self.errstr = os.strerror(e.errno)
42
self.stderr = '\n' + stderr
4
Diff and patch functionality
6
__docformat__ = "restructuredtext"
50
8
def write_to_cmd(args, input=""):
51
"""Spawn a process, and wait for the result
53
If the process is killed, an exception is raised
55
:param args: The command line, the first entry should be the program name
56
:param input: [optional] The text to send the process on stdin
57
:return: (stdout, stderr, status)
59
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
60
stderr=PIPE, close_fds=_do_close_fds)
10
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
11
stderr=PIPE, close_fds=True)
13
process = Popen(args, bufsize=len(input), stdin=PIPE, stdout=PIPE,
61
16
stdout, stderr = process.communicate(input)
62
17
status = process.wait()
64
raise Exception("%s killed by signal %i" % (args[0], -status))
19
raise Exception("%s killed by signal %i" (args[0], -status))
65
20
return stdout, stderr, status
68
23
def patch(patch_contents, filename, output_filename=None, reverse=False):
69
24
"""Apply a patch to a file, to produce another output file. This is should
87
42
args.extend(("-o", output_filename))
88
43
args.append(filename)
89
44
stdout, stderr, status = write_to_cmd(args, patch_contents)
93
48
def diff3(out_file, mine_path, older_path, yours_path):
94
49
def add_label(args, label):
95
50
args.extend(("-L", label))
96
check_text_path(mine_path)
97
check_text_path(older_path)
98
check_text_path(yours_path)
99
51
args = ['diff3', "-E", "--merge"]
100
52
add_label(args, "TREE")
101
53
add_label(args, "ANCESTOR")
102
54
add_label(args, "MERGE-SOURCE")
103
55
args.extend((mine_path, older_path, yours_path))
105
output, stderr, status = write_to_cmd(args)
107
if e.errno == errno.ENOENT:
56
output, stderr, status = write_to_cmd(args)
111
57
if status not in (0, 1):
112
58
raise Exception(stderr)
113
with open(out_file, 'wb') as f:
59
file(out_file, "wb").write(output)
118
def patch_tree(tree, patches, strip=0, reverse=False, dry_run=False,
119
quiet=False, out=None):
120
"""Apply a patch to a tree.
123
tree: A MutableTree object
124
patches: list of patches as bytes
125
strip: Strip X segments of paths
126
reverse: Apply reversal of patch
129
return run_patch(tree.basedir, patches, strip, reverse, dry_run,
133
def run_patch(directory, patches, strip=0, reverse=False, dry_run=False,
134
quiet=False, _patch_cmd='patch', target_file=None, out=None):
135
args = [_patch_cmd, '-d', directory, '-s', '-p%d' % strip, '-f']
137
args.append('--quiet')
139
if sys.platform == "win32":
140
args.append('--binary')
145
if sys.platform.startswith('freebsd'):
146
args.append('--check')
148
args.append('--dry-run')
152
if target_file is not None:
153
args.append(target_file)
156
process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=stderr)
158
raise PatchInvokeError(e)
160
for patch in patches:
161
process.stdin.write(bytes(patch))
162
process.stdin.close()
165
raise PatchInvokeError(e, process.stderr.read())
167
result = process.wait()
170
out.write(process.stdout.read())
172
process.stdout.read()
179
def iter_patched_from_hunks(orig_lines, hunks):
180
"""Iterate through a series of lines with a patch applied.
181
This handles a single file, and does exact, not fuzzy patching.
183
:param orig_lines: The unpatched lines.
184
:param hunks: An iterable of Hunk instances.
186
This is different from breezy.patches in that it invokes the patch
189
with tempfile.NamedTemporaryFile() as f:
190
f.writelines(orig_lines)
192
# TODO(jelmer): Stream patch contents to command, rather than
193
# serializing the entire patch upfront.
194
serialized = b''.join([hunk.as_bytes() for hunk in hunks])
195
args = ["patch", "-f", "-s", "--posix", "--binary",
196
"-o", "-", f.name, "-r", "-"]
197
stdout, stderr, status = write_to_cmd(args, serialized)
200
raise PatchFailed(stderr)