summaryrefslogtreecommitdiffstats
path: root/bin/patch-wrapper.in
blob: 4ee22c2604a00be92a75f3996d17ae59c65cc92e (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#! @BASH@

# This is a wrapper to GNU patch that recognizes the most common
# options and mimics GNU patch's behavior and output, and creates the
# quilt metadata as if quilt push was used to apply the patches.  When
# options are used that this wrapper does not recognize, GNU patch is
# used directly, and no quilt metadata will get created.

PATCH=@PATCH@
original_options=("$@")

# GNU patch recognizes these environment variables
if [ -n "$SIMPLE_BACKUP_SUFFIX" ]
then
    set -- --suffix "$SIMPLE_BACKUP_SUFFIX" "$@"
fi
if [ -n "$PATCH_VERSION_CONTROL" ]
then
    set -- --version-control "$PATCH_VERSION_CONTROL" "$@"
elif [ -n "$VERSION_CONTROL" ]
then
    set -- --version-control "$VERSION_CONTROL" "$@"
fi
if [ -n "$POSIXLY_CORRECT" ]
then
    set -- --posix "$@"
fi

backup_files()
{
    declare dir=${QUILT_PC:-.pc}/$name

    if [ "$backup_mode" = --backup-if-mismatch ]
    then
	awk '
	/^patching file / \
	    { file=$0
	      sub(/^patching file /, "", file)
	    }
	/^Hunk #[0-9]* / \
	    { if (!(file in backup)) {
	        backup[file] = 1
	        #print "ln -f "dir"/"file" "prefix file suffix > "/dev/stderr"
	        system("ln -f "dir"/"file" "prefix file suffix)
	      }
	    }
	    { if (!silent)
	        print
	    }
	' dir="${dir//\\/\\\\}" \
	  prefix="${opt_prefix//\\/\\\\}" \
	  suffix="${opt_suffix//\\/\\\\}" \
	  silent="${opt_silent:+1}"
    elif [ -n "$opt_silent" ]; then
	cat > /dev/null
    fi
    if [ "$backup_mode" = --backup -a -d "$dir" ]
    then
	for file in $(find "$dir" -type f -a ! -path "$path/.timestamp")
	do
	    dest=$opt_prefix${file#$dir/}$opt_suffix
	    mkdir -p $(dirname "$dest")
	    ln -f $file $dest
	done
    fi
}

find_pipe_patch()
{
    declare patch=$1
    patch=${patch//\[/\\[}
    patch=${patch//\]/\\]}
    set -- $(stat -c $'%a %N\n' /proc/*/fd/* 2>/dev/null \
	     | sed -nre "s,^300 \`(/proc/.*/fd)/.*' -> \`$patch'$,\\1,p")
    set -- $(stat -c $'%a %N\n' $1/* 2>/dev/null \
	     | sed -nre "s,^500 \`.*' -> \`(.*)',\\1,p")
    [ $# -eq 1 ] || set -- "$patch"
    echo "$1"
}

options=`getopt -q -o bsB:z:i:p:d: \
		   --long quiet,silent,backup,backup-if-mismatch \
		   --long no-backup-if-mismatch,prefix: \
		   --long suffix:,posix,input:,strip:,directory: -- "$@"`
if [ $? -ne 0 ]
then
    cannot_handle=1
else
	case "${LC_ALL:-${LC_MESSAGES:-${LANG}}}" in
	''|C|POSIX|en*)
		;;
	*)	cannot_handle=1
		;;
	esac
fi
if [ -z "$cannot_handle" ]; then
    eval set -- "$options"

    backup_mode=--backup-if-mismatch
    opt_prefix=
    opt_suffix=
    opt_silent=
    opt_input=
    opt_strip=
    opt_directory=$PWD

    while :
    do
	case "$1" in
	-b|--backup)
	    backup_mode=--backup
	    ;;
	--backup-if-mismatch)
	    backup_mode=--backup-if-mismatch
	    ;;
	-d|--directory)
	    cd "$2" || exit 1
	    shift
	    ;;
	-i|--input)
	    opt_input=$2
	    new_options[${#new_options[@]}]=$1
	    new_options[${#new_options[@]}]=$2
	    shift
	    ;;
	--no-backup-if-mismatch)
	    backup_mode=--no-backup-if-mismatch
	    ;;
	-B|--prefix)
	    opt_prefix=$2
	    new_options[${#new_options[@]}]=$1
	    new_options[${#new_options[@]}]=$2
	    shift
	    ;;
	-s|--silent|--quiet)
	    opt_silent=--silent
	    ;;
	-p|--strip)
	    opt_strip=-p$2
	    new_options[${#new_options[@]}]=-p$2
	    shift
	    ;;
	-z|--suffix)
	    opt_suffix=$2
	    new_options[${#new_options[@]}]=$1
	    new_options[${#new_options[@]}]=$2
	    shift
	    ;;
	--posix)
	    backup_mode=--no-backup-if-mismatch
	    new_options[${#new_options[@]}]=$1
	    ;;
	--)
	    shift
	    break
	    ;;
	*)
	    new_options[${#new_options[@]}]=$1
	    ;;
	esac
	shift
    done

    [ -n "$opt_prefix$opt_suffix" ] || opt_suffix=.orig
    if [ -z "$opt_strip" -o $# -ne 0 ]
    then
	cannot_handle=1
    fi
fi

if [ -z "$cannot_handle" ]
then
    if [ -n "$opt_input" ]
    then
	patch=$opt_input
    elif [ ! -e /proc/self ]
    then
	echo "patch-wrapper: /proc not mounted!" >&2
	exit 1
    elif [ -e /proc/self/fd/0 ]
    then
	patch=$(readlink /proc/self/fd/0)
	case "$patch" in
	pipe:*)
		patch=$(find_pipe_patch "$patch")
		;;
	esac
    fi
    patch=${patch#$PWD/}

    if [ ! -e "$patch" ]
    then
	cannot_handle=1
    fi
fi

if [ -n "$cannot_handle" ]
then
    $PATCH "${original_options[@]}"
    exit
fi

quilt_patches=${QUILT_PATCHES:-patches}

if [ "${patch#$RPM_SOURCE_DIR}" != "$patch" ]
then
    name=SOURCES/${patch#$RPM_SOURCE_DIR/}
    if [ ! -e "$quilt_patches/SOURCES" ]
    then
	mkdir -p "$quilt_patches"
	ln -s $RPM_SOURCE_DIR "$quilt_patches/SOURCES"
    fi
elif [ "${patch#$RPM_BUILD_DIR}" != "$patch" ]
then
    name=BUILD/${patch#$RPM_BUILD_DIR/}
    if [ ! -e "$quilt_patches/BUILD" ]
    then
	mkdir -p "$quilt_patches"
	ln -s $RPM_BUILD_DIR "$quilt_patches/BUILD"
    fi
else
    name=${patch#/}
    dir=$(dirname "$quilt_patches/$name")
    mkdir -p "$dir"

    if [ "${patch:0:1}" = / ]
    then
	ln -s "$patch" "$quilt_patches/${name#/}"
    else
	while ! [ "$dir/$updir$name" -ef "$patch" ]
	do
	    updir=$updir../
	    [ ${#updir} -gt 96 ] && break
	done
	if [ "$dir/$updir$name" -ef "$patch" ]
	then
	    ln -s "$updir$patch" "$quilt_patches/$name"
	fi
    fi
fi

if [ "$opt_strip" = -p1 ]; then
    echo "$name"
else
    echo "$name $opt_strip"
fi >> $quilt_patches/series

$PATCH "${new_options[@]}" --backup --prefix "${QUILT_PC:-.pc}/$name/" \
    | backup_files
status=${PIPESTATUS[0]}
if [ $status -eq 0 ]
then
    dir=${QUILT_PC:-.pc}/$name
    if [ ! -e "$dir" ]
    then
	mkdir -p "$dir"
    fi
    echo -n "" > $dir/.timestamp
    if [ ! -e ${QUILT_PC:-.pc}/.version ]
    then
	echo 2 > ${QUILT_PC:-.pc}/.version
    fi
    echo "$name" >> "${QUILT_PC:-.pc}/applied-patches"
fi
exit $status