chiark / gitweb /
devscripts (2.10.69+squeeze4) stable-security; urgency=high
[devscripts.git] / scripts / diff2patches.sh
1 #!/bin/bash -e
2
3 ####################
4 #    Copyright (C) 2007, 2008 by Raphael Geissert <atomo64@gmail.com>
5 #
6 #    This file is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This file is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this file  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #    On Debian systems, the complete text of the GNU General
20 #    Public License 3 can be found in '/usr/share/common-licenses/GPL-3'.
21 ####################
22
23 PROGNAME=$(basename "$0")
24
25 usage () {
26     echo \
27 "Usage: $PROGNAME [options] FILE.diff.gz
28   Options:
29     --help          Show this message
30     --version       Show version and copyright information
31   debian/control must exist on the current path for this script to work
32   If debian/patches exists and is a directory, patches are extracted there,
33   otherwise they are extracted under debian/ (unless the environment variable
34   DEB_PATCHES is defined and points to a valid directory, in which case
35   patches are extracted there)."
36 }
37
38 version () {
39     echo \
40 "This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
41 This code is copyright 2007, 2008 by Raphael Geissert, all rights reserved.
42 This program comes with ABSOLUTELY NO WARRANTY.
43 You are free to redistribute this code under the terms of the
44 GNU General Public License, version 3 or later."
45 }
46
47 case "$1" in
48         --help) usage; exit 0 ;;
49         --version) version; exit 0 ;;
50 esac
51
52 if ! which lsdiff >/dev/null 2>&1; then
53         echo "lsdiff was not found in \$PATH, package patchutils probably not installed!"
54         exit 1
55 fi
56
57 diffgz="$1"
58
59 if [ ! -f "$diffgz" ]; then
60         [ -z "$diffgz" ] && diffgz="an unspecified .diff.gz"
61         echo "Couldn't find $diffgz, aborting!"
62         exit 1
63 fi
64
65 if [ -x /usr/bin/dh_testdir ]; then
66         /usr/bin/dh_testdir || exit 1
67 else
68         [ ! -f debian/control ] && echo "Couldn't find debian/control!" && exit 1
69 fi
70
71 if [ -z "$DEB_PATCHES" ] || [ ! -d "$DEB_PATCHES" ]; then
72         DEB_PATCHES=debian
73         [ -d debian/patches ] && DEB_PATCHES=debian/patches
74 else
75         DEB_PATCHES="$(readlink -f "$DEB_PATCHES")"
76 fi
77
78 echo "Patches will be extracted under $DEB_PATCHES/"
79
80 FILES=$(zcat "$diffgz" | lsdiff --strip 1 | egrep -v ^debian/) || \
81         echo "$(basename "$diffgz") doesn't contain any patch outside debian/"
82
83 for file in $FILES; do
84         [ ! -z "$file" ] || continue
85         echo -n "Extracting $file..."
86         newFileName="$DEB_PATCHES/$(echo "$file" | sed 's#/#___#g').patch"
87         zcat "$diffgz" | filterdiff -i "$file" -p1 > "$newFileName"
88         echo "done"
89 done
90
91 exit