| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# This file is part of the DXX-Rebirth project. |
| 4 |
# |
| 5 |
# It is copyright by its individual contributors, as recorded in the |
| 6 |
# project's Git history. See COPYING.txt at the top level for license |
| 7 |
# terms and a link to the Git history. |
| 8 |
|
| 9 |
set -eu |
| 10 |
set -o pipefail |
| 11 |
|
| 12 |
cd "$(dirname "$0")" |
| 13 |
|
| 14 |
parse_version_from_SConstruct() { |
| 15 |
local version |
| 16 |
unset descent_version_MAJOR descent_version_MINOR descent_version_MICRO |
| 17 |
# Read the blob SConstruct from the chosen $git_commit. For lines that |
| 18 |
# assign a decimal number to a variable matching VERSION_\w\+, print a=b |
| 19 |
# where a is the suffix of the variable and b is the decimal value. |
| 20 |
# Discard all other lines. Printed lines are iterated to assign shell |
| 21 |
# variables of matching names. |
| 22 |
for version in $( git show "$git_commit:SConstruct" | sed -n -e 's/^\s*VERSION_\(\w\+\)\s*=\s*\([0-9]\+\)/\1=\2/p' ); do |
| 23 |
printf -v "descent_version_${version%=*}" '%s' "${version#*=}" |
| 24 |
done |
| 25 |
# Test that the three expected values were assigned. |
| 26 |
[[ -n "$descent_version_MAJOR" ]] |
| 27 |
[[ -n "$descent_version_MINOR" ]] |
| 28 |
[[ -n "$descent_version_MICRO" ]] |
| 29 |
} |
| 30 |
|
| 31 |
set_git_commit_information() { |
| 32 |
declare -a git_commit_information |
| 33 |
# Retrieve the date and hash of the chosen $git_commit. Store them to |
| 34 |
# shell variables for later use. |
| 35 |
git_commit_information=( $(git log -1 '--pretty=%ad %H' '--date=format:%Y%m%d' "$git_commit" ) ) |
| 36 |
git_commit_date="${git_commit_information[0]}" |
| 37 |
git_commit_hash="${git_commit_information[1]}" |
| 38 |
} |
| 39 |
|
| 40 |
generate_snapshot_ebuild() { |
| 41 |
[[ -n "$git_commit_date" ]] |
| 42 |
[[ -n "$git_commit_hash" ]] |
| 43 |
local live_ebuild snapshot_ebuild |
| 44 |
live_ebuild=contrib/gentoo/games-action/dxx-rebirth/dxx-rebirth-9999.ebuild |
| 45 |
snapshot_ebuild="${live_ebuild%/*}/dxx-rebirth-${descent_version_MAJOR}.${descent_version_MINOR}.${descent_version_MICRO}_pre${git_commit_date}.ebuild" |
| 46 |
# Read the blob of the live ebuild from the chosen $git_commit. Pipe the |
| 47 |
# blob to a sed that inserts the commit hash into the assignment to |
| 48 |
# `MY_COMMIT`. In dry-run mode, stdout remains directed to where the |
| 49 |
# caller set it. In non-dry-run mode, stdout is redirected to the non-live |
| 50 |
# file named by $snapshot_ebuild. |
| 51 |
git show "$git_commit:$live_ebuild" | \ |
| 52 |
( |
| 53 |
[[ -z "$dry" ]] && exec > "$snapshot_ebuild" |
| 54 |
exec sed -e "s/MY_COMMIT=''/MY_COMMIT='$git_commit_hash'/" |
| 55 |
) |
| 56 |
# If the caller requested to add the newly created ebuild to git, do so. |
| 57 |
if [[ -z "$dry" && -n "$add_snapshot" ]]; then |
| 58 |
git add "$snapshot_ebuild" |
| 59 |
fi |
| 60 |
# If the caller requested to remove all older snapshot ebuilds from git, do |
| 61 |
# so. |
| 62 |
if [[ -n "$remove_snapshot" ]]; then |
| 63 |
# List snapshot ebuilds, but tell git not to show the just-created |
| 64 |
# snapshot ebuild. This prevents the `git rm` from removing the |
| 65 |
# just-created snapshot ebuild. |
| 66 |
git ls-files -z 'contrib/gentoo/games-action/dxx-rebirth/dxx-rebirth-*.*_pre*.ebuild' ':!'"$snapshot_ebuild" | xargs -0 git rm |
| 67 |
fi |
| 68 |
} |
| 69 |
|
| 70 |
add_snapshot= |
| 71 |
dry= |
| 72 |
remove_snapshot= |
| 73 |
|
| 74 |
while getopts anr OPTNAME; do |
| 75 |
case "$OPTNAME" in |
| 76 |
a) |
| 77 |
add_snapshot=1 |
| 78 |
;; |
| 79 |
n) |
| 80 |
dry=1 |
| 81 |
;; |
| 82 |
r) |
| 83 |
remove_snapshot=1 |
| 84 |
;; |
| 85 |
esac |
| 86 |
done |
| 87 |
|
| 88 |
# If not otherwise specified, read the most recent commit. |
| 89 |
if [[ "$#" -lt $OPTIND ]]; then |
| 90 |
git_commit=HEAD |
| 91 |
else |
| 92 |
git_commit="$1" |
| 93 |
fi |
| 94 |
|
| 95 |
# In dry-run mode, the snapshot ebuild will not be created, so it cannot be |
| 96 |
# added to git. |
| 97 |
if [[ -n "$dry" ]]; then |
| 98 |
add_snapshot= |
| 99 |
fi |
| 100 |
|
| 101 |
parse_version_from_SConstruct |
| 102 |
set_git_commit_information |
| 103 |
generate_snapshot_ebuild |