This file is indexed.

/usr/bin/generate-git-snapshot is in jenkins-debian-glue 0.18.4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/bash

if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then
  set -x
fi
set -e
set -u

if [ -r /etc/jenkins/debian_glue ] ; then
  . /etc/jenkins/debian_glue
fi

[ -n "${DEBEMAIL:-}" ] || DEBEMAIL="jenkins-debian-glue Autobuilder <jenkins@`hostname -f`>"
export DEBEMAIL

[ -n "${SOURCE_DIRECTORY:-}" ] || SOURCE_DIRECTORY='source' # where checkout of sources resides

if [ ! -d "${SOURCE_DIRECTORY}" ] ; then
  echo "Please run the script in the jenkins workspace." >&2
  echo 'NOTE: Make sure that "Source Code Management -> Git -> Additional Behaviours -> Check out to a sub-directory" is set to "source"' >&2
  exit 1
fi

if [ -z "${BUILD_NUMBER:-}" ] ; then
  echo "No BUILD_NUMBER defined, please run it in jenkins." >&2
  exit 1
fi

if [ -z "${GIT_COMMIT:-}" ] ; then
  echo "No GIT_COMMIT defined, please run it with git-plugin in jenkins. ">&2
  exit 1
fi

# since git-buildpackage >=0.6.24 only the gbp command is available
if [ -x "$(which gbp)" ] ; then
  GBP_DCH="gbp dch"
  GBP_BUILDPACKAGE="gbp buildpackage"
else
  GBP_DCH="git-dch"
  GBP_BUILDPACKAGE="git-buildpackage"
fi

JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}')
if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then
  echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***"
fi

if [ -n "${DIST:-}" -a -z "${KEEP_SOURCE_CHANGES:-}" ]; then
  echo "*** Warning: DIST is set but KEEP_SOURCE_CHANGES is not, this doesn't make sense. ***"
  echo "*** Please consider setting KEEP_SOURCE_CHANGES=true when setting DIST. ***"

  if ! ${GBP_DCH} --help 2>&1 | grep -q -- --distribution ; then
    echo "Error: ${GBP_DCH} doesn't support the --distribution option. Upgrade git-buildpackage to version 0.6.7 or newer." >&2
    exit 1
  fi
fi

gbp_opts() {
  # support overriding git-buildpackage options
  if [ -n "${GBP_OPTS:-}" ] ; then
    echo "Found environment variable GBP_OPTS, set to ${GBP_OPTS}"
  else
    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= "
    echo "Using git-buildpackage default options provided by jenkins-debian-glue"
  fi
}

version_information() {
  # required for dpkg-parsechangelog
  if ! [ -r debian/changelog ] ; then
    echo "Error: debian/changelog could not be read. Is this really a Debian package?" >&2
    return 1
  fi

  # retrieve and adjust information
  ORIG_VERSION=$(dpkg-parsechangelog --count 1 | awk '/^Version/ {print $2}')
  DISTRIBUTION=$(dpkg-parsechangelog --count 1 | awk '/^Distribution/ {print $2}')
  APPLY_VERSION_WORKAROUND=false

  if [ "${USE_ORIG_VERSION:-}" = "true" ] ; then
    echo "*** USE_ORIG_VERSION is set to 'true', keeping original version [${ORIG_VERSION}] from changelog ***"
    VERSION_STRING="${ORIG_VERSION}"
  else
    # we want to get a version string like
    # $ORIG_VERSION+0~$TIMESTAMP.$BUILD_NUMBER~1.$GIT_ID
    # so the version is always increasing over time, no matter what
    INCREASED_VERSION=$(increase-version-number $ORIG_VERSION)
    if [ -n "${TIMESTAMP_FORMAT:-}" ] ; then
      echo "*** Using custom timestamp format as specified by TIMESTAMP_FORMAT ***"
      TIMESTAMP="$(date -u +"$TIMESTAMP_FORMAT")"
    else
      TIMESTAMP="$(date -u +%Y%m%d%H%M%S)"
    fi
    BUILD_VERSION="${TIMESTAMP}.${BUILD_NUMBER}" # git-dch appends something like ~1.gbp5f433e then

    # we do NOT raise the version number, if we detect an unreleased version,
    # otherwise the released version would be older than our snapshot builds
    if [ "$DISTRIBUTION" = "UNRELEASED" ] && dpkg --compare-versions "$ORIG_VERSION" lt "$INCREASED_VERSION" ; then
      echo "*** Not increasing version number as distribution is set to UNRELEASED ***"
      INCREASED_VERSION="$ORIG_VERSION"

      if [ "${UNRELEASED_APPEND_COMMIT:-}" = "true" ]; then
        # However, as we will use plain 'dch' for this, we still want to have
        # the (short) GIT commit id inside the version...
        echo "*** UNRELEASED_APPEND_COMMIT is set to 'true', manually appending GIT commit to version number ***"
        BUILD_VERSION="${BUILD_VERSION}~${GIT_COMMIT:0:7}"
      fi
      APPLY_VERSION_WORKAROUND=true
    fi

    VERSION_STRING="${INCREASED_VERSION}~${BUILD_VERSION}"
  fi

  if [ -n "${distribution:-}" ] ; then
    echo "Distribution variable found. Adding distribution specific version."
    VERSION_STRING="${VERSION_STRING}+${distribution//-/_}"
  fi

  echo "*** Version string set to $VERSION_STRING ***"
}

# support overriding git-dch options
dch_opts() {
  if [ -n "${DCH_OPTS:-}" ] ; then
    echo "Found environment variable DCH_OPTS, set to ${DCH_OPTS}"
  else
    DCH_OPTS="-S --multimaint-merge --ignore-branch"
    echo "Using default ${GBP_DCH} options (${DCH_OPTS})"
  fi
  DCH_COMBINED_OPTS="${DCH_OPTS}"

  # support overriding extra options for git-dch
  if [ -n "${DCH_EXTRA_OPTS:-}" ] ; then
    echo "Found environment variable DCH_EXTRA_OPTS, set to ${DCH_EXTRA_OPTS}"
  else
    DCH_EXTRA_OPTS="--new-version=${VERSION_STRING}"
    if [ -n "${DIST:-}" ]; then
      DCH_EXTRA_OPTS="${DCH_EXTRA_OPTS} --distribution ${DIST} --force-distribution"
    fi
    echo "Using extra ${GBP_DCH} options (${DCH_EXTRA_OPTS})"
  fi
  DCH_COMBINED_OPTS="${DCH_COMBINED_OPTS} ${DCH_EXTRA_OPTS}"

  if [ -n "${DCH_LOG_RESTRICTION:-}" ] ; then
    echo "Found environment variable DCH_LOG_RESTRICTION, set to ${DCH_LOG_RESTRICTION}"
    DCH_COMBINED_OPTS="${DCH_COMBINED_OPTS} ${DCH_LOG_RESTRICTION}"
  fi
}

# support overrides from gbp.conf
gbp_conf() {

  if [ "${IGNORE_GBP_CONF:-}" = "true" ] ; then
    echo "Skipping gbp_conf execution as requested via IGNORE_GBP_CONF ***"
    return 0
  fi

  # List of config files in order
  gbp_conf_files=(
  /etc/git-buildpackage/gbp.conf
  ~/.gbp.conf
  .gbp.conf
  debian/gbp.conf
  .git/gbp.conf
  )

  # Defaults
  UPSTREAM_BRANCH="upstream"
  DEBIAN_BRANCH="debian"
  PRISTINE_TAR="false"

  for gbp_conf_file in "${gbp_conf_files[@]}"; do
    if [ -f ${gbp_conf_file} ]; then
      local value;
      value=$(git config --file ${gbp_conf_file} --get DEFAULT.upstream-branch 2>&- || true)
      if [ -n "${value:-}" ]; then
        UPSTREAM_BRANCH=${value}
      fi

      value=$(git config --file ${gbp_conf_file} --get DEFAULT.debian-branch 2>&- || true)
      if [ -n "${value:-}" ]; then
        DEBIAN_BRANCH=${value}
      fi

      value=$(git config --file ${gbp_conf_file} --bool --get DEFAULT.pristine-tar 2>&- || true)
      if [ -n "${value:-}" ]; then
        PRISTINE_TAR=${value}
      fi
    fi
  done
}

create_local_branch() {
  [ -n "${1:-}" ] || return 1

  local BRANCH="$1"

  if git branch -a | grep -q "remotes/origin/${BRANCH}"'$' ; then
    git branch -D "${BRANCH}" || true
    git branch "${BRANCH}" "remotes/origin/${BRANCH}"
  else
    echo "NOTE: branch $BRANCH does not exist, ignoring request to checkout therefore."
  fi
}

git_dch_auto() {
  if $APPLY_VERSION_WORKAROUND ; then
    echo "Applying version workaround workaround"
    dch -b --distribution=UNRELEASED --newversion=$VERSION_STRING -- \
      "SNAPSHOT autobuild for unreleased $ORIG_VERSION via jenkins-debian-glue."
  else
    ${GBP_DCH} --auto $DCH_COMBINED_OPTS ${DCH_CHANGELOG_FILE:-}
  fi
}

identify_latest_change() {
  # debian/changelog might be a symlink (e.g. because debian points to
  # pkg/debian), so make sure we don't access a non-existing file
  # by calling readlink
  git checkout -- $(readlink -f debian/changelog)

  local OLD_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')

  local last_tag=$(git describe $(git rev-list --tags='[^u]*' --max-count=1 HEAD))
  local last_merge=$(git describe $(git rev-list --all --merges --max-count=1 HEAD))
  local since=${last_tag}

  if [ -n "$last_merge" ] ; then
    local merge_date=$(git log ${last_merge} --pretty="format:%at" -1)
    local tag_date=$(git log ${last_tag} --pretty="format:%at" -1)
    if [ ${merge_date} -gt ${tag_date} ] ; then
      local since=${last_merge}
    fi
  fi

  echo "Latest tag [${last_tag:-}] / merge [${last_merge:-}] seems to be $since"
  ${GBP_DCH} -s "${since}" $DCH_COMBINED_OPTS ${DCH_CHANGELOG_FILE:-}

  local NEW_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')

  if dpkg --compare-versions "$NEW_VERSION" lt "$OLD_VERSION" ; then
    echo "Calculated version is older than last release, falling back to auto mode."
    # debian/changelog might be a symlink (e.g. because debian points to
    # pkg/debian), so make sure we don't access a non-existing file
    # by calling readlink
    git checkout -- $(readlink -f debian/changelog)

    git_dch_auto
  fi
}

branch_checkout() {
  # git-dch and git-buildpackage refuse to operate on a single git
  # commit, so instead create a temporary branch to work on
  random_branch="jenkins-debian-glue-buildbranch$RANDOM"

  if [ "${SKIP_BRANCH_AND_TAG_HANDLING:-}" = "true" ] ; then
    echo "*** Skipping branch/tag handling as requested via SKIP_BRANCH_AND_TAG_HANDLING ***"

    echo "Making sure that random_branch $random_branch does not exist"
    git branch -D "$random_branch" || true

    echo "Checking out branch $random_branch based on git commit $GIT_COMMIT"
    git checkout -b "$random_branch" "$GIT_COMMIT"
  elif [ -n "${GERRIT_PATCHSET_REVISION:-}" ] ; then
    echo "*** We seem to be building for Gerrit ***"

    echo "Making sure that random_branch $random_branch does not exist"
    git branch -D "$random_branch" || true

    echo "*** Fetching Gerrit patchsets/commits from ${GIT_URL} ***"
    git fetch --tags --progress ${GIT_URL} +refs/changes/*:refs/remotes/origin/*

    echo "Checking out branch $random_branch based on Gerrit patchset revision ${GERRIT_PATCHSET_REVISION} ***"
    git checkout -b "$random_branch" "$GERRIT_PATCHSET_REVISION"
  elif [ -n "${tag:-}" ] && [ "${tag:-}" != "none" ] ; then
    echo "Making sure that random_branch $random_branch does not exist"
    git branch -D "$random_branch" || true

    echo "Checking out branch $random_branch based on tag $tag"
    git checkout -b "$random_branch" "$tag"
  elif [ -n "${branch:-}" ] ; then
    # prepend "origin" when checking out a branch, otherwise we might be
    # checking out a branch which doesn't exist locally yet, failing with:
    # | fatal: git checkout: updating paths is incompatible with switching branches.
    # | Did you intend to checkout '$BRANCH' which can not be resolved as commit?
    local checkout_branch

    case "$branch" in
      # rewrite refs/heads/FOO to refs/remotes/origin/FOO, otherwise it fails;
      # the refs/heads/FOO is something e.g. github pushes through its API
      refs/heads/*) checkout_branch="${branch/refs\/heads/refs/remotes/origin}" ;;

      # keep as it is when receiving origin/* already
          origin/*) checkout_branch="$branch" ;;

      # prepend "origin/" otherwise
                 *) checkout_branch="origin/${branch}" ;;
    esac

    echo "Making sure that random_branch $random_branch does not exist"
    git branch -D "$random_branch" || true

    echo "Checking out branch $random_branch based on branch $checkout_branch"
    git checkout -b "$random_branch" "$checkout_branch"
  else
    echo "Making sure that random_branch $random_branch does not exist"
    git branch -D "$random_branch" || true

    echo "Checking out branch $random_branch based on commit $GIT_COMMIT"
    git checkout -b "$random_branch" "$GIT_COMMIT"
  fi
}

changelog_generation() {
  # Allow skipping of changelog creation, e.g. to build a branch as-is
  if [ "${SKIP_DCH:-}" = "true" ] ; then
    echo "*** SKIP_DCH is set, not modifying debian/changelog therefore ***"
  else
    if [ -n "${tag:-}" ] && [ "${tag:-}" != "none" ] ; then
      echo "Tag parameter found (and not set to 'none'), not touching the changelog."
    elif [ "${1:-}" = "identify" ] ; then
      echo "Trying to identify latest tag / merge..."

      if ! git describe $(git rev-list --tags='[^u]*' --max-count=1 HEAD) >/dev/null ; then
        echo "Failed to identify latest change, falling back to auto mode."
        git_dch_auto
      else
        identify_latest_change
      fi
    elif [ -r debian/gbp.conf ] ; then
      echo "Found debian/gbp.conf, using ${GBP_DCH} with auto mode."
      git_dch_auto
    else
      echo "Using ${GBP_DCH} with auto mode."
      git_dch_auto
    fi

    # get rid of "UNRELEASED" distribution header, only
    # necessary if the debian/changelog was modified
    if [ -z "${tag:-}" ]; then
      local debchange_opts
      if [ -n "${DIST:-}" ]; then
        debchange_opts="--distribution ${DIST} --force-distribution"
      fi
      debchange --release ${debchange_opts:-} ""
    fi
  fi
}

git_cleanup() {
  if [ -n "${SKIP_GIT_CLEANUP:-}" ] ; then
    echo "*** Skipping git_cleanup as requested via SKIP_GIT_CLEANUP ***"
    return 0
  fi

  echo "*** Cleaning git repository to make sure there are no modified files ***"
  echo "*** Note: You can skip this step by setting SKIP_GIT_CLEANUP=true ***"
  git clean -fxd
  git reset --hard HEAD
}

quilt_cleanup() {
  if [ -n "${SKIP_QUILT_CLEANUP:-}" ] ; then
    echo "*** Skipping quilt_cleanup as requested via SKIP_QUILT_CLEANUP ***"
    return 0
  fi

  echo "*** Unapplying all possibly applied quilt patches and removing quilt directory ***"
  echo "*** Note: You can skip this step by setting SKIP_QUILT_CLEANUP=true ***"
  quilt pop -a || true
  rm -rf .pc
}

identify_source_build_opts() {
  # unset by default
  DBP_OPTS=""

  if [ -r "debian/source/format" ] && ! grep -q '^1\.0' debian/source/format ; then
    echo "*** Found dpkg source format != 1.0, nothing to do ****"
    return 0
  fi

  if [ -r "debian/source/options" ] ; then
    if ! grep -q '^[[:space:]]*tar-ignore' "debian/source/options" ; then
      echo "*** Enabling '-I' option as tar-ignore option not present in debian/source/options ***"
      DBP_OPTS+=" -I"
    fi

    # note: we shouldn't match on 'extend-diff-ignore'
    if ! grep -q '^[[:space:]]*diff-ignore' "debian/source/options" ; then
      echo "*** Enabling '-i' option as diff-ignore option not present in debian/source/options ***"
      DBP_OPTS+=" -i"
    fi
  else
    echo "*** Enabling '-i -I' options as building for dpkg source format 1.0 ***"
    DBP_OPTS="-i -I"
  fi
}

source_format_opts() {
  if ! [ -f "debian/source/format" ] ; then
    echo '*** WARNING: Source format is not specified in debian/source/format! ***'
    return 0
  fi

  local SRC_FORMAT="$(cat debian/source/format)"

  case "$SRC_FORMAT" in
    '3.0 (quilt)')
      echo "*** Source format detected as 3.0 (quilt) ***";
      DBP_EXTRA_OPTS="${DBP_EXTRA_OPTS:-} --source-option=--unapply-patches"
      ;;
    '3.0 (git)')
      echo "*** Source format detected as 3.0 (git) ***"
      PRISTINE_TAR=false
      SKIP_QUILT_CLEANUP=true
      IGNORE_GIT_BUILDPACKAGE=true
      ;;
    '3.0 (native)')
      echo "*** Source format detected as 3.0 (native) ***"
      ;;
    '3.0')
      echo "*** Source format detected as 3.0 ***"
      ;;
    '1.0')
      echo "*** Source format detected as 1.0 ***"
      SKIP_QUILT_CLEANUP=true
      ;;
    *)
      echo "*** WARNING: invalid or unknown source format: ${SRC_FORMAT} ***"
      ;;
  esac
}

# main execution
echo "*** source package build phase ***"
rm -f ./* || true

if [ -n "${PRE_SOURCE_HOOK:-}" ] ; then
  echo "*** Found environment variable PRE_SOURCE_HOOK, set to ${PRE_SOURCE_HOOK:-} ***"
  sh ${PRE_SOURCE_HOOK:-}
fi

cd "$SOURCE_DIRECTORY"

git_cleanup
source_format_opts
identify_source_build_opts

# Only if we are using git-buildpackage...
if [ "${IGNORE_GIT_BUILDPACKAGE:-}" != "true" ] ; then
	# gbp.conf handling
	gbp_conf

	# make sure common branches are available for git-buildpackage
	create_local_branch ${UPSTREAM_BRANCH}
	create_local_branch ${DEBIAN_BRANCH}
	if [ ${PRISTINE_TAR} = 'true' ]; then
	  create_local_branch pristine-tar
	fi
fi

# check out the according branch/tag with help of $random_branch
branch_checkout

if [ -n "${PRE_DCH_HOOK:-}" ] ; then
  echo "*** Found environment variable PRE_DCH_HOOK, set to ${PRE_DCH_HOOK:-} ***"
  sh ${PRE_DCH_HOOK:-}
fi

# retrieve version information *after* checking out the according branch/tag,
# otherwise version number might result in something different than expected
version_information

# handling of debian/changelog
dch_opts
changelog_generation

# prepare orig.tar.gz using pristine-tar, but without actually building the source package
if [ "${IGNORE_GIT_BUILDPACKAGE:-}" = "true" ] ; then
  echo "Skipping git-buildpackage execution as requested via IGNORE_GIT_BUILDPACKAGE ***"
else
  gbp_opts # set $GBP_OPTS
  ${GBP_BUILDPACKAGE} ${GBP_OPTS:-}
fi

quilt_cleanup

# build source package, run before switching back to previous branch
# to get the actual requested version
dpkg-buildpackage -uc -us -nc -d -S ${DBP_OPTS:-} ${DBP_EXTRA_OPTS:-}

if [ -n "${KEY_ID:-}" ] ; then
  echo "*** Found environment variable KEY_ID, set to ${KEY_ID:-}, signing source package ***"

  if ! [ -x "$(which debsign)" ] ; then
    echo "Error: debsign not available, please make sure the devscripts package is installed." >&2
    exit 1
  fi

  debsign -k"${KEY_ID:-}" ../*_source.changes
fi

if [ -n "${KEEP_SOURCE_CHANGES:-}" ]; then
  echo "*** Not removing ../*_source.changes because KEEP_SOURCE_CHANGES is set ***"
else
  echo "*** Removing ../*_source.changes because KEEP_SOURCE_CHANGES is not set ***"
  # get rid of the *_source.changes files generated by dpkg-buildpackage,
  # to avoid failures in a later stage when copying *all* files around
  rm -f ../*_source.changes
fi

# revert to original debian/changelog to avoid merge conflicts
git checkout -- $(readlink -f debian/changelog)

# switch back to previous "branch" before removing the tmp branch
if ! git checkout -f "${GIT_BRANCH}"; then
  # we were probably detached
  git checkout -f "${GIT_COMMIT}"
fi
git branch -D "$random_branch"

# vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2