]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/zipdiff
changed configs using r_hdr to use r_bloom instead for performance
[divverent/nexuiz.git] / misc / 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
18 while [ $# -gt 0 ]; do
19         o=$1
20         shift
21         case "$o" in
22                 -o)
23                         output=$1
24                         shift
25                         ;;
26                 -f)
27                         from=$1
28                         shift
29                         ;;
30                 -t)
31                         to=$1
32                         shift
33                         ;;
34                 -x)
35                         excludes="$excludes $1"
36                         shift
37                         ;;
38                 *)
39                         usage
40                         ;;
41         esac
42 done
43
44 [ -n "$from" ] || usage
45 [ -n "$to" ] || usage
46
47 found()
48 {
49         type=$1
50         source=$2
51         echo >&2 "$type: $source"
52         case "$type" in
53                 new|changed|deleted)
54                         echo "$source"
55                         ;;
56                 excluded)
57                         ;;
58                 deleted|*)
59                         echo >&2 " * Sorry, can't handle deletion of $source."
60                         ;;
61         esac
62 }
63
64 tempdir=`mktemp -d -t zipdiff.XXXXXX`
65
66 newline="
67 "
68 fromlist="$(zipinfo -1 "$from" | grep -v /\$)"
69 tolist="$(zipinfo -1 "$to" | grep -v /\$)"
70
71 diffit()
72 {
73         echo "$fromlist" | while IFS= read -r line; do
74                 case "$newline$tolist$newline" in
75                         *$newline$line$newline*)
76                                 ;;
77                         *)
78                                 found deleted "$line"
79                                 ;;
80                 esac
81         done
82         echo "$tolist" | while IFS= read -r line; do
83                 case "$newline$fromlist$newline" in
84                         *$newline$line$newline*)
85                                 # check if equal
86                                 isexcluded=false
87
88                                 for P in $excludes; do
89                                         case "$line" in
90                                                 $P)
91                                                         found excluded "$line"
92                                                         isexcluded=true
93                                                         break
94                                                         ;;
95                                         esac
96                                 done
97
98                                 if ! $isexcluded; then
99                                         unzip -p "$from" "$line" > "$tempdir/v1"
100                                         unzip -p "$to" "$line" > "$tempdir/v2"
101                                         if ! diff --brief "$tempdir/v1" "$tempdir/v2" >/dev/null 2>&1; then
102                                                 found changed "$line"
103                                         fi
104                                         rm "$tempdir/v1"
105                                         rm "$tempdir/v2"
106                                 fi
107                                 ;;
108                         *)
109                                 found new "$line"
110                                 ;;
111                 esac
112         done
113 }
114
115 result=`diffit`
116
117 case "$output" in
118         '')
119                 ;;
120         *)
121                 rm -f "$output"
122                 echo "$result" | while IFS= read -r line; do
123                         echo >&2 "extracting $line..."
124                         dline=./$line
125                         mkdir -p "$tempdir/${dline%/*}"
126                         unzip -p "$to" "$line" > "$tempdir/$line" # this may create an empty file - don't care, DP handles this as deletion
127                 done
128                 case "$output" in
129                         /*)
130                                 ;;
131                         *)
132                                 output=`pwd`/$output
133                                 ;;
134                 esac
135                 cd "$tempdir"
136                 #zip -9r "$output" .
137                 7za a -tzip -mx=9 "$output" .
138                 cd ..
139                 ;;
140 esac
141
142 rm -rf "$tempdir"