github-merge-pr: fix loading .config if symbolic link is used
[maintainer-tools.git] / maketag.sh
1 #!/usr/bin/env bash
2
3 git_author="$(git config user.name)"
4 git_email="$(git config user.email)"
5 gpg_keyid=""
6
7 base_url="https://downloads.openwrt.org/releases"
8
9 [ -f "./feeds.conf.default" ] || {
10 echo "Please execute as ./${0##*/}" >&2
11 exit 1
12 }
13
14 usage() {
15 {
16 echo ""
17 echo "Usage: $0 [-i] [-a <Git author>] [-e <Git email>] \\"
18 echo " [-k <GPG key id>] [-p <GPG passphrase file>] \\"
19 echo " [-u <Download base url>] -v <version>"
20 echo ""
21 echo "-i"
22 echo "Exit successfully if tag already exists"
23 echo ""
24 echo "-a Git author [$git_author]"
25 echo "Override the author name used for automated Git commits"
26 echo ""
27 echo "-e Git email [$git_email]"
28 echo "Override the email used for automated Git commits"
29 echo ""
30 echo "-k GPG key id [${gpg_keyid:-none}]"
31 echo "Enable GPG signing of tags with given GPG key id"
32 echo ""
33 echo "-p GPG passphrase file [none]"
34 echo "Use the passphrase stored in the given file for signing"
35 echo ""
36 echo "-u Download base url [$base_url]"
37 echo "Use the given URL as base for download repositories"
38 echo ""
39 exit 1
40 } >&2
41 }
42
43 while getopts "a:e:ik:p:u:v:" opt; do
44 case "$opt" in
45 a) git_author="$OPTARG" ;;
46 e) git_email="$OPTARG" ;;
47 i) ignore_existing=1 ;;
48 k) gpg_keyid="${OPTARG#0x}" ;;
49 p) gpg_passfile="${OPTARG}" ;;
50 u) base_url="${OPTARG%/}" ;;
51 v)
52 case "$OPTARG" in
53 [0-9]*.[0-9]*.[0-9]*)
54 version="$OPTARG"
55 basever="$(echo "$version" | cut -d. -f1-2)"
56 ;;
57 *)
58 echo "Unexpected version format: $OPTARG" >&2
59 exit 1
60 ;;
61 esac
62 ;;
63 \?)
64 echo "Unexpected option: -$OPTARG" >&2
65 usage
66 ;;
67 :)
68 echo "Missing argument for option: -$OPTARG" >&2
69 usage
70 ;;
71 esac
72 done
73
74 [ -n "$version" ] || usage
75
76 if git rev-parse "v${version}^{tag}" >/dev/null 2>/dev/null; then
77 if [ -z "$ignore_existing" ]; then
78 echo "Tag v${version} already exists!" >&2
79 exit 1
80 fi
81
82 exit 0
83 fi
84
85 revnum="$(./scripts/getver.sh)"
86 epoch="$(./scripts/get_source_date_epoch.sh)"
87
88 branch="$(git symbolic-ref -q HEAD)"
89 distro="OpenWrt"
90
91 case "$branch" in
92 *-$basever) : ;;
93 *)
94 echo "Expecting current branch name to end in \"-$basever\"," \
95 "but it is \"${branch#refs/heads/}\" - aborting."
96
97 exit 1
98 ;;
99 esac
100
101 case "$branch" in
102 */lede-*) distro="LEDE" ;;
103 */openwrt-*) distro="OpenWrt" ;;
104 esac
105
106 export GIT_AUTHOR_NAME="$git_author"
107 export GIT_AUTHOR_EMAIL="$git_email"
108 export GIT_COMMITTER_NAME="$git_author"
109 export GIT_COMMITTER_EMAIL="$git_email"
110
111 while read type name url; do
112 case "$type" in
113 src-git|\
114 src-git-full)
115 case "$url" in
116 *^*) sha1="${url##*^}" ;;
117 *\;*) sha1="$(git ls-remote "${url%;*}" "${url##*;}")" ;;
118 *) sha1="$(git ls-remote "$url" "master")" ;;
119 esac
120 echo "$type $name ${url%[;^]*}${sha1:+^${sha1:0:40}}"
121 ;;
122 src-svn)
123 case "$url" in
124 *\;*) rev="${url##*;}" ;;
125 *) rev="$(svn log -l 1 "$url" | sed -ne '2s/ .*$//p')" ;;
126 esac
127 echo "$type $name ${url%;*}${rev:+;$rev}"
128 ;;
129 src-*)
130 echo "$type $name $url"
131 ;;
132 esac
133 done < feeds.conf.default > feeds.conf.tagged && \
134 mv feeds.conf.tagged feeds.conf.default
135
136 sed -e 's!\(VERSION_NUMBER:=\$(if .*\),[^,]*)!\1,'"$version"')!g' \
137 -e 's!\(VERSION_CODE:=\$(if .*\),[^,]*)!\1,'"$revnum"')!g' \
138 -e 's!\(VERSION_REPO:=\$(if .*\),[^,]*)!\1,'"$base_url/$version"')!g' \
139 include/version.mk > include/version.tagged && \
140 mv include/version.tagged include/version.mk
141
142 sed -e 's!\(http\|https\)://downloads.\(openwrt\|lede-project\).org/[^"]*!'"$base_url/$version"'!g' \
143 -e '/config VERSION_CODE_FILENAMES/ { :next; n; s!default y!default n!; t end; b next }; :end' \
144 package/base-files/image-config.in > package/base-files/image-config.tagged && \
145 mv package/base-files/image-config.tagged package/base-files/image-config.in
146
147 echo "$revnum" > version && git add version
148 echo "$epoch" > version.date && git add version.date
149
150 git commit -sm "$distro v$version: adjust config defaults" \
151 feeds.conf.default \
152 include/version.mk \
153 package/base-files/image-config.in \
154 version version.date
155
156
157 if [ -n "$gpg_keyid" -a -n "$gpg_passfile" ]; then
158 gpg_script="$(tempfile)"
159
160 cat <<-EOT > "$gpg_script"
161 #!/usr/bin/env bash
162 exec $(which gpg) --batch --passphrase-file $gpg_passfile "\$@"
163 EOT
164
165 chmod 0700 "$gpg_script"
166 fi
167
168 git ${gpg_script:+-c "gpg.program=$gpg_script"} tag \
169 -a "v$version" \
170 -m "$distro v$version Release" \
171 ${gpg_keyid:+-s -u "$gpg_keyid"}
172
173 [ -n "$gpg_script" ] && rm -f "$gpg_script"
174
175 git revert --no-edit HEAD
176 git commit --amend -sm "$distro v$version: revert to branch defaults"
177
178 git --no-pager show "v$version"
179
180 cat <<EOT
181 # Push the branch and tag with:
182 git push origin "${branch#refs/heads/}"
183 git push --follow-tags origin "refs/tags/v$version:refs/tags/v$version"
184 EOT