summaryrefslogtreecommitdiffstats
path: root/debian/extra/checkout-upstream
blob: 7981d7b6b16c27171f5d00d10df09c0c629f6f49 (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
#!/bin/bash
# Prepare systemd source package in current directory for testing an upstream
# commit, branch, or PR, without Debian patches. This replaces everything
# except the debian/ directory with an upstream checkout.
# NEVER run this in your actual packaging work directory! This is only meant
# for upstream CI.
#
# Author: Martin Pitt <martin.pitt@ubuntu.com>

set -eu
test -x debian/rules
case "${DEB_BUILD_PROFILES:-}" in
    *pkg.systemd.upstream*) ;;
    *)
        echo "Not in upstream testing mode. Do *not* run this script unless you know what you are doing." >&2
        exit 1
esac
if [ -n "${UPSTREAM_PULL_REQUEST:-}" ]; then
    FETCH="git fetch -fu origin refs/pull/$UPSTREAM_PULL_REQUEST/head:pr"
    CO='git checkout pr'
    DESC="PR #$UPSTREAM_PULL_REQUEST"
elif [ -n "${UPSTREAM_HEAD:-}" ]; then
    FETCH=''
    CO="git checkout $UPSTREAM_HEAD"
    DESC="$UPSTREAM_HEAD"
else
    echo "WARNING: $0: Neither UPSTREAM_PULL_REQUEST nor UPSTREAM_HEAD set, ignoring" >&2
    exit 0
fi

DUMMY_USER_NAME="Merge dummy user"
DUMMY_USER_EMAIL="invalid@example.com"

UPSTREAM_REPO="${UPSTREAM_REPO:-https://github.com/systemd/systemd.git}"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

# Use git, if in a git repo
if [ -d .git ]; then
    # make sure user.name/user.email are set, git commit wants them
    git config --get user.name || git config user.name "$DUMMY_USER_NAME"
    git config --get user.email || git config user.email "$DUMMY_USER_EMAIL"
fi

mkdir -p debian/tmp
(cd debian/tmp
 git clone "${UPSTREAM_REPO}" upstream || (rm -rf upstream; sleep 60; git clone "${UPSTREAM_REPO}" upstream)
 cd upstream
 $FETCH
 $CO
 git config user.email "$DUMMY_USER_EMAIL"
 git config user.name "$DUMMY_USER_NAME"
 if [ -f meson.version ]; then
     UPSTREAM_VER=$(cat meson.version)
 else
     UPSTREAM_VER=$(git describe | sed 's/^v//;s/-/./g')
 fi
 if  [ -n "${UPSTREAM_PULL_REQUEST:-}" ] && [ "${UPSTREAM_REPO}" != "https://github.com/systemd/systemd-stable.git" ] && ! echo "$UPSTREAM_VER" | grep -q '\.'; then
     git rebase main || git rebase --abort
 fi
)
if [ -f debian/tmp/upstream/meson.version ]; then
    UPSTREAM_VER=$(cat debian/tmp/upstream/meson.version)
else
    UPSTREAM_VER=$(cd debian/tmp/upstream; git describe | sed 's/^v//;s/-/./g')
fi

# If we are on a stable branch, check out the corresponding packaging branch. For some settings like
# build dependencies it is already too late, but this is mostly relevant for the dh_install files and
# the tests.
if [ -d .git ] && echo "$UPSTREAM_VER" | grep -q '\.'; then
    rm -rf .pc
    git reset --hard "origin/ci/v${UPSTREAM_VER%%.*}-stable"
fi

if [ -n "${KEEP_DEBIAN_PATCHES:-}" ]; then
        # set up pq branch if it does not exist
        if [ "$BRANCH_NAME" = HEAD ]; then
                echo "ERROR: $0 must be started from a branch when using KEEP_DEBIAN_PATCHES" >&2
                exit 1
        fi
        ( gbp pq import 2> /dev/null && gbp pq switch ) || true
        if ! git branch --contains "$BRANCH_NAME" | grep -q patch-queue/"$BRANCH_NAME"; then
                echo "ERROR: patch-queue/$BRANCH_NAME exists but it is not rebased, please rebase it." >&2
                exit 1
        fi
fi

# switch to native instead of quilt
echo '3.0 (native)' > debian/source/format

# drop our patches
rm -rf debian/patches

# disable tests which are not for upstream
[ -n "${KEEP_DEBIAN_TESTS:-}" ] || sed -i '/# NOUPSTREAM/ q' debian/tests/control

# create new git commit with debian/ changes
if [ -d .git ] && [ -n "$(git status --short debian)" ]; then
    git add debian
    git commit -n -m "checkout-upstream: edit debian/ files for upstream testing"
fi

# clean out original upstream sources
find . -mindepth 1 -maxdepth 1 -name debian -o -name .git -prune -o -print0 | xargs -0n1 rm -rf

# These options changes are only needed for the upstream CI, and we don't want to pollute d/rules with them.
# Also we want to ensure running on an older branch (e.g.: for the systemd-stable repository) doesn't break.
if grep -q default-timeout-sec debian/tmp/upstream/meson_options.txt; then
    sed -i "s/option('default-timeout-sec', type : 'integer', value : 90/option('default-timeout-sec', type : 'integer', value : 180/" debian/tmp/upstream/meson_options.txt
fi
if grep -q log-trace debian/tmp/upstream/meson_options.txt; then
    sed -i "s/option('log-trace', type : 'boolean', value : false/option('log-trace', type : 'boolean', value : true/" debian/tmp/upstream/meson_options.txt
fi

# replace with checkout
mv debian/tmp/upstream/* .
rm -rf debian/tmp

# create new git commit with upstream code
if [ -d .git ] && [ -n "$(git status --short)" ] ; then
    git add .
    git commit -n -m "checkout-upstream: replace with upstream code at version ${UPSTREAM_VER}"
fi

# import Debian patches which apply cleanly
if [ -n "${KEEP_DEBIAN_PATCHES:-}" ]; then
    for c in $(git log "$BRANCH_NAME"..patch-queue/"$BRANCH_NAME" --format='%H' --reverse); do
        if ! git cherry-pick "$c"; then
            git cherry-pick --abort
            git reset --hard
            git clean -dxf
        fi
    done
fi

if [ -z "${UPSTREAM_KEEP_CHANGELOG:-}" ] ; then
    # craft changelog
    cat << EOF > debian/changelog.new
systemd (${UPSTREAM_VER}.0) UNRELEASED; urgency=low

  * Automatic build from upstream $DESC

 -- systemd test <pkg-systemd-maintainers@lists.alioth.debian.org>  $(date -R)

EOF
    cat debian/changelog >> debian/changelog.new
    mv debian/changelog.new debian/changelog

    # create new git commit with changelog entry
    if [ -d .git ] ; then
        git add debian
        git commit -n -m "checkout-upstream: update changelog to version ${UPSTREAM_VER}.0"
    fi
fi