summaryrefslogtreecommitdiffstats
path: root/packaging/source/git-export-release.sh.in
blob: 51adfb73e8ba5e3bf399d4df85590fcba5c7df8b (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
#!/bin/bash
#
# Creates a release tarball directly from git

# Note that the tarball contents might not exactly match
# a particular git commit, particularly for untagged
# commits.
#
# An alternative approach would be to generate source tarballs
# using CPack. That would remove our dependency on git, but if
# Autotools is any indication it would require continuous
# maintenance.
#
# Copyright 2011 Balint Reczey <balint@balintreczey.hu>
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later

set -e -u -o pipefail

DESTDIR=.

while getopts "d:" OPTCHAR ; do
    case $OPTCHAR in
        d) DESTDIR=$OPTARG ;;
        *) printf "Unknown option %s\n" "$OPTCHAR" ;;
    esac
done
shift $(( OPTIND - 1 ))

# The remaining parameter, if set, is a package version such as 3.4.5
# or 3.4.5-67-gabcd4321
# By default the version from make-version.py + CMake is used.
PROJECT_VERSION=@PROJECT_VERSION@
if test -n "${1-}"; then
    PROJECT_VERSION="$1"
fi

TARBALL="${DESTDIR}/wireshark-${PROJECT_VERSION}.tar.xz"

# A tarball produced by 'git archive' will have the $Format string
# substituted due to the use of 'export-subst' in .gitattributes.
# shellcheck disable=SC2016
COMMIT='1fe5bce8d665c0d64e1af63545421c67596db165'

if [[ $COMMIT != \$F* ]] ; then
    # This file was extracted from a tarball produced by git archive
    # and so we are not in a git repository.
    if [[ -f "$TARBALL" ]] ; then
        # git get-tar-commit-id works outside a git repo, as it
        # only reads the first 1024 bytes of the tar extended header.
        if [[ $(git get-tar-commit-id < <(xzcat "$TARBALL")) == "$COMMIT" ]] ; then
            echo "$TARBALL commit ID matches $COMMIT."
        else
            # Allow people to make changes to a downloaded source tarball
            # and re-tar it?
            echo "WARNING: $TARBALL is not the original git archive."
        fi
        exit 0
    fi

    echo ""
    echo "The build system cannot produce a source tarball outside of a git repository."
    echo "If you are trying to build an RPM package from source extracted from a tarball,"
    echo "copy it (i.e., wireshark-${PROJECT_VERSION}.tar.xz) to"
    echo "$DESTDIR"
    echo "and run the build command again."

    exit 1
fi

STASH_ID=$(git stash create || echo "")

if [[ -n "$STASH_ID" ]] ; then
    echo "Setting commit from stash $STASH_ID"
    COMMIT="$STASH_ID"
else
    echo "Setting commit from HEAD"
    COMMIT="HEAD"
fi

if [ -f "$TARBALL" ] ; then
    printf "Found %s\\n" "$TARBALL"
    if TARBALL_ID=$(git get-tar-commit-id < <(xzcat "$TARBALL")) && COMMIT_ID=$(git rev-parse --verify "$COMMIT") ; then
        if [[ $TARBALL_ID == "$COMMIT_ID" ]] ; then
            echo "$TARBALL commit ID matches $COMMIT."
            exit 0
        fi
    fi
fi

echo "Creating $TARBALL from $COMMIT"

XZ_OPTS=
echo . | xz --threads=0 > /dev/null 2>&1 && XZ_OPTS=--threads=0

git archive --prefix="wireshark-${PROJECT_VERSION}/" "$COMMIT" | xz $XZ_OPTS > "$TARBALL"