~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patch.py

  • Committer: Aaron Bentley
  • Date: 2007-03-26 16:26:09 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070326162609-7ale5l110bjkfa0g
Add branch parameter to show-paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
1
17
import sys
2
18
from subprocess import Popen, PIPE
 
19
 
 
20
from bzrlib import urlutils
3
21
from bzrlib.transport import get_transport
4
 
from urlparse import urlsplit, urlunsplit
5
 
def patch(branch, location, strip):
 
22
from bzrlib.workingtree import WorkingTree
 
23
import bzrlib.add
 
24
 
 
25
def patch(tree, location, strip, quiet=False):
6
26
    """Apply a patch to a branch, using patch(1).  URLs may be used."""
7
27
    my_file = None
8
28
    if location is None:
9
29
        my_file = sys.stdin
10
30
    else:
11
 
        for prefix in ('http://', 'sftp://', 'file://'):
12
 
            if not location.startswith(prefix):
13
 
                continue
14
 
            (scheme, loc, path, query, fragment) = urlsplit(location)
15
 
            loc_start = urlunsplit((scheme, loc, '/', '', ''))
16
 
            my_file = get_transport(loc_start).get(path[1:])
17
 
        if my_file is None:
18
 
            my_file = file(location, 'rb')
19
 
    cmd = ['patch', '--directory', branch.base, '--strip', str(strip)]
 
31
        location = urlutils.normalize_url(location)
 
32
        dirname, basename = urlutils.split(location)
 
33
        my_file = get_transport(dirname).get(basename)
 
34
    cmd = ['patch', '--directory', tree.basedir, '--strip', str(strip)]
 
35
    if quiet:
 
36
        cmd.append('--quiet')
 
37
    r = 0
20
38
    child_proc = Popen(cmd, stdin=PIPE)
21
39
    for line in my_file:
22
40
        child_proc.stdin.write(line)
23
41
    child_proc.stdin.close()
24
 
    return child_proc.wait()
 
42
    r = child_proc.wait()
 
43
    return r