summaryrefslogtreecommitdiffstats
path: root/scripts/apatch.in
blob: 99c5d18b5e538c014a3820fc85892df66feefb1f (plain) (blame)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! @BASH@

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r @SCRIPTS@/patchfns ]
	then
		echo "Cannot read library @SCRIPTS@/patchfns" >&2
		exit 1
	fi
	. @SCRIPTS@/patchfns
fi

usage()
{
	echo $"Usage: $0 [-fqv] patchname"
	exit 1
}

rollback_patch()
{
	local patch=$1 pc_file=$(gen_tempfile)

	# FIXME backup_files should scan the directory hierarchy itself.
	files_in_patch $patch > $pc_file
	@LIB@/backup-files $silent_unless_verbose -f $pc_file -B .pc/$patch/ -r
	if [ -z "$opt_leave_rejects" ]
	then
		@SED@ -e 's/$/\.rej/' $pc_file | xargs rm -f
	fi
	rm -f $pc_file
}

interrupt()
{
	rollback_patch $1
	echo $"Interrupted by user; patch $patch was not applied."
	exit 1
}

apply_patch()
{
	local patch=$1
	local patch_file=$(patch_file_name $patch) output

	if ! [ -e "$patch_file" ]
	then
		echo $"Patch file $patch_file did not exist, push failed"
		exit 1
	fi
	if ! [ -s $patch_file ]
	then
		echo $"Patch file $patch_file appears to be empty"
		return 0
	fi
	
	if [ "x${patch_file:(-3)}" = "x.gz" ]
	then
		output="$(
		gzip -cd $patch_file \
		| @PATCH@ $(patch_args $patch) --backup --prefix=".pc/$patch/" \
			-E $silent $force_apply 2>&1 )"
	elif [ "x${patch_file:(-4)}" = "x.bz2" ]
	then
		output="$(
		bzip2 -cd $patch_file \
		| @PATCH@ $(patch_args $patch) --backup --prefix=".pc/$patch/" \
			-E $silent $force_apply 2>&1 )"
	else
		output="$(
		@PATCH@ $(patch_args $patch) --backup --prefix=".pc/$patch/" \
			-E $silent -i $patch_file $force_apply 2>&1 )"
	fi
	
	status=${PIPESTATUS[0]}
	if [ $status -ne 0 -a -z "$opt_leave_rejects" ]
	then
		# The reject files are removed in rollback_patch.
		echo "$output" \
		| sed -e 's/-- saving rejects to file \(.\+\)\.rej/-- rejects in file \1/'
	elif [ -n "$output" ]
	then
		echo "$output"
	fi
	return $status
}

apatch()
{
	local patch=$(stripit $1)
	local file status

	echo $"Applying $patch"
	trap "interrupt $patch" SIGINT

	apply_patch $patch
	status=$?

	trap "" SIGINT

	if [ $status -eq 0 -o -n "$opt_force" ]
	then
		add_to_db $patch
		if [ $status -eq 0 ]
		then
			rm -f .pc/$patch~refresh
		else
			touch .pc/$patch~refresh
		fi
		if [ "$(shopt -s nullglob ; echo .pc/$patch/*)" = "" ]
		then
			echo $"Patch $patch appears to be empty, applied"
		elif [ $status -ne 0 ]
		then
			echo $"Applied $patch (forced; needs refresh)"
		fi
	else
		rollback_patch $patch
		echo $"Patch $patch does not apply (enforce with -f)"
		status=1
	fi
	trap - SIGINT
	return $status
}

options=`getopt -o fqvh --long leave-rejects,interactive -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-f)
		opt_force=1
		shift ;;
	-q)
		opt_quiet=1
		shift ;;
	-v)
		opt_verbose=1
		shift ;;
	--leave-rejects)
		opt_leave_rejects=1
		shift ;;
	--interactive)
		opt_interactive=1
		shift ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -ne 1 ]
then
	usage
fi

[ -n "$opt_quiet" ] && silent=-s
[ -z "$opt_verbose" ] && silent_unless_verbose=-s
[ -z "$opt_interactive" ] && force_apply=-f
[ -n "$opt_force" ] && opt_leave_rejects=1

patch=$(stripit $1)

top=$(top_patch)
if [ -n "$top" -a -e .pc/$top~refresh ]
then
	echo $"The topmost patch $top needs to be refreshed first."
	exit 1
fi

apatch $patch
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh