Skip to content

Commit f2edac3

Browse files
committed
enh(switch-branch): Remove hardcoded branch names
- Fetch them from Git upstream directly - Replace `mapfile` for backward compatibility
1 parent f5c4e46 commit f2edac3

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

tool/switch-branch.sh

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
#!/usr/bin/env bash
1+
#! /usr/bin/env bash
22

3-
NANO_VER=$(nano --version 2>/dev/null | awk '/version/ {print $4}' |
4-
awk -F . '{printf("%d%02d%02d", $1, $2, $3)}')
3+
function version_to_num() {
4+
if [[ -z "$1" ]]; then
5+
return
6+
fi
7+
awk -F . '{printf("%d%02d%02d", $1, $2, $3)}' <<<"$1"
8+
}
9+
10+
11+
NANO_VER=$(version_to_num "$(nano --version 2>/dev/null | awk '/version/ {print $4}')")
512

613
if [[ -z "${NANO_VER}" ]]; then
7-
printf "Cannot determine nano's version\\n" >&2
8-
exit 1
14+
printf "Cannot determine nano's version\n" >&2
15+
exit 1
916
fi
1017

11-
printf 'Found nano version %s\n' "$(nano --version 2>/dev/null | awk '/version/ {print $4}')"
12-
13-
if ((NANO_VER < 20105)); then
14-
NANO_BRANCH='pre-2.1.5'
15-
elif ((NANO_VER < 20299)); then
16-
NANO_BRANCH='pre-2.2.99'
17-
elif ((NANO_VER < 20302)); then
18-
NANO_BRANCH='pre-2.3.2'
19-
elif ((NANO_VER < 20503)); then
20-
NANO_BRANCH='pre-2.5.3'
21-
elif ((NANO_VER < 20905)); then
22-
NANO_BRANCH='pre-2.9.5'
23-
elif ((NANO_VER < 40500)); then
24-
NANO_BRANCH='pre-4.5'
25-
elif ((NANO_VER < 50000)); then
26-
NANO_BRANCH='pre-5.0'
27-
else
28-
NANO_BRANCH='master'
29-
fi
18+
echo "Found nano version ${NANO_VER} ($(nano --version | awk '/version/ {print $4}'))"
19+
20+
# fetch the available "pre*" branches
21+
git fetch --prune origin
22+
branches=()
23+
24+
#mapfile -t branches <<<"$(git branch -l -r --format='%(refname:short)' "origin/pre-*")"
25+
while IFS= read -r line; do
26+
branches+=("$line")
27+
done < <(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/pre-*')
28+
29+
# choose the suitable branch
30+
target="master"
31+
32+
for b in "${branches[@]}"; do
33+
num=$(version_to_num "${b#*/pre-}")
34+
35+
if ((NANO_VER < num)); then
36+
target="${b}"
37+
break
38+
fi
39+
done
3040

31-
printf 'Switching to branch %s\n' "${NANO_BRANCH}"
41+
echo "Switching to branch ${target}"
3242

33-
git checkout "${NANO_BRANCH}"
43+
git checkout "${target}"

0 commit comments

Comments
 (0)