/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/patch.py

  • Committer: Jelmer Vernooij
  • Date: 2019-11-22 04:59:15 UTC
  • mto: This revision was merged to the branch mainline in revision 7416.
  • Revision ID: jelmer@jelmer.uk-20191122045915-4y99lilhub0yt3d6
Add iter_patched_from_hunks implementation that calls out to the patch command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import os
24
24
from subprocess import Popen, PIPE
25
25
import sys
 
26
import tempfile
26
27
 
27
28
from .errors import NoDiff3, BzrError
28
29
from .textfile import check_text_path
175
176
        raise PatchFailed()
176
177
 
177
178
    return result
 
179
 
 
180
 
 
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.
 
184
 
 
185
    :param orig_lines: The unpatched lines.
 
186
    :param hunks: An iterable of Hunk instances.
 
187
 
 
188
    This is different from breezy.patches in that it invokes the patch
 
189
    command.
 
190
    """
 
191
    with tempfile.NamedTemporaryFile() as f:
 
192
        f.writelines(orig_lines)
 
193
        f.flush()
 
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", "-o", "-", f.name]
 
198
        stdout, stderr, status = write_to_cmd(args, serialized)
 
199
    return [stdout]