]> wolfpit.net Git - tool/Arch-pacman/.git/blob - contrib/pacdiff.sh.in
pacdiff: add --nocolor option for the terminally challenged.
[tool/Arch-pacman/.git] / contrib / pacdiff.sh.in
1 #!/bin/bash
2 # pacdiff : a simple pacnew/pacorig/pacsave updater
3 #
4 # Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
5 # Copyright (c) 2013 Pacman Development Team <pacman-dev@archlinux.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 declare -r myname='pacdiff'
22 declare -r myver='@PACKAGE_VERSION@'
23
24 diffprog=${DIFFPROG:-vimdiff}
25 diffsearchpath=${DIFFSEARCHPATH:-/etc}
26 locate=0
27 USE_COLOR='y'
28 declare -a oldsaves
29
30 m4_include(../scripts/library/output_format.sh)
31
32 usage() {
33 cat <<EOF
34 $myname is a simple pacnew/pacorig/pacsave updater.
35
36 Usage: $myname [-l]
37
38 Options:
39 -l/--locate scan using locate (default: find)
40 --nocolor remove colors from output
41
42 Enviroment Variables:
43 DIFFPROG override the merge program: (default: vimdiff)
44 DIFFSEARCHPATH override the search path. (only when using find)
45 (default: /etc)
46
47 Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
48
49 EOF
50 }
51
52 version() {
53 printf "%s %s\n" "$myname" "$myver"
54 echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
55 echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
56 }
57
58 cmd() {
59 if [ $locate -eq 1 ]; then
60 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
61 else
62 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
63 fi
64 }
65
66 while [[ -n "$1" ]]; do
67 case "$1" in
68 -l|--locate)
69 locate=1;;
70 --nocolor)
71 USE_COLOR='n' ;;
72 -V|--version)
73 version; exit 0;;
74 -h|--help)
75 usage; exit 0;;
76 *)
77 usage; exit 1;;
78 esac
79 shift
80 done
81
82 m4_include(../scripts/library/term_colors.sh)
83
84 # see http://mywiki.wooledge.org/BashFAQ/020
85 while IFS= read -u 3 -r -d '' pacfile; do
86 file="${pacfile%.pac*}"
87 file_type="pac${pacfile##*.pac}"
88
89 # add matches for pacsave.N to oldsaves array, do not prompt
90 if [[ $file_type = pacsave.+([0-9]) ]]; then
91 oldsaves+=("$pacfile")
92 continue
93 fi
94
95 msg "%s file found for %s" "$file_type" "$file"
96 if [ ! -f "$file" ]; then
97 warning "$file does not exist"
98 rm -iv "$pacfile"
99 continue
100 fi
101
102 if cmp -s "$pacfile" "$file"; then
103 msg2 "Files are identical, removing..."
104 rm -v "$pacfile"
105 else
106 ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
107 while read c; do
108 case $c in
109 q|Q) exit 0;;
110 r|R) rm -v "$pacfile"; break ;;
111 o|O) mv -v "$pacfile" "$file"; break ;;
112 v|V)
113 $diffprog "$pacfile" "$file"
114 rm -iv "$pacfile"; break ;;
115 s|S) break ;;
116 *) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
117 esac
118 done
119 fi
120 done 3< <(cmd)
121
122 (( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
123
124 exit 0
125
126 # vim: set ts=2 sw=2 noet: