~abentley/bzrtools/bzrtools.dev

257.1.2 by Aaron Bentley
Updated GPL notices
1
# Copyright (C) 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
3
#
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.
8
#
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.
13
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
152 by Aaron Bentley
Added new bzr patch command
17
import sys
18
from subprocess import Popen, PIPE
259 by Aaron Bentley
Got patch working with urls
19
from bzrlib.transport import get_transport
20
from urlparse import urlsplit, urlunsplit
21
def patch(branch, location, strip):
22
    """Apply a patch to a branch, using patch(1).  URLs may be used."""
23
    my_file = None
24
    if location is None:
152 by Aaron Bentley
Added new bzr patch command
25
        my_file = sys.stdin
26
    else:
259 by Aaron Bentley
Got patch working with urls
27
        for prefix in ('http://', 'sftp://', 'file://'):
28
            if not location.startswith(prefix):
29
                continue
30
            (scheme, loc, path, query, fragment) = urlsplit(location)
31
            loc_start = urlunsplit((scheme, loc, '/', '', ''))
32
            my_file = get_transport(loc_start).get(path[1:])
33
        if my_file is None:
34
            my_file = file(location, 'rb')
152 by Aaron Bentley
Added new bzr patch command
35
    cmd = ['patch', '--directory', branch.base, '--strip', str(strip)]
36
    child_proc = Popen(cmd, stdin=PIPE)
37
    for line in my_file:
38
        child_proc.stdin.write(line)
39
    child_proc.stdin.close()
40
    return child_proc.wait()