github-merge-pr: fix loading .config if symbolic link is used
[maintainer-tools.git] / makebranch.sh
1 #!/usr/bin/env bash
2
3 git_author="Release System"
4 git_email="lede-dev@lists.infradead.org"
5
6 base_url="https://downloads.openwrt.org/releases"
7
8 [ -f "./feeds.conf.default" ] || {
9 echo "Please execute as ./${0##*/}" >&2
10 exit 1
11 }
12
13 usage() {
14 {
15 echo ""
16 echo "Usage: $0 [-i] [-a <Git author>] [-e <Git email>] \\"
17 echo " [-u <Download base url>] [-n <codename>] -v <version>"
18 echo ""
19 echo "-i"
20 echo "Exit successfully if branch already exists"
21 echo ""
22 echo "-a Git author [$git_author]"
23 echo "Override the author name used for automated Git commits"
24 echo ""
25 echo "-e Git email [$git_email]"
26 echo "Override the email used for automated Git commits"
27 echo ""
28 echo "-u Download base url [$base_url]"
29 echo "Use the given URL as base for download repositories"
30 echo ""
31 exit 1
32 } >&2
33 }
34
35 while getopts "a:e:iu:n:v:" opt; do
36 case "$opt" in
37 a) git_author="$OPTARG" ;;
38 e) git_email="$OPTARG" ;;
39 i) ignore_existing=1 ;;
40 u) base_url="${OPTARG%/}" ;;
41 n) codename="$OPTARG" ;;
42 v)
43 case "$OPTARG" in
44 [0-9]*.[0-9]*)
45 version="$(echo "$OPTARG" | cut -d. -f1-2)"
46 ;;
47 *)
48 echo "Unexpected version format: $OPTARG" >&2
49 exit 1
50 ;;
51 esac
52 ;;
53 \?)
54 echo "Unexpected option: -$OPTARG" >&2
55 usage
56 ;;
57 :)
58 echo "Missing argument for option: -$OPTARG" >&2
59 usage
60 ;;
61 esac
62 done
63
64 [ -n "$version" ] || usage
65
66 revnum="$(./scripts/getver.sh)"
67 githash="$(git log --format=%h -1)"
68
69 prev_branch="$(git symbolic-ref -q HEAD)"
70
71 if [ "$prev_branch" != "refs/heads/master" ]; then
72 echo "Expecting current branch name to be \"master\"," \
73 "but it is \"${prev_branch#refs/heads/}\" - aborting."
74
75 exit 1
76 fi
77
78 distname="$(sed -ne '/config VERSION_DIST/ { :next; n; s/^[[:space:]]*default "\(.*\)"/\1/p; T next }' \
79 package/base-files/image-config.in)"
80
81 distname="${distname:-OpenWrt}"
82 distname_lc="$(echo "$distname" | tr 'A-Z' 'a-z')"
83
84 if git rev-parse "${distname_lc}-${version}^{tree}" >/dev/null 2>/dev/null; then
85 if [ -z "$ignore_existing" ]; then
86 echo "Branch ${distname_lc}-${version} already exists!" >&2
87 exit 1
88 fi
89
90 exit 0
91 fi
92
93 if grep -sq 'RELEASE:=' include/version.mk && [ -z "$codename" ]; then
94 echo "A codename is required for this ${distname} version!" >&2
95 exit 1
96 fi
97
98 export GIT_AUTHOR_NAME="$git_author"
99 export GIT_AUTHOR_EMAIL="$git_email"
100 export GIT_COMMITTER_NAME="$git_author"
101 export GIT_COMMITTER_EMAIL="$git_email"
102
103 git checkout -b "${distname_lc}-$version"
104
105 while read type name url; do
106 case "$type" in
107 src-git|\
108 src-git-full)
109 case "$url" in
110 *^*|*\;*) : ;;
111 *)
112 ref="$(git ls-remote "$url" "${distname_lc}-$version")"
113
114 if [ -z "$ref" ]; then
115 echo "WARNING: Feed \"$name\" provides no" \
116 "\"${distname_lc}-$version\" branch - using master!" >&2
117 else
118 url="$url;${distname_lc}-$version"
119 fi
120 ;;
121 esac
122 echo "$type $name $url"
123 ;;
124 src-*)
125 echo "$type $name $url"
126 ;;
127 esac
128 done < feeds.conf.default > feeds.conf.branch && \
129 mv feeds.conf.branch feeds.conf.default
130
131 sed -e 's!^RELEASE:=.*!RELEASE:='"$codename"'!g' \
132 -e 's!\(VERSION_NUMBER:=\$(if .*\),[^,]*)!\1,'"$version-SNAPSHOT"')!g' \
133 -e 's!\(VERSION_REPO:=\$(if .*\),[^,]*)!\1,'"$base_url/$version-SNAPSHOT"')!g' \
134 include/version.mk > include/version.branch && \
135 mv include/version.branch include/version.mk
136
137 sed -e 's!\(http\|https\)://downloads.\(openwrt\|lede-project\).org/[^"]*!'"$base_url/$version-SNAPSHOT"'!g' \
138 package/base-files/image-config.in > package/base-files/image-config.branch && \
139 mv package/base-files/image-config.branch package/base-files/image-config.in
140
141 git commit -sm "${distname:-OpenWrt} v$version: set branch defaults" \
142 feeds.conf.default \
143 include/version.mk \
144 package/base-files/image-config.in
145
146 git --no-pager log -p -1
147 git checkout "${prev_branch#refs/heads/}"
148
149 cat <<EOT
150 # Push the branch with:
151 git push origin "refs/heads/${distname_lc}-$version:refs/heads/${distname_lc}-$version"
152 EOT
153