early_patch: document ownership for files bind mounted into /home/amnesia
[tails.git] / auto / scripts / utils.sh
blob122f9a38f30649d03b97e4312059a52da7d54f02
1 # shellcheck shell=bash
3 BASE_BRANCHES="stable testing devel"
5 if [[ ! -o errexit ]] || [[ ! -o nounset ]]; then
6 echo "This library is meant to be used with 'set -e' and 'set -u'. Exiting..." >&2
7 exit 1
8 fi
10 # Returns "" if in detached head
11 git_current_branch() {
12 local git_ref
13 if git_ref="$(git symbolic-ref HEAD 2>/dev/null)"; then
14 echo "${git_ref#refs/heads/}"
15 else
16 echo ""
20 git_in_detached_head() {
21 [ -z "$(git_current_branch)" ]
24 # Returns "" if ref does not exist
25 git_commit_from_ref() {
26 git rev-parse --verify "${@}" 2>/dev/null || :
29 # shellcheck disable=SC2120
30 git_current_commit() {
31 git_commit_from_ref "${@}" HEAD
34 # Returns "" if not a tag
35 git_tag_from_commit() {
36 git describe --tags --exact-match "${1}" 2>/dev/null || :
39 # Returns "" if not on a tag
40 git_current_tag() {
41 git_tag_from_commit "$(git_current_commit)"
44 # Try to describe what currently is checked out. Returns "" if we are
45 # in detached HEAD, otherwise, in order, the tag pointing to HEAD, or
46 # the current branch.
47 git_current_head_name() {
48 local ret
49 ret="$(git_current_tag)"
50 if [ -z "${ret}" ]; then
51 ret="$(git_current_branch)"
53 echo "${ret}"
56 git_on_a_tag() {
57 [ -n "$(git_current_tag)" ]
60 git_only_doc_changes_since() {
61 local commit non_doc_diff
62 commit="$(git_commit_from_ref "${1}")"
63 non_doc_diff="$(git diff \
64 "${commit}"... \
65 -- \
66 '*' \
67 ':!/wiki' \
68 ':!/ikiwiki.setup' \
69 ':!*.po' \
72 [ -z "${non_doc_diff}" ]
75 base_branch() {
76 head -n1 config/base_branch
79 base_branches() {
80 # shellcheck disable=SC2086
81 echo ${BASE_BRANCHES}
84 on_base_branch() {
85 for base_branch in $BASE_BRANCHES ; do
86 if [ "$(git_current_branch)" = "${base_branch}" ] ; then
87 return 0
89 done
91 return 1
94 # Returns the top commit ref of the base branch
95 git_base_branch_head() {
96 git_commit_from_ref "${@}" origin/"$(base_branch)"
99 branch_name_to_suite() {
100 local branch="$1"
102 echo "$branch" | sed -e 's,[^.a-z0-9-],-,ig' | tr '[:upper:]' '[:lower:]'
105 fatal() {
106 echo "E: $*" >&2
107 exit 1
110 git_tag_exists() {
111 local tag="$1"
113 test -n "$(git tag -l "$tag")"
116 version_was_released() {
117 local version="$1"
119 version="$(echo "$version" | tr '~' '-')"
120 git_tag_exists "$version"
123 version_in_changelog() {
124 dpkg-parsechangelog | awk '/^Version: / { print $2 }'
127 previous_version_in_changelog() {
128 dpkg-parsechangelog --offset 1 --count 1 | awk '/^Version: / { print $2 }'
131 # Make it so that when this script is called, any function defined in
132 # this script can be invoked via arguments, e.g.:
134 # $ auto/scripts/utils.sh git_commit_from_ref 3.0-beta2
135 # eca83a88a9dd958b16b4d5b04fea3ea503a3815d
137 if grep -q __utils_sh_magic_5773fa52-0d1a-11e7-a606-0021ccc177a7 "${0}" && [ -n "${1}" ]; then
138 if grep -q "^${1}() {$" "${0}"; then
139 eval "\"\${@}\""
140 else
141 echo "unknown shell function: ${1}" >&2
142 exit 1