utils.sh 2.67 KB
Newer Older
1
# shellcheck shell=bash
2
3
# This library is meant to be used in bash, with "set -e" and "set -u".

anonym's avatar
anonym committed
4
BASE_BRANCHES="stable testing devel"
anonym's avatar
anonym committed
5

6
7
8
# Returns "" if in undetached head
git_current_branch() {
	local git_ref
anonym's avatar
anonym committed
9
	if git_ref="$(git symbolic-ref HEAD 2>/dev/null)"; then
anonym's avatar
anonym committed
10
		echo "${git_ref#refs/heads/}"
11
	else
anonym's avatar
anonym committed
12
		echo ""
13
	fi
14
15
}

16
git_in_detached_head() {
anonym's avatar
anonym committed
17
	[ -z "$(git_current_branch)" ]
18
19
}

20
# Returns "" if ref does not exist
21
git_commit_from_ref() {
22
	git rev-parse --verify "${@}" 2>/dev/null || :
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
}

git_current_commit() {
	git_commit_from_ref "${@}" HEAD
}

# Returns "" if not a tag
git_tag_from_commit() {
	git describe --tags --exact-match "${1}" 2>/dev/null || :
}

# Returns "" if not on a tag
git_current_tag() {
	git_tag_from_commit $(git_current_commit)
}

39
40
41
# Try to describe what currently is checked out. Returns "" if we are
# in detached HEAD, otherwise, in order, the tag pointing to HEAD, or
# the current branch.
42
git_current_head_name() {
43
44
45
46
	local ret
	ret="$(git_current_tag)"
	if [ -z "${ret}" ]; then
		ret="$(git_current_branch)"
47
	fi
48
	echo "${ret}"
49
50
}

51
52
git_on_a_tag() {
	[ -n "$(git_current_tag)" ]
53
54
}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
git_only_doc_changes_since() {
	local commit non_doc_diff
	commit="$(git_commit_from_ref ${1})"
	non_doc_diff="$(git diff \
		${commit}... \
		-- \
		'*' \
		':!/wiki' \
		':!/ikiwiki.setup' \
		':!/ikiwiki-cgi.setup' \
		':!*.po' \
	)"

	[ -z "${non_doc_diff}" ]
}

71
72
73
74
base_branch() {
	cat config/base_branch | head -n1
}

anonym's avatar
anonym committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
base_branches() {
	echo ${BASE_BRANCHES}
}

on_base_branch() {
	for base_branch in $BASE_BRANCHES ; do
		if [ "$(git_current_branch)" = "${base_branch}" ] ; then
			return 0
		fi
	done

	return 1
}

89
90
# Returns the top commit ref of the base branch
git_base_branch_head() {
91
	git_commit_from_ref "${@}" origin/"$(base_branch)"
92
93
}

94
95
96
97
98
99
branch_name_to_suite() {
	local branch="$1"

	echo "$branch" | sed -e 's,[^.a-z0-9-],-,ig'  | tr '[A-Z]' '[a-z]'
}

100
fatal() {
101
	echo "E: $*" >&2
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	exit 1
}

git_tag_exists() {
	local tag="$1"

	test -n "$(git tag -l "$tag")"
}

version_was_released() {
	local version="$1"

	version="$(echo "$version" | tr '~' '-')"
	git_tag_exists "$version"
}

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

previous_version_in_changelog() {
	dpkg-parsechangelog --offset 1 --count 1 | awk '/^Version: / { print $2 }'
}
125

126
127
128
129
130
131
# Make it so that when this script is called, any function defined in
# this script can be invoked via arguments, e.g.:
#
# $ auto/scripts/utils.sh git_commit_from_ref 3.0-beta2
# eca83a88a9dd958b16b4d5b04fea3ea503a3815d
#
132
if grep -q __utils_sh_magic_5773fa52-0d1a-11e7-a606-0021ccc177a7 "${0}" && [ -n "${1}" ]; then
133
	if grep -q "^${1}() {$" "${0}"; then
134
		eval "\"\${@}\""
135
136
137
138
	else
		echo "unknown shell function: ${1}" >&2
		exit 1
	fi
139
fi