15
14
# along with this program; if not, write to the Free Software
16
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from __future__ import absolute_import
20
"""Diff and patch functionality"""
24
20
from subprocess import Popen, PIPE
28
from .errors import NoDiff3, BzrError
29
from .textfile import check_text_path
31
class PatchFailed(BzrError):
33
_fmt = """Patch application failed"""
36
class PatchInvokeError(BzrError):
38
_fmt = """Error invoking patch: %(errstr)s%(stderr)s"""
39
internal_error = False
41
def __init__(self, e, stderr=''):
43
self.errstr = os.strerror(e.errno)
44
self.stderr = '\n' + stderr
22
from bzrlib.errors import NoDiff3
23
from bzrlib.textfile import check_text_path
25
"""Diff and patch functionality"""
27
__docformat__ = "restructuredtext"
47
30
_do_close_fds = True
105
88
args.extend((mine_path, older_path, yours_path))
107
90
output, stderr, status = write_to_cmd(args)
109
92
if e.errno == errno.ENOENT:
113
96
if status not in (0, 1):
114
97
raise Exception(stderr)
115
with open(out_file, 'wb') as f:
98
f = open(out_file, 'wb')
120
def patch_tree(tree, patches, strip=0, reverse=False, dry_run=False,
121
quiet=False, out=None):
122
"""Apply a patch to a tree.
125
tree: A MutableTree object
126
patches: list of patches as bytes
127
strip: Strip X segments of paths
128
reverse: Apply reversal of patch
131
return run_patch(tree.basedir, patches, strip, reverse, dry_run,
135
def run_patch(directory, patches, strip=0, reverse=False, dry_run=False,
136
quiet=False, _patch_cmd='patch', target_file=None, out=None):
137
args = [_patch_cmd, '-d', directory, '-s', '-p%d' % strip, '-f']
139
args.append('--quiet')
141
if sys.platform == "win32":
142
args.append('--binary')
147
if sys.platform.startswith('freebsd'):
148
args.append('--check')
150
args.append('--dry-run')
154
if target_file is not None:
155
args.append(target_file)
158
process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=stderr)
160
raise PatchInvokeError(e)
162
for patch in patches:
163
process.stdin.write(bytes(patch))
164
process.stdin.close()
167
raise PatchInvokeError(e, process.stderr.read())
169
result = process.wait()
172
out.write(process.stdout.read())
174
process.stdout.read()
181
def iter_patched_from_hunks(orig_lines, hunks):
182
"""Iterate through a series of lines with a patch applied.
183
This handles a single file, and does exact, not fuzzy patching.
185
:param orig_lines: The unpatched lines.
186
:param hunks: An iterable of Hunk instances.
188
This is different from breezy.patches in that it invokes the patch
191
with tempfile.NamedTemporaryFile() as f:
192
f.writelines(orig_lines)
194
# TODO(jelmer): Stream patch contents to command, rather than
195
# serializing the entire patch upfront.
196
serialized = b''.join([hunk.as_bytes() for hunk in hunks])
197
args = ["patch", "-f", "-s", "--posix", "--binary",
198
"-o", "-", f.name, "-r", "-"]
199
stdout, stderr, status = write_to_cmd(args, serialized)
202
raise PatchFailed(stderr)