]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/zipdiff
make the excludes work :P
[xonotic/xonotic.git] / misc / tools / zipdiff
1 #!/bin/sh
2
3 usage()
4 {
5         cat <<EOF
6 Usage:
7   $0 -o difference.zip -f from.zip -t to.zip
8   $0 -f from.zip -t to.zip
9 EOF
10         exit 1
11 }
12
13 output=
14 from=
15 to=
16 excludes=
17 ziptool="mkzip"
18
19 mkzip()
20 {
21         archive=$1; shift
22         sevenzipflags=-mx=9
23         zipflags=-9
24         ziplist=`mktemp`
25         find "$@" -xtype f \( -executable -or -type l \) -print > "$ziplist"
26         7za a -tzip $sevenzipflags -x@"$ziplist" "$archive" "$@" || true
27         zip         $zipflags -y   -@<"$ziplist" "$archive"      || true
28         rm -f "$ziplist"
29 }
30
31 while [ $# -gt 0 ]; do
32         o=$1
33         shift
34         case "$o" in
35                 -o)
36                         output=$1
37                         shift
38                         ;;
39                 -f)
40                         from=$1
41                         shift
42                         ;;
43                 -t)
44                         to=$1
45                         shift
46                         ;;
47                 -x)
48                         excludes="$excludes $1"
49                         shift
50                         ;;
51                 -z)
52                         ziptool=$1
53                         shift
54                         ;;
55                 *)
56                         usage
57                         ;;
58         esac
59 done
60
61 [ -n "$from" ] || usage
62 [ -n "$to" ] || usage
63
64 case "$output" in '') ;; /*) ;; *) output=`pwd`/$output ;; esac
65 case "$from" in /*) ;; *) from=`pwd`/$from ;; esac
66 case "$to" in /*) ;; *) to=`pwd`/$to ;; esac
67
68 found()
69 {
70         type=$1
71         source=$2
72         echo >&2 "$type: $source"
73         case "$type" in
74                 new|changed|deleted)
75                         echo "$source"
76                         ;;
77                 excluded)
78                         ;;
79                 deleted|*)
80                         echo >&2 " * Sorry, can't handle deletion of $source."
81                         ;;
82         esac
83 }
84
85 tempdir=`mktemp -d -t zipdiff.XXXXXX`
86
87 newline="
88 "
89 fromlist="$(zipinfo -1 "$from" | grep -v /\$)"
90 tolist="$(zipinfo -1 "$to" | grep -v /\$)"
91
92 diffit()
93 {
94         echo "$fromlist" | while IFS= read -r line; do
95                 case "$newline$tolist$newline" in
96                         *$newline$line$newline*)
97                                 ;;
98                         *)
99                                 isexcluded=false
100
101                                 for P in $excludes; do
102                                         case "$line" in
103                                                 $P)
104                                                         found excluded "$line"
105                                                         isexcluded=true
106                                                         break
107                                                         ;;
108                                         esac
109                                 done
110
111                                 if ! $isexcluded; then
112                                         found deleted "$line"
113                                 fi
114                                 ;;
115                 esac
116         done
117         echo "$tolist" | while IFS= read -r line; do
118                 case "$newline$fromlist$newline" in
119                         *$newline$line$newline*)
120                                 # check if equal
121                                 isexcluded=false
122
123                                 for P in $excludes; do
124                                         case "$line" in
125                                                 $P)
126                                                         found excluded "$line"
127                                                         isexcluded=true
128                                                         break
129                                                         ;;
130                                         esac
131                                 done
132
133                                 if ! $isexcluded; then
134                                         unzip -p "$from" "$line" > "$tempdir/v1"
135                                         unzip -p "$to" "$line" > "$tempdir/v2"
136                                         if ! diff --brief "$tempdir/v1" "$tempdir/v2" >/dev/null 2>&1; then
137                                                 found changed "$line"
138                                         fi
139                                         rm "$tempdir/v1"
140                                         rm "$tempdir/v2"
141                                 fi
142                                 ;;
143                         *)
144                                 # check if equal
145                                 isexcluded=false
146
147                                 for P in $excludes; do
148                                         case "$line" in
149                                                 $P)
150                                                         found excluded "$line"
151                                                         isexcluded=true
152                                                         break
153                                                         ;;
154                                         esac
155                                 done
156
157                                 if ! $isexcluded; then
158                                         found new "$line"
159                                 fi
160                                 ;;
161                 esac
162         done
163 }
164
165 result=`diffit`
166
167 case "$output" in
168         '')
169                 ;;
170         *)
171                 rm -f "$output"
172                 if [ -n "$result" ]; then
173                         cd "$tempdir"
174                         echo "$result" | while IFS= read -r line; do
175                                 echo >&2 "extracting $line..."
176                                 dline=./$line
177                                 mkdir -p "$tempdir/${dline%/*}"
178                                 unzip "$to" "$line"
179                         done
180                         $ziptool "$output" *
181                         cd ..
182                 fi
183                 ;;
184 esac
185
186 rm -rf "$tempdir"