Debian Codegraph @

scripts/jdg-generate-git-snapshot

raw
1 #!/bin/bash
2
3 if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then
4 set -x
5 fi
6 set -e
7 set -u
8
9 if [ -r /etc/jenkins/debian_glue ] ; then
10 . /etc/jenkins/debian_glue
11 fi
12
13 [ -n "${DEBEMAIL:-}" ] || DEBEMAIL="jenkins-debian-glue Autobuilder <jenkins@`hostname -f`>"
14 export DEBEMAIL
15
16 [ -n "${SOURCE_DIRECTORY:-}" ] || SOURCE_DIRECTORY='source' # where checkout of sources resides
17
18 if [ ! -d "${SOURCE_DIRECTORY}" ] ; then
19 echo "Please run the script in the jenkins workspace." >&2
20 echo 'NOTE: Make sure that "Source Code Management -> Git -> Additional Behaviours -> Check out to a sub-directory" is set to "source"' >&2
21 exit 1
22 fi
23
24 if [ -z "${BUILD_NUMBER:-}" ] ; then
25 echo "No BUILD_NUMBER defined, please run it in jenkins." >&2
26 exit 1
27 fi
28
29 if [ -z "${GIT_COMMIT:-}" ] ; then
30 echo "No GIT_COMMIT defined, please run it with git-plugin in jenkins. ">&2
31 exit 1
32 fi
33
34 # since git-buildpackage >=0.6.24 only the gbp command is available
35 if [ -x "$(which gbp)" ] ; then
36 GBP_DCH="gbp dch"
37 GBP_BUILDPACKAGE="gbp buildpackage"
38 else
39 GBP_DCH="git-dch"
40 GBP_BUILDPACKAGE="git-buildpackage"
41 fi
42
43 JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}')
44 if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then
45 echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***"
46 fi
47
48 if [ -n "${DIST:-}" -a -z "${KEEP_SOURCE_CHANGES:-}" ]; then
49 echo "*** Warning: DIST is set but KEEP_SOURCE_CHANGES is not, this doesn't make sense. ***"
50 echo "*** Please consider setting KEEP_SOURCE_CHANGES=true when setting DIST. ***"
51
52 if ! ${GBP_DCH} --help 2>&1 | grep -q -- --distribution ; then
53 echo "Error: ${GBP_DCH} doesn't support the --distribution option. Upgrade git-buildpackage to version 0.6.7 or newer." >&2
54 exit 1
55 fi
56 fi
57
58 gbp_opts() {
59 # support overriding git-buildpackage options
60 if [ -n "${GBP_OPTS:-}" ] ; then
61 echo "Found environment variable GBP_OPTS, set to ${GBP_OPTS}"
62 else
63 GBP_OPTS=" -nc --git-force-create --git-ignore-new --git-ignore-branch -S -us -uc --git-verbose --git-builder=/bin/true --git-cleaner=/bin/true --git-export-dir= "
64 echo "Using git-buildpackage default options provided by jenkins-debian-glue"
65 fi
66 }
67
68 version_information() {
69 # required for dpkg-parsechangelog
70 if ! [ -r debian/changelog ] ; then
71 echo "Error: debian/changelog could not be read. Is this really a Debian package?" >&2
72 return 1
73 fi
74
75 # retrieve and adjust information
76 ORIG_VERSION=$(dpkg-parsechangelog --count 1 | awk '/^Version/ {print $2}')
77 DISTRIBUTION=$(dpkg-parsechangelog --count 1 | awk '/^Distribution/ {print $2}')
78 APPLY_VERSION_WORKAROUND=false
79
80 if [ "${USE_ORIG_VERSION:-}" = "true" ] ; then
81 echo "*** USE_ORIG_VERSION is set to 'true', keeping original version [${ORIG_VERSION}] from changelog ***"
82 VERSION_STRING="${ORIG_VERSION}"
83 else
84 # we want to get a version string like
85 # $ORIG_VERSION+0~$TIMESTAMP.$BUILD_NUMBER~1.$GIT_ID
86 # so the version is always increasing over time, no matter what
87 INCREASED_VERSION=$(jdg-increase-version-number $ORIG_VERSION)
88 if [ -n "${TIMESTAMP_FORMAT:-}" ] ; then
89 echo "*** Using custom timestamp format as specified by TIMESTAMP_FORMAT ***"
90 TIMESTAMP="$(date -u +"$TIMESTAMP_FORMAT")"
91 else
92 TIMESTAMP="$(date -u +%Y%m%d%H%M%S)"
93 fi
94 BUILD_VERSION="${TIMESTAMP}.${BUILD_NUMBER}" # git-dch appends something like ~1.gbp5f433e then
95
96 # we do NOT raise the version number, if we detect an unreleased version,
97 # otherwise the released version would be older than our snapshot builds
98 if [ "$DISTRIBUTION" = "UNRELEASED" ] && dpkg --compare-versions "$ORIG_VERSION" lt "$INCREASED_VERSION" ; then
99 echo "*** Not increasing version number as distribution is set to UNRELEASED ***"
100 INCREASED_VERSION="$ORIG_VERSION"
101
102 if [ "${UNRELEASED_APPEND_COMMIT:-}" = "true" ]; then
103 # However, as we will use plain 'dch' for this, we still want to have
104 # the (short) GIT commit id inside the version...
105 echo "*** UNRELEASED_APPEND_COMMIT is set to 'true', manually appending GIT commit to version number ***"
106 BUILD_VERSION="${BUILD_VERSION}~${GIT_COMMIT:0:7}"
107 fi
108 APPLY_VERSION_WORKAROUND=true
109 fi
110
111 VERSION_STRING="${INCREASED_VERSION}~${BUILD_VERSION}"
112 fi
113
114 if [ -n "${distribution:-}" ] ; then
115 echo "Distribution variable found. Adding distribution specific version."
116 VERSION_STRING="${VERSION_STRING}+${distribution//-/_}"
117 fi
118
119 echo "*** Version string set to $VERSION_STRING ***"
120 }
121
122 # support overriding git-dch options
123 dch_opts() {
124 if [ -n "${DCH_OPTS:-}" ] ; then
125 echo "Found environment variable DCH_OPTS, set to ${DCH_OPTS}"
126 else
127 DCH_OPTS="-S --multimaint-merge --ignore-branch"
128 echo "Using default ${GBP_DCH} options (${DCH_OPTS})"
129 fi
130 DCH_COMBINED_OPTS="${DCH_OPTS}"
131
132 # support overriding extra options for git-dch
133 if [ -n "${DCH_EXTRA_OPTS:-}" ] ; then
134 echo "Found environment variable DCH_EXTRA_OPTS, set to ${DCH_EXTRA_OPTS}"
135 else
136 DCH_EXTRA_OPTS="--new-version=${VERSION_STRING}"
137 if [ -n "${DIST:-}" ]; then
138 DCH_EXTRA_OPTS="${DCH_EXTRA_OPTS} --distribution ${DIST} --force-distribution"
139 fi
140 echo "Using extra ${GBP_DCH} options (${DCH_EXTRA_OPTS})"
141 fi
142 DCH_COMBINED_OPTS="${DCH_COMBINED_OPTS} ${DCH_EXTRA_OPTS}"
143
144 if [ -n "${DCH_LOG_RESTRICTION:-}" ] ; then
145 echo "Found environment variable DCH_LOG_RESTRICTION, set to ${DCH_LOG_RESTRICTION}"
146 DCH_COMBINED_OPTS="${DCH_COMBINED_OPTS} ${DCH_LOG_RESTRICTION}"
147 fi
148 }
149
150 # support overrides from gbp.conf
151 gbp_conf() {
152
153 if [ "${IGNORE_GBP_CONF:-}" = "true" ] ; then
154 echo "Skipping gbp_conf execution as requested via IGNORE_GBP_CONF ***"
155 return 0
156 fi
157
158 # List of config files in order
159 gbp_conf_files=(
160 /etc/git-buildpackage/gbp.conf
161 ~/.gbp.conf
162 .gbp.conf
163 debian/gbp.conf
164 .git/gbp.conf
165 )
166
167 # Defaults
168 UPSTREAM_BRANCH="upstream"
169 DEBIAN_BRANCH="debian"
170 PRISTINE_TAR="false"
171
172 for gbp_conf_file in "${gbp_conf_files[@]}"; do
173 if [ -f ${gbp_conf_file} ]; then
174 local value;
175 value=$(git config --file ${gbp_conf_file} --get DEFAULT.upstream-branch 2>&- || true)
176 if [ -n "${value:-}" ]; then
177 UPSTREAM_BRANCH=${value}
178 fi
179
180 value=$(git config --file ${gbp_conf_file} --get DEFAULT.debian-branch 2>&- || true)
181 if [ -n "${value:-}" ]; then
182 DEBIAN_BRANCH=${value}
183 fi
184
185 value=$(git config --file ${gbp_conf_file} --bool --get DEFAULT.pristine-tar 2>&- || true)
186 if [ -n "${value:-}" ]; then
187 PRISTINE_TAR=${value}
188 fi
189 fi
190 done
191 }
192
193 create_local_branch() {
194 [ -n "${1:-}" ] || return 1
195
196 local BRANCH="$1"
197
198 if git branch -a | grep -q "remotes/origin/${BRANCH}"'$' ; then
199 git branch -D "${BRANCH}" || true
200 git branch "${BRANCH}" "remotes/origin/${BRANCH}"
201 else
202 echo "NOTE: branch $BRANCH does not exist, ignoring request to checkout therefore."
203 fi
204 }
205
206 git_dch_auto() {
207 if $APPLY_VERSION_WORKAROUND ; then
208 echo "Applying version workaround workaround"
209 dch -b --distribution=UNRELEASED --newversion=$VERSION_STRING -- \
210 "SNAPSHOT autobuild for unreleased $ORIG_VERSION via jenkins-debian-glue."
211 else
212 ${GBP_DCH} --auto $DCH_COMBINED_OPTS ${DCH_CHANGELOG_FILE:-}
213 fi
214 }
215
216 identify_latest_change() {
217 # debian/changelog might be a symlink (e.g. because debian points to
218 # pkg/debian), so make sure we don't access a non-existing file
219 # by calling readlink
220 git checkout -- $(readlink -f debian/changelog)
221
222 local OLD_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')
223
224 local last_tag=$(git describe $(git rev-list --tags='[^u]*' --max-count=1 HEAD))
225 local last_merge=$(git describe $(git rev-list --all --merges --max-count=1 HEAD))
226 local since=${last_tag}
227
228 if [ -n "$last_merge" ] ; then
229 local merge_date=$(git log ${last_merge} --pretty="format:%at" -1)
230 local tag_date=$(git log ${last_tag} --pretty="format:%at" -1)
231 if [ ${merge_date} -gt ${tag_date} ] ; then
232 local since=${last_merge}
233 fi
234 fi
235
236 echo "Latest tag [${last_tag:-}] / merge [${last_merge:-}] seems to be $since"
237 ${GBP_DCH} -s "${since}" $DCH_COMBINED_OPTS ${DCH_CHANGELOG_FILE:-}
238
239 local NEW_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')
240
241 if dpkg --compare-versions "$NEW_VERSION" lt "$OLD_VERSION" ; then
242 echo "Calculated version is older than last release, falling back to auto mode."
243 # debian/changelog might be a symlink (e.g. because debian points to
244 # pkg/debian), so make sure we don't access a non-existing file
245 # by calling readlink
246 git checkout -- $(readlink -f debian/changelog)
247
248 git_dch_auto
249 fi
250 }
251
252 branch_checkout() {
253 # git-dch and git-buildpackage refuse to operate on a single git
254 # commit, so instead create a temporary branch to work on
255 random_branch="jenkins-debian-glue-buildbranch$RANDOM"
256
257 if [ "${SKIP_BRANCH_AND_TAG_HANDLING:-}" = "true" ] ; then
258 echo "*** Skipping branch/tag handling as requested via SKIP_BRANCH_AND_TAG_HANDLING ***"
259
260 echo "Making sure that random_branch $random_branch does not exist"
261 git branch -D "$random_branch" || true
262
263 echo "Checking out branch $random_branch based on git commit $GIT_COMMIT"
264 git checkout -b "$random_branch" "$GIT_COMMIT"
265 elif [ -n "${GERRIT_PATCHSET_REVISION:-}" ] ; then
266 echo "*** We seem to be building for Gerrit ***"
267
268 echo "Making sure that random_branch $random_branch does not exist"
269 git branch -D "$random_branch" || true
270
271 echo "*** Fetching Gerrit patchsets/commits from ${GIT_URL} ***"
272 git fetch --tags --progress ${GIT_URL} +refs/changes/*:refs/remotes/origin/*
273
274 echo "Checking out branch $random_branch based on Gerrit patchset revision ${GERRIT_PATCHSET_REVISION} ***"
275 git checkout -b "$random_branch" "$GERRIT_PATCHSET_REVISION"
276 elif [ -n "${tag:-}" ] && [ "${tag:-}" != "none" ] ; then
277 echo "Making sure that random_branch $random_branch does not exist"
278 git branch -D "$random_branch" || true
279
280 echo "Checking out branch $random_branch based on tag $tag"
281 git checkout -b "$random_branch" "$tag"
282 elif [ -n "${branch:-}" ] ; then
283 # prepend "origin" when checking out a branch, otherwise we might be
284 # checking out a branch which doesn't exist locally yet, failing with:
285 # | fatal: git checkout: updating paths is incompatible with switching branches.
286 # | Did you intend to checkout '$BRANCH' which can not be resolved as commit?
287 local checkout_branch
288
289 case "$branch" in
290 # rewrite refs/heads/FOO to refs/remotes/origin/FOO, otherwise it fails;
291 # the refs/heads/FOO is something e.g. github pushes through its API
292 refs/heads/*) checkout_branch="${branch/refs\/heads/refs/remotes/origin}" ;;
293
294 # keep as it is when receiving origin/* already
295 origin/*) checkout_branch="$branch" ;;
296
297 # prepend "origin/" otherwise
298 *) checkout_branch="origin/${branch}" ;;
299 esac
300
301 echo "Making sure that random_branch $random_branch does not exist"
302 git branch -D "$random_branch" || true
303
304 echo "Checking out branch $random_branch based on branch $checkout_branch"
305 git checkout -b "$random_branch" "$checkout_branch"
306 else
307 echo "Making sure that random_branch $random_branch does not exist"
308 git branch -D "$random_branch" || true
309
310 echo "Checking out branch $random_branch based on commit $GIT_COMMIT"
311 git checkout -b "$random_branch" "$GIT_COMMIT"
312 fi
313 }
314
315 changelog_generation() {
316 # Allow skipping of changelog creation, e.g. to build a branch as-is
317 if [ "${SKIP_DCH:-}" = "true" ] ; then
318 echo "*** SKIP_DCH is set, not modifying debian/changelog therefore ***"
319 else
320 if [ -n "${tag:-}" ] && [ "${tag:-}" != "none" ] ; then
321 echo "Tag parameter found (and not set to 'none'), not touching the changelog."
322 elif [ "${1:-}" = "identify" ] ; then
323 echo "Trying to identify latest tag / merge..."
324
325 if ! git describe $(git rev-list --tags='[^u]*' --max-count=1 HEAD) >/dev/null ; then
326 echo "Failed to identify latest change, falling back to auto mode."
327 git_dch_auto
328 else
329 identify_latest_change
330 fi
331 elif [ -r debian/gbp.conf ] ; then
332 echo "Found debian/gbp.conf, using ${GBP_DCH} with auto mode."
333 git_dch_auto
334 else
335 echo "Using ${GBP_DCH} with auto mode."
336 git_dch_auto
337 fi
338
339 # get rid of "UNRELEASED" distribution header, only
340 # necessary if the debian/changelog was modified
341 if [ -z "${tag:-}" ]; then
342 local debchange_opts
343 if [ -n "${DIST:-}" ]; then
344 debchange_opts="--distribution ${DIST} --force-distribution"
345 fi
346 debchange --release ${debchange_opts:-} ""
347 fi
348 fi
349 }
350
351 git_cleanup() {
352 if [ -n "${SKIP_GIT_CLEANUP:-}" ] ; then
353 echo "*** Skipping git_cleanup as requested via SKIP_GIT_CLEANUP ***"
354 return 0
355 fi
356
357 echo "*** Cleaning git repository to make sure there are no modified files ***"
358 echo "*** Note: You can skip this step by setting SKIP_GIT_CLEANUP=true ***"
359 git clean -fxd
360 git reset --hard HEAD
361 }
362
363 quilt_cleanup() {
364 if [ -n "${SKIP_QUILT_CLEANUP:-}" ] ; then
365 echo "*** Skipping quilt_cleanup as requested via SKIP_QUILT_CLEANUP ***"
366 return 0
367 fi
368
369 echo "*** Unapplying all possibly applied quilt patches and removing quilt directory ***"
370 echo "*** Note: You can skip this step by setting SKIP_QUILT_CLEANUP=true ***"
371 quilt pop -a || true
372 rm -rf .pc
373 }
374
375 identify_source_build_opts() {
376 # unset by default
377 DBP_OPTS=""
378
379 if [ -r "debian/source/format" ] && ! grep -q '^1\.0' debian/source/format ; then
380 echo "*** Found dpkg source format != 1.0, nothing to do ****"
381 return 0
382 fi
383
384 if [ -r "debian/source/options" ] ; then
385 if ! grep -q '^[[:space:]]*tar-ignore' "debian/source/options" ; then
386 echo "*** Enabling '-I' option as tar-ignore option not present in debian/source/options ***"
387 DBP_OPTS+=" -I"
388 fi
389
390 # note: we shouldn't match on 'extend-diff-ignore'
391 if ! grep -q '^[[:space:]]*diff-ignore' "debian/source/options" ; then
392 echo "*** Enabling '-i' option as diff-ignore option not present in debian/source/options ***"
393 DBP_OPTS+=" -i"
394 fi
395 else
396 echo "*** Enabling '-i -I' options as building for dpkg source format 1.0 ***"
397 DBP_OPTS="-i -I"
398 fi
399 }
400
401 source_format_opts() {
402 if ! [ -f "debian/source/format" ] ; then
403 echo '*** WARNING: Source format is not specified in debian/source/format! ***'
404 return 0
405 fi
406
407 local SRC_FORMAT="$(cat debian/source/format)"
408
409 case "$SRC_FORMAT" in
410 '3.0 (quilt)')
411 echo "*** Source format detected as 3.0 (quilt) ***";
412 DBP_EXTRA_OPTS="${DBP_EXTRA_OPTS:-} --source-option=--unapply-patches"
413 ;;
414 '3.0 (git)')
415 echo "*** Source format detected as 3.0 (git) ***"
416 PRISTINE_TAR=false
417 SKIP_QUILT_CLEANUP=true
418 IGNORE_GIT_BUILDPACKAGE=true
419 ;;
420 '3.0 (native)')
421 echo "*** Source format detected as 3.0 (native) ***"
422 ;;
423 '3.0')
424 echo "*** Source format detected as 3.0 ***"
425 ;;
426 '1.0')
427 echo "*** Source format detected as 1.0 ***"
428 SKIP_QUILT_CLEANUP=true
429 ;;
430 *)
431 echo "*** WARNING: invalid or unknown source format: ${SRC_FORMAT} ***"
432 ;;
433 esac
434 }
435
436 # main execution
437 echo "*** source package build phase ***"
438
439 if [ -n "${SKIP_PRE_CLEANUP:-}" ] ; then
440 echo '*** SKIP_PRE_CLEANUP is set ***'
441 else
442 rm -f ./* || true
443 fi
444
445 if [ -n "${PRE_SOURCE_HOOK:-}" ] ; then
446 echo "*** Found environment variable PRE_SOURCE_HOOK, set to ${PRE_SOURCE_HOOK:-} ***"
447 sh ${PRE_SOURCE_HOOK:-}
448 fi
449
450 cd "$SOURCE_DIRECTORY"
451
452 git_cleanup
453 source_format_opts
454 identify_source_build_opts
455
456 # Only if we are using git-buildpackage...
457 if [ "${IGNORE_GIT_BUILDPACKAGE:-}" != "true" ] ; then
458 # gbp.conf handling
459 gbp_conf
460
461 # make sure common branches are available for git-buildpackage
462 create_local_branch ${UPSTREAM_BRANCH}
463 create_local_branch ${DEBIAN_BRANCH}
464 if [ ${PRISTINE_TAR} = 'true' ]; then
465 create_local_branch pristine-tar
466 fi
467 fi
468
469 # check out the according branch/tag with help of $random_branch
470 branch_checkout
471
472 if [ -n "${PRE_DCH_HOOK:-}" ] ; then
473 echo "*** Found environment variable PRE_DCH_HOOK, set to ${PRE_DCH_HOOK:-} ***"
474 sh ${PRE_DCH_HOOK:-}
475 fi
476
477 # retrieve version information *after* checking out the according branch/tag,
478 # otherwise version number might result in something different than expected
479 version_information
480
481 # handling of debian/changelog
482 dch_opts
483 changelog_generation
484
485 # prepare orig.tar.gz using pristine-tar, but without actually building the source package
486 if [ "${IGNORE_GIT_BUILDPACKAGE:-}" = "true" ] ; then
487 echo "Skipping git-buildpackage execution as requested via IGNORE_GIT_BUILDPACKAGE ***"
488 else
489 gbp_opts # set $GBP_OPTS
490 ${GBP_BUILDPACKAGE} ${GBP_OPTS:-}
491 fi
492
493 quilt_cleanup
494
495 # build source package, run before switching back to previous branch
496 # to get the actual requested version
497 dpkg-buildpackage -uc -us -nc -d -S ${DBP_OPTS:-} ${DBP_EXTRA_OPTS:-}
498
499 if [ -n "${KEY_ID:-}" ] ; then
500 echo "*** Found environment variable KEY_ID, set to ${KEY_ID:-}, signing source package ***"
501
502 if ! [ -x "$(which debsign)" ] ; then
503 echo "Error: debsign not available, please make sure the devscripts package is installed." >&2
504 exit 1
505 fi
506
507 debsign -k"${KEY_ID:-}" ../*_source.changes
508 fi
509
510 if [ -n "${KEEP_SOURCE_CHANGES:-}" ]; then
511 echo "*** Not removing ../*_source.changes because KEEP_SOURCE_CHANGES is set ***"
512 else
513 echo "*** Removing ../*_source.changes because KEEP_SOURCE_CHANGES is not set ***"
514 # get rid of the *_source.changes files generated by dpkg-buildpackage,
515 # to avoid failures in a later stage when copying *all* files around
516 rm -f ../*_source.changes
517 fi
518
519 # revert to original debian/changelog to avoid merge conflicts
520 git checkout -- $(readlink -f debian/changelog)
521
522 # switch back to previous "branch" before removing the tmp branch
523 if ! git checkout -f "${GIT_BRANCH}"; then
524 # we were probably detached
525 git checkout -f "${GIT_COMMIT}"
526 fi
527 git branch -D "$random_branch"
528
529 # vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2