]> wolfpit.net Git - tool/Arch-pacman/.git/blame - contrib/pacdiff.sh.in
pacdiff: add --nocolor option for the terminally challenged.
[tool/Arch-pacman/.git] / contrib / pacdiff.sh.in
CommitLineData
9efd10cd 1#!/bin/bash
dce7aa85 2# pacdiff : a simple pacnew/pacorig/pacsave updater
1dbe2781
AG
3#
4# Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
23f93118 5# Copyright (c) 2013 Pacman Development Team <pacman-dev@archlinux.org>
1dbe2781
AG
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
9781d0d6 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
1dbe2781
AG
19#
20
1038e669 21declare -r myname='pacdiff'
a77e638c 22declare -r myver='@PACKAGE_VERSION@'
1038e669 23
1dbe2781 24diffprog=${DIFFPROG:-vimdiff}
6ed7d001 25diffsearchpath=${DIFFSEARCHPATH:-/etc}
dce7aa85 26locate=0
f8ed8620 27USE_COLOR='y'
c4d8da47 28declare -a oldsaves
dce7aa85 29
f8ed8620
WG
30m4_include(../scripts/library/output_format.sh)
31
dce7aa85 32usage() {
bcde0270
JF
33 cat <<EOF
34$myname is a simple pacnew/pacorig/pacsave updater.
35
36Usage: $myname [-l]
37
38Options:
39 -l/--locate scan using locate (default: find)
b1b9ca01 40 --nocolor remove colors from output
bcde0270
JF
41
42Enviroment Variables:
43 DIFFPROG override the merge program: (default: vimdiff)
44 DIFFSEARCHPATH override the search path. (only when using find)
45 (default: /etc)
46
47Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
48
49EOF
dce7aa85
XC
50}
51
a77e638c
LF
52version() {
53 printf "%s %s\n" "$myname" "$myver"
54 echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
23f93118 55 echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
a77e638c
LF
56}
57
dce7aa85
XC
58cmd() {
59 if [ $locate -eq 1 ]; then
c4d8da47 60 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
dce7aa85 61 else
c4d8da47 62 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
dce7aa85
XC
63 fi
64}
65
05e954d2
JF
66while [[ -n "$1" ]]; do
67 case "$1" in
dce7aa85
XC
68 -l|--locate)
69 locate=1;;
b1b9ca01
JF
70 --nocolor)
71 USE_COLOR='n' ;;
a77e638c
LF
72 -V|--version)
73 version; exit 0;;
74 -h|--help)
dce7aa85 75 usage; exit 0;;
a77e638c
LF
76 *)
77 usage; exit 1;;
dce7aa85 78 esac
05e954d2
JF
79 shift
80done
dce7aa85 81
b1b9ca01
JF
82m4_include(../scripts/library/term_colors.sh)
83
dce7aa85
XC
84# see http://mywiki.wooledge.org/BashFAQ/020
85while IFS= read -u 3 -r -d '' pacfile; do
86 file="${pacfile%.pac*}"
dd3762ed
FP
87 file_type="pac${pacfile##*.pac}"
88
c4d8da47
JF
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
f8ed8620 95 msg "%s file found for %s" "$file_type" "$file"
dce7aa85 96 if [ ! -f "$file" ]; then
f8ed8620 97 warning "$file does not exist"
6a804d55 98 rm -iv "$pacfile"
dce7aa85
XC
99 continue
100 fi
495460d7
FP
101
102 if cmp -s "$pacfile" "$file"; then
f8ed8620 103 msg2 "Files are identical, removing..."
6a804d55 104 rm -v "$pacfile"
dce7aa85 105 else
9108bfe0 106 ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
dce7aa85
XC
107 while read c; do
108 case $c in
9108bfe0 109 q|Q) exit 0;;
6a804d55 110 r|R) rm -v "$pacfile"; break ;;
163ba401 111 o|O) mv -v "$pacfile" "$file"; break ;;
dce7aa85
XC
112 v|V)
113 $diffprog "$pacfile" "$file"
6a804d55 114 rm -iv "$pacfile"; break ;;
dce7aa85 115 s|S) break ;;
9108bfe0 116 *) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
dce7aa85
XC
117 esac
118 done
119 fi
120done 3< <(cmd)
121
c4d8da47
JF
122(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
123
dce7aa85
XC
124exit 0
125
126# vim: set ts=2 sw=2 noet: