This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
pacdiffviewer.sh.in
executable file
·279 lines (264 loc) · 8.17 KB
/
pacdiffviewer.sh.in
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
#
# pacdiffviewer : manage/backup/clean/merge pac* files
#
# Copyright (c) 2008-2010 Julien MISCHKOWITZ <[email protected]>
# Copyright (c) 2010-2014 tuxce <[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 2 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/>.
#
export TEXTDOMAINDIR='@localedir@'
export TEXTDOMAIN=yaourt
NAME='pacdiffviewer'
VERSION='@VERSION@'
P_LOCATE=0
P_SEARCHDIR=(/etc/ /boot/)
P_SAVEDIR='@localstatedir@/lib/yaourt/backupfiles'
. '@libdir@/yaourt/util.sh'
ROOTDIR=
QUIET=0
[[ -d "$P_SAVEDIR" ]] && MERGE=1 || MERGE=0
tmp_file=$(mktemp)
cleanup_add rm "$tmp_file"
usage() {
echo "$NAME $VERSION"
[[ "$1" = "-v" ]] && exit 0
echo -e "\t-c $(gettext 'Delete all *.pac* found')"
echo -e "\t-b $(gettext 'Save all packages backup files for a later merge')"
echo -e "\t-q $(gettext 'Use with backup to not output messages')"
echo -e "\t-s $(gettext 'Sequential listing')"
echo -e "\t-d $(gettext 'Display diff')"
echo -e "\t-h $(gettext 'This help')"
echo -e "\t-v $(gettext 'Show version')"
exit 0
}
# Save files marked as backup in packages for a possible later merge
backup_files() {
local _file _md5sum _version pkgname pkgver backupdir currentfile
pkgquery -Qf '%n - %v\n%B' --delimiter '\n' |
while read _file _md5sum _version
do
# no backups
[[ "$_file" = "-" ]] && pkgname="" && continue
# _md5sum = "-" -> on name/version line
[[ "$_md5sum" = "-" ]] && pkgname="$_file" && pkgver="$_version" &&\
continue
[[ ! -f "$ROOTDIR/$_file" ]] && continue
backupdir="$P_SAVEDIR/$pkgname/$pkgname-$pkgver"
[[ -f "$backupdir/$_file" ]] && continue
currentfile="$_file.pacnew"
[[ -f "$ROOTDIR/$currentfile" ]] || currentfile="$_file"
if echo "$_md5sum $ROOTDIR/$currentfile" |
md5sum --status -c - &>/dev/null; then
(( ! QUIET )) && echo "-> $(gettext 'saving') $ROOTDIR/$currentfile"
mkdir -p "$(dirname "$backupdir/$_file")" || return 1
cp -a "$ROOTDIR/$currentfile" "$backupdir/$_file" || return 1
fi
done
}
# Create pacnew/pacsave/pacorig db
# PACOLD=() -> all pacnew/pacsave files which package is not installed
# PACNEW=() -> rest of pacnew files
# PACSAVE=() -> rest of pacsave files
# PACORIG=() -> pacorig files
create_db() {
unset PACOLD PACNEW PACSAVE
local pacfiles _file ext
IFS=$'\n'
if (( P_LOCATE )); then
pacfiles=($(locate -eb "*.pacorig" "*.pacsave" "*.pacnew"))
else
pacfiles=($(find "${P_SEARCHDIR[@]}" \( -name "*.pacorig" \
-o -name "*.pacsave" -o -name "*.pacnew" \)))
fi
unset IFS
for _file in "${pacfiles[@]}"; do
for ext in .pacnew .pacsave .pacorig; do
[[ ${_file%$ext} != $_file ]] && break
done
[[ ! -f "${_file%$ext}" ]] && PACOLD+=("${_file}") && continue
[[ "$ext" = ".pacnew" ]] && PACNEW+=("${_file}")
[[ "$ext" = ".pacsave" ]] && PACSAVE+=("${_file}")
[[ "$ext" = ".pacorig" ]] && PACORIG+=("${_file}")
done
echo "${#PACORIG[@]} .pacorig $(gettext 'found')"
echo "${#PACNEW[@]} .pacnew $(gettext 'found')"
echo "${#PACSAVE[@]} .pacsave $(gettext 'found')"
echo "${#PACOLD[@]} $(gettext 'files are orphans')"
echo
}
# Return previous version file if we had backup it
# usage: previous_version ($file)
# return previous file or ""
previous_version() {
[[ $1 ]] || return
local file=$1 pkgname pkgver
read pkgname pkgver < <(pacman_parse -Qo "$file" |
awk '{print $(NF-1)" "$NF}' 2>/dev/null)
[[ $pkgname ]] || return
[[ -d "$P_SAVEDIR/$pkgname/" ]] || return
pushd "$P_SAVEDIR/$pkgname/" &> /dev/null
local pkgver_prev="" _rep
for _rep in $(ls -Ad *-*); do
[[ "$_rep" = "$pkgname-$pkgver" || ! -f "${_rep}${file}" ||
$(vercmp $pkgver ${_rep#$pkgname-}) -lt 0 ]] && continue
if [[ -z "$pkgver_prev" || \
$(vercmp $pkgver_prev ${_rep#$pkgname-}) -lt 0 ]]; then
pkgver_prev="${_rep#$pkgname-}"
fi
done
popd &> /dev/null
[[ ! "$pkgver_prev" ]] && return
echo "$P_SAVEDIR/$pkgname/$pkgname-$pkgver_prev/$file"
}
# Search if we can merge a .pac{save,new} with a current file
# usage: is_mergeable ($file)
# return: 0 on success
is_mergeable() {
(( ! MERGE )) && return 1
[[ $1 ]] || return 1
local file=$1
local ext=${2:-.pacnew}
local file_prev=$(previous_version "$file")
[[ $file_prev ]] || return 1
diff -aBbu "$file_prev" "${file}${ext}" > $tmp_file
(( $? != 1 )) && return 1
patch --dry-run -sp0 "$file" -i "$tmp_file" &> /dev/null || return 1
return 0
}
# Remove .pac* files
suppress() {
local pacfiles=(${PACOLD[@]})
[[ "$1" = "all" ]] && \
pacfiles+=("${PACNEW[@]}" "${PACSAVE[@]}" "${PACORIG[@]}")
(( ! ${#pacfiles[@]} )) && return
msg "$(gettext 'The following files have no original version.')"
echo_wrap 4 "${pacfiles[*]}"
echo
prompt "$(gettext 'Do you want to delete these files ?') $(\
yes_no 2) $(gettext '(S: no confirm)')"
local answer=$(userinput "YNS" "N")
local _opt=""
[[ "$answer" = "Y" ]] && _opt="-i"
[[ "$answer" = "Y" ]] || [[ "$answer" = "S" ]] && rm $_opt "${pacfiles[@]}"
}
# Manage .pac* file
# Called by manage()
manage_file() {
local ext=$1; shift
local _file=$1
local same loop=0
while true; do
local _msg="$ext: ${_file%$ext}"
local _prompt="Action: [E]dit, [R]eplace, [S]uppress,"
local _prompt_action="ERSCA"
diff -abBu "${_file%$ext}" "$_file" &> /dev/null && \
_msg+=" $(gettext '**same file**')" && same=1 || same=0
if is_mergeable "${_file%$ext}" "$ext"; then
_prompt+=" [M]erge,"
_prompt_action+="M"
_msg+=" $(gettext '**automerge**')"
fi
((DIFF && !same && !loop++)) && diff -abBu "${_file%$ext}" "$_file"
msg "$_msg"
# gettext "Action: [E]dit, [R]eplace, [S]uppress, [C]ontinue (default), [A]bort ?"
# gettext "Action: [E]dit, [R]eplace, [S]uppress, [M]erge, [C]ontinue (default), [A]bort ?"
# gettext "ERSCA"
# gettext "ERSCAM"
prompt "$(gettext "$_prompt [C]ontinue (default), [A]bort ?")"
local answer=$(userinput "$_prompt_action" "C")
case "$answer" in
A) return 2;; # break manage() loop
C) break;;
E) $DIFFEDITCMD "${_file%$ext}" "$_file" ;;
R) mv "$_file" "${_file%$ext}"; return 1;;
S) rm "$_file"; return 1 ;;
M) echo
msg "$(gettext 'Patch: ')"
cat "$tmp_file"
echo
prompt2 "$(gettext 'Apply ?') $(yes_no 1)"
useragrees || continue;
patch -sp0 "${_file%$ext}" -i "$tmp_file"
(( $? )) && error "$(gettext 'patch returned an error!')"
rm "$_file"
break;;
esac
done
}
# Manage list of .pac* files
manage() {
echo
local ext=$1; shift
[[ $@ ]] || return
local pacdata pacfiles i _line
readarray -t pacdata < <(stat -c "%Y %n" "$@" |
sort |
awk '{printf ("%s %s\n", strftime("%x %X",$1), substr ($0, length($1)+1))}')
readarray -t pacfiles < <(stat -c "%Y %n" "$@" | sort)
pacfiles=("${pacfiles[@]#* }")
if (( ! SEQUENTIAL )); then
while true; do
echo
list_select "${pacdata[@]}"
prompt2 "$(gettext 'Enter n° : ')"
read -e i
(( ! i )) && break
(( --i>=0 && i < ${#pacdata[@]} )) || continue
manage_file "$ext" "${pacfiles[$i]}"; local ret=$?
if (( ret == 1 )); then
unset pacdata[$i]; pacdata=("${pacdata[@]}")
unset pacfiles[$i]; pacfiles=("${pacfiles[@]}")
(( ${#pacdata[@]} )) || break
elif (( ret == 2 )); then
break
fi
done
else
i=0
for _line in "${pacdata[@]}"; do
(( i++ ))
msg "$i/${#pacdata[@]}:"
manage_file "$ext" "${pacfiles[$i-1]}"
done
fi
}
init_color
action=""
explode_args "$@"
set -- "${OPTS[@]}"
unset OPTS
while [[ $1 ]]; do
case "$1" in
-q) QUIET=1;;
-s) SEQUENTIAL=1;;
-d) DIFF=1;;
--backup|-b) action=backup;;
--confirm) NOCONFIRM=0;;
-c) action=clean;;
-*) usage $1;;
esac
shift
done
case "$action" in
backup) backup_files;;
clean) create_db; suppress all;;
*) create_db
manage .pacsave "${PACSAVE[@]}"
manage .pacnew "${PACNEW[@]}"
manage .pacorig "${PACORIG[@]}"
suppress
;;
esac
# vim: set ts=4 sw=4 noet: