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