-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpmdiff.sh
249 lines (214 loc) · 6.33 KB
/
rpmdiff.sh
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
#!/bin/bash
# rpmdiff : a simple rpmnew/rpmsave/rpmorig updater
#
# Copyright (c) 2018 Martin Födinger <[email protected]>
#
# Original script taken from pacdiff (https://git.archlinux.org/pacman-contrib.git/tree/src/pacdiff.sh.in?id=2197352aca3b3554e891fe4397bc30649f5c6cbd) on 2018-07-23. Pacdiff is written by these guys:
# Copyright (c) 2007 Aaron Griffin <[email protected]>
# Copyright (c) 2013-2016 Pacman Development Team <[email protected]>
#
# This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
#
shopt -s extglob
declare -r myname='rpmdiff'
declare -r myver='1.1'
diffprog=${DIFFPROG:-'vim -d'}
rpmprog=${RPMPROG:-'rpm'}
diffsearchpath=${DIFFSEARCHPATH:-/etc}
USE_COLOR='y'
SUDO=''
declare -a oldsaves
declare -i USE_FIND=0 USE_LOCATE=0 OUTPUTONLY=0 BACKUP=0
#format outputs
plain() {
(( QUIET )) && return
local mesg=$1; shift
printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
}
msg() {
(( QUIET )) && return
local mesg=$1; shift
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
}
msg2() {
(( QUIET )) && return
local mesg=$1; shift
printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
}
ask() {
local mesg=$1; shift
printf "${BLUE}::${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&1
}
warning() {
local mesg=$1; shift
printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
error() {
local mesg=$1; shift
printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}
die() {
error "$@"
exit 1
}
usage() {
cat <<EOF
${myname} v${myver}
A simple program to merge or remove rpmnew/rpmsave/rpmorig files.
Usage: $myname [-l | -f | -p] [--nocolor]
Search Options: select one (default: --find)
-l/--locate scan using locate
-f/--find scan using find
General Options:
-o/--output print files instead of merging them
--nocolor remove colors from output
-s/--sudo use sudo and sudoedit to merge/remove files
-b/--backup when overwriting, save old files with .bak
Environment Variables:
DIFFPROG override the merge program: (default: 'vim -d')
RPMPROG override the rpm program: (default: 'rpm')
DIFFSEARCHPATH override the search path. (only when using find)
(default: /etc)
Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
Example: $myname --output --locate
EOF
}
version() {
printf "%s %s\n" "$myname" "$myver"
echo 'Copyright (C) 2018 Martin Födinger <[email protected]>'
echo
echo 'Original pacdiff script from these guys:'
echo 'Copyright (C) 2007 Aaron Griffin <[email protected]>'
echo 'Copyright (C) 2013-2016 Pacman Development Team <[email protected]>'
}
print_existing() {
[[ -f "$1" ]] && printf '%s\0' "$1"
}
print_existing_rpmsave(){
for f in "${1}"?(.+([0-9])); do
[[ -f $f ]] && printf '%s\0' "$f"
done
}
cmd() {
if (( USE_LOCATE )); then
$SUDO locate -0 -e -b \*.rpmnew \*.rpmorig \*.rpmsave '*.rpmsave.[0-9]*'
elif (( USE_FIND )); then
$SUDO find $diffsearchpath \( -name \*.rpmnew -o -name \*.rpmorig -o -name \*.rpmsave -o -name '*.rpmsave.[0-9]*' \) -print0
fi
}
while [[ -n "$1" ]]; do
case "$1" in
-l|--locate)
USE_LOCATE=1;;
-f|--find)
USE_FIND=1;;
-o|--output)
OUTPUTONLY=1;;
--nocolor)
USE_COLOR='n';;
-s|--sudo)
SUDO=sudo;;
-b|--backup)
BACKUP=1;;
-V|--version)
version; exit 0;;
-h|--help)
usage; exit 0;;
*)
usage; exit 1;;
esac
shift
done
# check if messages are to be printed using color
unset ALL_OFF BOLD BLUE GREEN RED YELLOW
if [[ -t 2 && $USE_COLOR != "n" ]]; then
# prefer terminal safe colored and bold text when tput is supported
if tput setaf 0 &>/dev/null; then
ALL_OFF="$(tput sgr0)"
BOLD="$(tput bold)"
BLUE="${BOLD}$(tput setaf 4)"
GREEN="${BOLD}$(tput setaf 2)"
RED="${BOLD}$(tput setaf 1)"
YELLOW="${BOLD}$(tput setaf 3)"
else
ALL_OFF="\e[1;0m"
BOLD="\e[1;1m"
BLUE="${BOLD}\e[1;34m"
GREEN="${BOLD}\e[1;32m"
RED="${BOLD}\e[1;31m"
YELLOW="${BOLD}\e[1;33m"
fi
fi
readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
if ! type -p ${diffprog%% *} >/dev/null && (( ! OUTPUTONLY )); then
die "Cannot find the $diffprog binary required for viewing differences."
fi
case $(( USE_FIND + USE_LOCATE)) in
0) USE_FIND=1;; # set the default search option
[^1]) error "Only one search option may be used at a time"
usage; exit 1;;
esac
# see http://mywiki.wooledge.org/BashFAQ/020
while IFS= read -u 3 -r -d '' rpmfile; do
file="${rpmfile%.rpm*}"
file_type="rpm${rpmfile##*.rpm}"
if (( OUTPUTONLY )); then
echo "$rpmfile"
continue
fi
# add matches for rpmsave.N to oldsaves array, do not prompt
if [[ $file_type = rpmsave.+([0-9]) ]]; then
oldsaves+=("$rpmfile")
continue
fi
msg "%s file found for %s" "$file_type" "$file"
if [ ! -f "$file" ]; then
warning "$file does not exist"
$SUDO rm -iv "$rpmfile"
continue
fi
if cmp -s "$rpmfile" "$file"; then
msg2 "Files are identical, removing..."
$SUDO rm -v "$rpmfile"
else
while :; do
ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (I)dentify owner, (Q)uit: [v/s/r/o/i/q] " "$file_type" "$file_type"
read c || break
case $c in
q|Q) exit 0;;
r|R) $SUDO rm -v "$rpmfile"; break ;;
o|O)
if (( BACKUP )); then
$SUDO cp -v "$file" "$file.bak"
fi
$SUDO cp -v "$rpmfile" "$file" && $SUDO rm "$rpmfile";
break ;;
i|I)
$SUDO $rpmprog -qf "$file";;
v|V)
$SUDO $diffprog "$rpmfile" "$file"
if cmp -s "$rpmfile" "$file"; then
msg2 "Files are identical, removing..."
$SUDO rm -v "$rpmfile"
break
fi ;;
s|S) break ;;
*) msg2 "Invalid answer." ;;
esac
done
fi
done 3< <(cmd)
(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
exit 0
# vim: set noet: