~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
473 by Aaron Bentley
Clean up patch command (support http urls again)
17
import sys
152 by Aaron Bentley
Added new bzr patch command
18
from subprocess import Popen, PIPE
473 by Aaron Bentley
Clean up patch command (support http urls again)
19
20
from bzrlib import urlutils
259 by Aaron Bentley
Got patch working with urls
21
from bzrlib.transport import get_transport
321.2.3 by ghigo
Inform bzr of a renaming
22
from bzrlib.workingtree import WorkingTree
23
import bzrlib.add
473 by Aaron Bentley
Clean up patch command (support http urls again)
24
496 by Aaron Bentley
Add --silent option to patch
25
def patch(tree, location, strip, quiet=False):
259 by Aaron Bentley
Got patch working with urls
26
    """Apply a patch to a branch, using patch(1).  URLs may be used."""
27
    my_file = None
28
    if location is None:
152 by Aaron Bentley
Added new bzr patch command
29
        my_file = sys.stdin
30
    else:
473 by Aaron Bentley
Clean up patch command (support http urls again)
31
        location = urlutils.normalize_url(location)
480 by Aaron Bentley
Patch now handles sftp, code slightly simpler too.
32
        dirname, basename = urlutils.split(location)
33
        my_file = get_transport(dirname).get(basename)
340 by Aaron Bentley
Fixed patch on checkouts
34
    cmd = ['patch', '--directory', tree.basedir, '--strip', str(strip)]
496 by Aaron Bentley
Add --silent option to patch
35
    if quiet:
36
        cmd.append('--quiet')
321.2.2 by ghigo
Initializate r before the use
37
    r = 0
473 by Aaron Bentley
Clean up patch command (support http urls again)
38
    child_proc = Popen(cmd, stdin=PIPE)
39
    for line in my_file:
40
        child_proc.stdin.write(line)
41
    child_proc.stdin.close()
42
    r = child_proc.wait()
340 by Aaron Bentley
Fixed patch on checkouts
43
    return r