Debian Codegraph @

debian/extra/checkout-upstream

raw
1 #!/bin/bash
2 # Prepare systemd source package in current directory for testing an upstream
3 # commit, branch, or PR, without Debian patches. This replaces everything
4 # except the debian/ directory with an upstream checkout.
5 # NEVER run this in your actual packaging work directory! This is only meant
6 # for upstream CI.
7 #
8 # Author: Martin Pitt <[email protected]>
9
10 set -eu
11 test -x debian/rules
12 case "${DEB_BUILD_PROFILES:-}" in
13 *pkg.systemd.upstream*) ;;
14 *)
15 echo "Not in upstream testing mode. Do *not* run this script unless you know what you are doing." >&2
16 exit 1
17 esac
18 if [ -n "${UPSTREAM_PULL_REQUEST:-}" ]; then
19 FETCH="git fetch -fu origin refs/pull/$UPSTREAM_PULL_REQUEST/head:pr"
20 CO='git checkout pr'
21 DESC="PR #$UPSTREAM_PULL_REQUEST"
22 elif [ -n "${UPSTREAM_HEAD:-}" ]; then
23 FETCH=''
24 CO="git checkout $UPSTREAM_HEAD"
25 DESC="$UPSTREAM_HEAD"
26 else
27 echo "WARNING: $0: Neither UPSTREAM_PULL_REQUEST nor UPSTREAM_HEAD set, ignoring" >&2
28 exit 0
29 fi
30
31 DUMMY_USER_NAME="Merge dummy user"
32 DUMMY_USER_EMAIL="[email protected]"
33
34 UPSTREAM_REPO="${UPSTREAM_REPO:-https://github.com/systemd/systemd.git}"
35 BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
36
37 # Use git, if in a git repo
38 if [ -d .git ]; then
39 # make sure user.name/user.email are set, git commit wants them
40 git config --get user.name || git config user.name "$DUMMY_USER_NAME"
41 git config --get user.email || git config user.email "$DUMMY_USER_EMAIL"
42 fi
43
44 mkdir -p debian/tmp
45 (cd debian/tmp
46 git clone "${UPSTREAM_REPO}" upstream || (rm -rf upstream; sleep 60; git clone "${UPSTREAM_REPO}" upstream)
47 cd upstream
48 $FETCH
49 $CO
50 git config user.email "$DUMMY_USER_EMAIL"
51 git config user.name "$DUMMY_USER_NAME"
52 if [ -f meson.version ]; then
53 UPSTREAM_VER=$(cat meson.version)
54 else
55 UPSTREAM_VER=$(git describe | sed 's/^v//;s/-/./g')
56 fi
57 if [ -n "${UPSTREAM_PULL_REQUEST:-}" ] && [ "${UPSTREAM_REPO}" != "https://github.com/systemd/systemd-stable.git" ] && ! echo "$UPSTREAM_VER" | grep -q '\.'; then
58 git rebase main || git rebase --abort
59 fi
60 )
61 if [ -f debian/tmp/upstream/meson.version ]; then
62 UPSTREAM_VER=$(cat debian/tmp/upstream/meson.version)
63 else
64 UPSTREAM_VER=$(cd debian/tmp/upstream; git describe | sed 's/^v//;s/-/./g')
65 fi
66
67 # If we are on a stable branch, check out the corresponding packaging branch. For some settings like
68 # build dependencies it is already too late (so try to install them again), but this is mostly relevant for the dh_install files and
69 # the tests.
70 if [ -d .git ] && echo "$UPSTREAM_VER" | grep -q '\.'; then
71 rm -rf .pc
72 git reset --hard "origin/ci/v${UPSTREAM_VER%%.*}-stable"
73 apt build-dep --yes . || sudo apt build-dep --yes . || true
74 fi
75
76 if [ -n "${KEEP_DEBIAN_PATCHES:-}" ]; then
77 # set up pq branch if it does not exist
78 if [ "$BRANCH_NAME" = HEAD ]; then
79 echo "ERROR: $0 must be started from a branch when using KEEP_DEBIAN_PATCHES" >&2
80 exit 1
81 fi
82 ( gbp pq import 2> /dev/null && gbp pq switch ) || true
83 if ! git branch --contains "$BRANCH_NAME" | grep -q patch-queue/"$BRANCH_NAME"; then
84 echo "ERROR: patch-queue/$BRANCH_NAME exists but it is not rebased, please rebase it." >&2
85 exit 1
86 fi
87 fi
88
89 # switch to native instead of quilt
90 echo '3.0 (native)' > debian/source/format
91
92 # drop our patches
93 rm -rf debian/patches
94
95 # disable tests which are not for upstream
96 [ -n "${KEEP_DEBIAN_TESTS:-}" ] || sed -i '/# NOUPSTREAM/ q' debian/tests/control
97
98 # create new git commit with debian/ changes
99 if [ -d .git ] && [ -n "$(git status --short debian)" ]; then
100 git add debian
101 git commit -n -m "checkout-upstream: edit debian/ files for upstream testing"
102 fi
103
104 # clean out original upstream sources
105 find . -mindepth 1 -maxdepth 1 -name debian -o -name .git -prune -o -print0 | xargs -0n1 rm -rf
106
107 # These options changes are only needed for the upstream CI, and we don't want to pollute d/rules with them.
108 # Also we want to ensure running on an older branch (e.g.: for the systemd-stable repository) doesn't break.
109 if grep -q default-timeout-sec debian/tmp/upstream/meson_options.txt; then
110 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
111 fi
112 if grep -q log-trace debian/tmp/upstream/meson_options.txt; then
113 sed -i "s/option('log-trace', type : 'boolean', value : false/option('log-trace', type : 'boolean', value : true/" debian/tmp/upstream/meson_options.txt
114 fi
115
116 # replace with checkout
117 mv debian/tmp/upstream/* .
118 rm -rf debian/tmp
119
120 # create new git commit with upstream code
121 if [ -d .git ] && [ -n "$(git status --short)" ] ; then
122 git add .
123 git commit -n -m "checkout-upstream: replace with upstream code at version ${UPSTREAM_VER}"
124 fi
125
126 # import Debian patches which apply cleanly
127 if [ -n "${KEEP_DEBIAN_PATCHES:-}" ]; then
128 for c in $(git log "$BRANCH_NAME"..patch-queue/"$BRANCH_NAME" --format='%H' --reverse); do
129 if ! git cherry-pick "$c"; then
130 git cherry-pick --abort
131 git reset --hard
132 git clean -dxf
133 fi
134 done
135 fi
136
137 if [ -z "${UPSTREAM_KEEP_CHANGELOG:-}" ] ; then
138 # craft changelog
139 cat << EOF > debian/changelog.new
140 systemd (${UPSTREAM_VER}.0) UNRELEASED; urgency=low
141
142 * Automatic build from upstream $DESC
143
144 -- systemd test <[email protected]> $(date -R)
145
146 EOF
147 cat debian/changelog >> debian/changelog.new
148 mv debian/changelog.new debian/changelog
149
150 # create new git commit with changelog entry
151 if [ -d .git ] ; then
152 git add debian
153 git commit -n -m "checkout-upstream: update changelog to version ${UPSTREAM_VER}.0"
154 fi
155 fi