summaryrefslogtreecommitdiffstats
path: root/scripts/mergechanges.sh
blob: 7a2a68c6277f8d0cd0e9f880c00a05e02f065531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/bin/bash
##
## mergechanges -- merge Architecture: and Files: fields of a set of .changes
## Copyright 2002 Gergely Nagy <algernon@debian.org>
## Changes copyright 2002,2003 by Julian Gilbey <jdg@debian.org>
##
## $MadHouse: home/bin/mergechanges,v 1.1 2002/01/25 12:37:27 algernon Exp $
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <https://www.gnu.org/licenses/>.

set -e

PROGNAME=`basename $0`

synopsis () {
    echo "Usage: $PROGNAME [-h|--help|--version] [-d] [-S|--source] [-i|--indep] [-f] <file1> <file2> [<file> ...]"
}

usage () {
    synopsis
    cat <<EOT
  Merge the changes files <file1>, <file2>, ....  Output on stdout
  unless -f option given, in which case, output to
  <package>_<version>_multi.changes in the same directory as <file1>.
  If -i is given, only source and architecture-independent packages
  are included in the output.
  If -S is given, only the source package is included in the output.
EOT
}

version () {
    echo \
"This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
This code is copyright (C) 2002 Gergely Nagy <algernon@debian.org>
Changes copyright 2002 by Julian Gilbey <jdg@debian.org>
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later."
}

# Commandline parsing
FILE=0
DELETE=0
REMOVE_ARCHDEP=0
REMOVE_INDEP=0

while [ $# -gt 0 ]; do
    case "$1" in
	-h|--help)
	    usage
	    exit 0
	    ;;
	--version)
	    version
	    exit 0
	    ;;
	-f)
	    FILE=1
	    shift
	    ;;
	-d)
	    DELETE=1
	    shift
	    ;;
	-i|--indep)
	    REMOVE_ARCHDEP=1
	    shift
	    ;;
	-S|--source)
	    REMOVE_ARCHDEP=1
	    REMOVE_INDEP=1
	    shift
	    ;;
	-*)
	    echo "Unrecognised option $1.  Use $progname --help for help" >&2
	    exit 1
	    ;;
	*)
	    break
	    ;;
    esac
done

# Sanity check #0: Do we have enough parameters?
if [ $# -lt 2 ]; then
    echo "Not enough parameters." >&2
    synopsis >&2
    exit 1
fi

# Sanity check #1: Do the requested files exist?
for f in "$@"; do
    if ! test -r $f; then
	echo "ERROR: Cannot read $f!" >&2
	exit 1
    fi
done

# Get a (possibly multi-line) field.
get_field () {
    perl -e '
    use warnings;
    use strict;
    use autodie;

    use Dpkg::Control;

    my $field = shift;
    foreach my $file (@ARGV) {
        my $changes = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
        $changes->load($file);
        next unless defined $changes->{$field};
        print $changes->{$field};
        print "\n";
    }
    ' "$@"
}

# Extract the Architecture: field from all .changes files,
# and merge them, sorting out duplicates. Skip architectures
# other than all and source if desired.
ARCHS=$(get_field Architecture "$@" | tr ' ' '\n' | sort -u)
if test ${REMOVE_ARCHDEP} = 1; then
    ARCHS=$(echo "$ARCHS" | grep -E '^(all|source)$')
fi
if test ${REMOVE_INDEP} = 1; then
    ARCHS=$(echo "$ARCHS" | grep -vxF all)
fi
ARCHS=$(echo "$ARCHS" | tr '\n' ' ' | sed 's/ $//')

checksum_uniq() {
    local line
    local IFS=
    if test ${REMOVE_ARCHDEP} = 1 -o ${REMOVE_INDEP} = 1; then
	while read line; do
	    case "$line" in
		("")
		    # empty first line
		    echo "$line"
		    ;;
		(*.dsc|*.diff.gz|*.tar.*|*_source.buildinfo)
		    # source
		    echo "$line"
		    ;;
		(*_all.deb|*_all.udeb|*_all.buildinfo)
		    # architecture-indep
		    if test ${REMOVE_INDEP} = 0; then
			echo "$line"
		    fi
		    ;;
		(*.deb|*.udeb|*.buildinfo)
		    # architecture-specific
		    if test ${REMOVE_ARCHDEP} = 0; then
			echo "$line"
		    fi
		    ;;
		(*)
		    echo "Unrecognised file, is it architecture-dependent?" >&2
		    echo "$line" >&2
		    exit 1
		    ;;
	    esac
	done | awk '{if(arr[$NF] != 1){arr[$NF] = 1; print;}}'
    else
	awk '{if(arr[$NF] != 1){arr[$NF] = 1; print;}}'
    fi
}

# Extract & merge the Version: field from all files..
# Don't catch Version: GnuPG lines, though!
VERSION=$(get_field Version "$@" | sort -u)
SVERSION=$(echo "$VERSION" | perl -pe 's/^\d+://')
# Extract & merge the sources from all files
SOURCE=$(get_field Source "$@" | sort -u)
# Extract & merge the files from all files
FILES=$(get_field Files "$@" | checksum_uniq)
# Extract & merge the sha1 checksums from all files
SHA1S=$(get_field Checksums-Sha1 "$@" | checksum_uniq)
# Extract & merge the sha256 checksums from all files
SHA256S=$(get_field Checksums-Sha256 "$@" | checksum_uniq)
# Extract & merge the description from all files
DESCRIPTIONS=$(get_field Description "$@" | sort -u)
# Extract & merge the Formats from all files
FORMATS=$(get_field Format "$@" | sort -u)
# Extract & merge the Checksums-* field names from all files
CHECKSUMS=$(grep -h "^Checksums-.*:" "$@" | sort -u)
UNSUPCHECKSUMS="$(echo "${CHECKSUMS}" | grep -v "^Checksums-Sha\(1\|256\):" || true)"

# Sanity check #2: Versions must match
if test $(echo "${VERSION}" | wc -l) -ne 1; then
    echo "ERROR: Version numbers do not match:" >&2
    grep "^Version: [0-9]" "$@" >&2
    exit 1
fi

# Sanity check #3: Sources must match
if test $(echo "${SOURCE}" | wc -l) -ne 1; then
    echo "Error: Source packages do not match:" >&2
    grep "^Source: " "$@" >&2
    exit 1
fi

# Sanity check #4: Description for same binary must match
if test $(echo "${DESCRIPTIONS}" | sed -e 's/ \+- .*$//' | uniq -d | wc -l) -ne 0; then
    echo "Error: Descriptions do not match:" >&2
    echo "${DESCRIPTIONS}" >&2
    exit 1
fi

# Sanity check #5: Formats must match
if test $(echo "${FORMATS}" | wc -l) -ne 1; then
    if test "${FORMATS}" = "$(printf "1.7\n1.8\n")"; then
	FORMATS="1.7"
	CHECKSUMS=""
	UNSUPCHECKSUMS=""
	SHA1S=""
	SHA256S=""
    else
	echo "Error: Changes files have different Format fields:" >&2
	grep "^Format: " "$@" >&2
	exit 1
    fi
fi

# Sanity check #6: The Format must be one we understand
case "$FORMATS" in
    1.7|1.8) # Supported
        ;;
    *)
        echo "Error: Changes files use unknown Format:" >&2
        echo "${FORMATS}" >&2
        exit 1
        ;;
esac

# Sanity check #7: Unknown checksum fields
if test -n "${UNSUPCHECKSUMS}"; then
    echo "Error: Unsupported checksum fields:" >&2
    echo "${UNSUPCHECKSUMS}" >&2
    exit 1
fi

if test ${FILE} = 1; then
    DIR=`dirname "$1"`
    REDIR1="> '${DIR}/${SOURCE}_${SVERSION}_multi.changes'"
    REDIR2=">$REDIR1"
fi

# Temporary output
OUTPUT=$(mktemp --tmpdir mergechanges.tmp.XXXXXXXXXX)
DESCFILE=$(mktemp --tmpdir mergechanges.tmp.XXXXXXXXXX)
trap 'rm -f "${OUTPUT}" "${DESCFILE}"' EXIT

# Copy one of the files to ${OUTPUT}, nuking any PGP signature
if $(grep -q "BEGIN PGP SIGNED MESSAGE" "$1"); then
    perl -ne 'next if 1../^$/; next if /^$/..1; print' "$1" > ${OUTPUT}
else
    cp "$1" ${OUTPUT}
fi

# Combine the Binary: and Description: fields. This is straightforward,
# unless we want to exclude some binary packages, in which case we need
# more thought.
BINARY=$(get_field Binary "$@" | tr ' ' '\n' | sort -u)
if test ${REMOVE_ARCHDEP} = 1 && test ${REMOVE_INDEP} = 1; then
    BINARY=
    DESCRIPTIONS=
elif test ${REMOVE_ARCHDEP} = 1 || test ${REMOVE_INDEP} = 1; then
    keep_binaries=$(
        get_field Files "$@" | while read -r line; do
            file="${line##* }"
            case "$line" in
                ("")
                    # empty first line
                    echo "$line"
                    ;;
                (*.dsc|*.diff.gz|*.tar.*|*.buildinfo)
                    # source or buildinfo
                    ;;
                (*_all.deb|*_all.udeb)
                    # architecture-indep
                    package="${file%%_*}"

                    if ! echo "$BINARY" | grep -q -x -F "$package"; then
                        echo "Warning: $package not found in Binary field" >&2
                        echo "$line" >&2
                    fi

                    if test ${REMOVE_INDEP} != 1; then
                        echo "$package"
                    fi
                    ;;
                (*.deb|*.udeb)
                    # architecture-specific
                    package="${file%%_*}"

                    if ! echo "$BINARY" | grep -q -x -F "$package"; then
                        echo "Warning: $package not found in Binary field" >&2
                        echo "$line" >&2
                    fi

                    if test ${REMOVE_ARCHDEP} != 1; then
                        echo "$package"
                    fi
                    ;;
                (*)
                    echo "Unrecognised file, is it architecture-dependent?" >&2
                    echo "$line" >&2
                    exit 1
                    ;;
            esac
        done \
    | tr '\n' ' ')

    BINARY=$(
        echo "$BINARY" |
        while read -r line; do
            if echo " $keep_binaries" | grep -q -F " $line "; then
                echo "$line";
            fi
        done
    )
    DESCRIPTIONS=$(
        echo "$DESCRIPTIONS" |
        while read -r line; do
            package="${line%% *}"
            if echo " $keep_binaries" | grep -q -F " $package "; then
                echo "$line";
            fi
        done
    )
fi
BINARY=$(echo "$BINARY" | tr '\n' ' ' | sed 's/ $//')

if test -n "${DESCRIPTIONS}"; then
    printf "Description:" > "${DESCFILE}"
    echo "${DESCRIPTIONS}" | sed -e 's/^/ /' >> "${DESCFILE}"
fi

if [ -n "$BINARY" ]; then
    BINARY="Binary: $BINARY\\n"
fi

# Modify the output to be the merged version:
# * Replace the Architecture: and Binary: fields
# * Nuke the value of Checksums-*: and Files:
# * Insert the Description: field before the Changes: field
#
# We print Binary directly before Source instead of directly replacing
# Binary, because with dpkg 1.19.3, if the first .changes file is
# source-only, it won't have a Binary field at all.
eval "awk -- '/^[^ ]/{ deleting=0 }
    /^ /{
        if (!deleting) {
            print
        }
        next
    }
    /^Architecture: /{printf \"%s ${ARCHS}\\n\", \$1; deleting=1; next}
    /^Source: /{printf \"${BINARY}\"; print; next}
    /^Binary: /{deleting=1; next}
    /^Changes:/{
        field=\$0
        while ((getline < \"${DESCFILE}\") > 0) {
            print
        }
        printf \"%s\\n\", field
        next
    }
    /^Format: /{ printf \"%s ${FORMATS}\\n\", \$1; deleting=1; next}
    /^(Checksums-.*|Files|Description):/{ deleting=1; next }
    { print }' \
    ${OUTPUT} ${REDIR1}"

# Voodoo magic to get the merged file and checksum lists into the output
if test -n "${SHA1S}"; then
    eval "printf 'Checksums-Sha1:' ${REDIR2}"
    eval "echo '${SHA1S}' | sed -e 's/^/ /' ${REDIR2}"
fi
if test -n "${SHA256S}"; then
    eval "printf 'Checksums-Sha256:' ${REDIR2}"
    eval "echo '${SHA256S}' | sed -e 's/^/ /' ${REDIR2}"
fi
eval "printf 'Files:' ${REDIR2}"
eval "echo '${FILES}' | sed -e 's/^/ /' ${REDIR2}"

if test ${DELETE} = 1; then
    rm "$@"
fi

exit 0