summaryrefslogtreecommitdiffstats
path: root/scripts/diff2patches.sh
blob: fa5fd6afc826ee52536ca61dfe8b73e349f47b41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash

####################
#    Copyright (C) 2007, 2008 by Raphael Geissert <atomo64@gmail.com>
#
#    This file is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This file is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this file  If not, see <https://www.gnu.org/licenses/>.
#
#    On Debian systems, the complete text of the GNU General
#    Public License 3 can be found in '/usr/share/common-licenses/GPL-3'.
####################

set -e

PROGNAME=${0##*/}

usage() {
    echo \
"Usage: $PROGNAME [options] FILE.diff.gz
  Options:
    --help          Show this message
    --version       Show version and copyright information
  debian/control must exist on the current path for this script to work
  If debian/patches exists and is a directory, patches are extracted there,
  otherwise they are extracted under debian/ (unless the environment variable
  DEB_PATCHES is defined and points to a valid directory, in which case
  patches are extracted there)."
}

version() {
    echo \
"This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
This code is copyright 2007, 2008 by Raphael Geissert, all rights reserved.
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 3 or later."
}

case "$1" in
	--help) usage; exit 0 ;;
	--version) version; exit 0 ;;
esac

if ! which lsdiff > /dev/null 2>&1; then
	echo "lsdiff was not found in \$PATH, package patchutils probably not installed!"
	exit 1
fi

diffgz="$1"

if [ ! -f "$diffgz" ]; then
	[ -z "$diffgz" ] && diffgz="an unspecified .diff.gz"
	echo "Couldn't find $diffgz, aborting!"
	exit 1
fi

if [ -x /usr/bin/dh_testdir ]; then
	/usr/bin/dh_testdir || exit 1
else
	[ ! -f debian/control ] && echo "Couldn't find debian/control!" && exit 1
fi

if [ -z "$DEB_PATCHES" ] || [ ! -d "$DEB_PATCHES" ]; then
	DEB_PATCHES=debian
	[ -d debian/patches ] && DEB_PATCHES=debian/patches
else
	DEB_PATCHES="$(readlink -f "$DEB_PATCHES")"
fi

echo "Patches will be extracted under $DEB_PATCHES/"

FILES=$(zcat "$diffgz" | lsdiff --strip 1 | grep -v ^debian/) || \
	echo "$(basename "$diffgz") doesn't contain any patch outside debian/"

for file in $FILES; do
	[ ! -z "$file" ] || continue
	echo -n "Extracting $file..."
	newFileName="$DEB_PATCHES/$(echo "$file" | sed 's#/#___#g').patch"
	zcat "$diffgz" | filterdiff -i "$file" -p1 > "$newFileName"
	echo "done"
done

exit