summaryrefslogtreecommitdiffstats
path: root/maketag.sh
blob: 80e94fa8bfc948d10150ed113e8493711a0cf993 (plain)
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
#!/usr/bin/env bash

git_author="$(git config user.name)"
git_email="$(git config user.email)"
gpg_keyid=""

base_url="https://downloads.openwrt.org/releases"

[ -f "./feeds.conf.default" ] || {
	echo "Please execute as ./${0##*/}" >&2
	exit 1
}

usage() {
	{
		echo ""
		echo "Usage: $0 [-i] [-a <Git author>] [-e <Git email>] \\"
		echo "          [-k <GPG key id>] [-p <GPG passphrase file>] \\"
		echo "          [-u <Download base url>] -v <version>"
		echo ""
		echo "-i"
		echo "Exit successfully if tag already exists"
		echo ""
		echo "-a Git author [$git_author]"
		echo "Override the author name used for automated Git commits"
		echo ""
		echo "-e Git email [$git_email]"
		echo "Override the email used for automated Git commits"
		echo ""
		echo "-k GPG key id [${gpg_keyid:-none}]"
		echo "Enable GPG signing of tags with given GPG key id"
		echo ""
		echo "-p GPG passphrase file [none]"
		echo "Use the passphrase stored in the given file for signing"
		echo ""
		echo "-u Download base url [$base_url]"
		echo "Use the given URL as base for download repositories"
		echo ""
		exit 1
	} >&2
}

while getopts "a:e:ik:p:u:v:" opt; do
	case "$opt" in
		a) git_author="$OPTARG" ;;
		e) git_email="$OPTARG" ;;
		i) ignore_existing=1 ;;
		k) gpg_keyid="${OPTARG#0x}" ;;
		p) gpg_passfile="${OPTARG}" ;;
		u) base_url="${OPTARG%/}" ;;
		v)
			case "$OPTARG" in
				[0-9]*.[0-9]*.[0-9]*)
					version="$OPTARG"
					basever="$(echo "$version" | cut -d. -f1-2)"
				;;
				*)
					echo "Unexpected version format: $OPTARG" >&2
					exit 1
				;;
			esac
		;;
		\?)
			echo "Unexpected option: -$OPTARG" >&2
			usage
		;;
		:)
			echo "Missing argument for option: -$OPTARG" >&2
			usage
		;;
	esac
done

[ -n "$version" ] || usage

if git rev-parse "v${version}^{tag}" >/dev/null 2>/dev/null; then
	if [ -z "$ignore_existing" ]; then
		echo "Tag v${version} already exists!" >&2
		exit 1
	fi

	exit 0
fi

revnum="$(./scripts/getver.sh)"
epoch="$(./scripts/get_source_date_epoch.sh)"

branch="$(git symbolic-ref -q HEAD)"
distro="OpenWrt"

case "$branch" in
	*-$basever) : ;;
	*)
		echo "Expecting current branch name to end in \"-$basever\"," \
		     "but it is \"${branch#refs/heads/}\" - aborting."

		exit 1
	;;
esac

case "$branch" in
	*/lede-*) distro="LEDE" ;;
	*/openwrt-*) distro="OpenWrt" ;;
esac

export GIT_AUTHOR_NAME="$git_author"
export GIT_AUTHOR_EMAIL="$git_email"
export GIT_COMMITTER_NAME="$git_author"
export GIT_COMMITTER_EMAIL="$git_email"

while read type name url; do
	case "$type" in
		src-git|\
		src-git-full)
			case "$url" in
				*^*)  sha1="${url##*^}" ;;
				*\;*) sha1="$(git ls-remote "${url%;*}" "${url##*;}")" ;;
				*)    sha1="$(git ls-remote "$url" "master")" ;;
			esac
			echo "$type $name ${url%[;^]*}${sha1:+^${sha1:0:40}}"
		;;
		src-svn)
			case "$url" in
				*\;*) rev="${url##*;}" ;;
				*)    rev="$(svn log -l 1 "$url" | sed -ne '2s/ .*$//p')" ;;
			esac
			echo "$type $name ${url%;*}${rev:+;$rev}"
		;;
		src-*)
			echo "$type $name $url"
		;;
	esac
done < feeds.conf.default > feeds.conf.tagged && \
	mv feeds.conf.tagged feeds.conf.default

sed -e 's!\(VERSION_NUMBER:=\$(if .*\),[^,]*)!\1,'"$version"')!g' \
    -e 's!\(VERSION_CODE:=\$(if .*\),[^,]*)!\1,'"$revnum"')!g' \
    -e 's!\(VERSION_REPO:=\$(if .*\),[^,]*)!\1,'"$base_url/$version"')!g' \
	include/version.mk > include/version.tagged && \
		mv include/version.tagged include/version.mk

sed -e 's!\(http\|https\)://downloads.\(openwrt\|lede-project\).org/[^"]*!'"$base_url/$version"'!g' \
    -e '/config VERSION_CODE_FILENAMES/ { :next; n; s!default y!default n!; t end; b next }; :end' \
	package/base-files/image-config.in > package/base-files/image-config.tagged && \
		mv package/base-files/image-config.tagged package/base-files/image-config.in

echo "$revnum" > version && git add version
echo "$epoch" > version.date && git add version.date

git commit -sm "$distro v$version: adjust config defaults" \
	feeds.conf.default \
	include/version.mk \
	package/base-files/image-config.in \
	version version.date


if [ -n "$gpg_keyid" -a -n "$gpg_passfile" ]; then
	gpg_script="$(tempfile)"

	cat <<-EOT > "$gpg_script"
		#!/usr/bin/env bash
		exec $(which gpg) --batch --passphrase-file $gpg_passfile "\$@"
	EOT

	chmod 0700 "$gpg_script"
fi

git ${gpg_script:+-c "gpg.program=$gpg_script"} tag \
	-a "v$version" \
	-m "$distro v$version Release" \
	${gpg_keyid:+-s -u "$gpg_keyid"}

[ -n "$gpg_script" ] && rm -f "$gpg_script"

git revert --no-edit HEAD
git commit --amend -sm "$distro v$version: revert to branch defaults"

git --no-pager show "v$version"

cat <<EOT
# Push the branch and tag with:
git push origin "${branch#refs/heads/}"
git push --follow-tags origin "refs/tags/v$version:refs/tags/v$version"
EOT