Skip to content

Commit 0bfe760

Browse files
authored
Merge pull request #27 from davidhcefx/branches
feat: add create-branches.sh and switch-branch.sh
2 parents 49b8483 + f2edac3 commit 0bfe760

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

tool/create-branches.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#! /usr/bin/env bash
2+
# Generate rcfiles compatible for old nano versions
3+
# Author: rasa (https://github.com/rasa)
4+
# Revised: davidhcefx (https://github.com/davidhcefx)
5+
6+
set -eu
7+
cd "$(dirname "$0")/../"
8+
9+
# use gsed if installed
10+
if command -v brew &>/dev/null; then
11+
PATH="$(brew --prefix)/opt/gnu-sed/libexec/gnubin:${PATH}"
12+
fi
13+
# bash version >= 4.0 for Assoc array and mapfile support
14+
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
15+
echo "Error: Bash version >= 4.0 is required."
16+
exit 1
17+
fi
18+
19+
# to ensure this list enumerates all created branches
20+
declare -A branches=(
21+
[pre-6.0]=pre-6.0
22+
[pre-5.0]=pre-5.0
23+
[pre-4.5]=pre-4.5
24+
[pre-2.9.5]=pre-2.9.5
25+
[pre-2.6.0]=pre-2.6.0
26+
[pre-2.3.3]=pre-2.3.3
27+
[pre-2.1.6]=pre-2.1.6
28+
)
29+
30+
function color_replace() {
31+
local from="$1"
32+
local to="$2"
33+
# color <fg>,<bg>
34+
sed -E -i -e "s/(color\\s+)$from\b/\\1$to/" \
35+
-e "s/(color\\s+[a-z]*,\\s*)$from\b/\\1$to/" "${nanos[@]}"
36+
}
37+
38+
39+
# ensures current branch is master
40+
git checkout master
41+
42+
# pulls all new commits made to upstream/master
43+
# git pull upstream master
44+
45+
# ensure there are no changes to commit
46+
if [ -n "$(git status --porcelain 2>/dev/null | tail -n 1)" ]; then
47+
echo "Error: There are changes to commit."
48+
exit 1
49+
fi
50+
51+
# remove existing pre branches
52+
for b in "${branches[@]}"; do
53+
git branch -D "${b}" || true
54+
done
55+
56+
# ignore symlinks, because sed will convert them to regular files
57+
mapfile -t nanos <<<"$(find . -not -type l -name '*.nanorc' | sort)"
58+
59+
################################
60+
git checkout -b "${branches[pre-6.0]}"
61+
62+
color_replace "rosy" "pink"
63+
color_replace "beet" "purple"
64+
color_replace "plum" "mauve"
65+
color_replace "sea" "lagoon"
66+
color_replace "sky" "lagoon"
67+
color_replace "slate" "mint"
68+
color_replace "teal" "mint"
69+
color_replace "sage" "lime"
70+
color_replace "brown" "lime"
71+
color_replace "ocher" "lime"
72+
color_replace "sand" "lime"
73+
color_replace "tawny" "latte"
74+
color_replace "brick" "orange"
75+
color_replace "crimson" "red"
76+
git commit -am "fix: pre-6.0: Replace color: rosy, beet, plum, sea, sky, slate, teal, sage, brown, ocher, sand, tawny, brick, crimson" || true
77+
78+
color_replace "grey" "lightblack"
79+
color_replace "gray" "lightblack"
80+
git commit -am "fix: pre-5.8: Replace {grey,gray} => 'lightblack'" || true
81+
82+
################################
83+
git checkout -b "${branches[pre-5.0]}"
84+
85+
color_replace "pink" "brightred"
86+
color_replace "purple" "magenta"
87+
color_replace "mauve" "magenta"
88+
color_replace "lagoon" "cyan"
89+
color_replace "mint" "green"
90+
color_replace "lime" "green"
91+
color_replace "peach" "yellow"
92+
color_replace "orange" "yellow"
93+
color_replace "latte" "yellow"
94+
git commit -am "fix: pre-5.0: Replace color: pink, purple, mauve, lagoon, mint, lime, peach, orange, latte" || true
95+
96+
sed -E -i -e 's/(color\s+)light([a-z]+)/\1bright\2/' \
97+
-e 's/(color\s+[a-z]*,\s*)light([a-z]+)/\1bright\2/' "${nanos[@]}"
98+
git commit -am "fix: pre-5.0: Replace prefix 'light*' => 'bright*'" || true
99+
100+
sed -E -i -e 's/(color\s+[a-z]*,\s*)bright([a-z]+)/\1\2/' "${nanos[@]}"
101+
git commit -am "fix: pre-5.0: Remove background prefix 'bright*'" || true
102+
103+
sed -E -i -e 's/(color\s+)(bold|italic),/\1/' "${nanos[@]}"
104+
git commit -am "fix: pre-5.0: Remove 'bold' and 'italic' specifier" || true
105+
106+
################################
107+
git checkout -b "${branches[pre-4.5]}"
108+
sed -E -i -e 's/^\s*tabgives\b/# \&/' "${nanos[@]}"
109+
git commit -am "fix: pre-4.5: Comment out 'tabgives'" || true
110+
111+
# somewhere before 4.5
112+
# +++ b/Rnw.nanorc
113+
# -color blue "([a-zA-Z0-9_\-$\.]*)\("
114+
# +color blue "([a-zA-Z0-9_$\.-]*)\("
115+
sed -E -i -e 's/a-zA-Z0-9_\\-\$\\\./a-zA-Z0-9_$\\.-/' "${nanos[@]}"
116+
117+
# +++ b/jade.nanorc
118+
# +++ b/pug.nanorc
119+
# -icolor brightgreen "https?:\/\/(www\.)?[a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)" "_blank"
120+
# +icolor brightgreen "https?:\/\/(www\.)?[a-zA-Z0-9@%._\+~#=]+\.[a-z]+\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)" "_blank"
121+
sed -E -i -e 's/]\{2,256\}\\/]+\\/' "${nanos[@]}"
122+
sed -E -i -e 's/]\{2,6\}\\b/]+\\b/' "${nanos[@]}"
123+
124+
# +++ b/toml.nanorc
125+
# -color ,red "^[[:space:]]*\[\..*?\]"
126+
# +color ,red "^[[:space:]]*\[\..*"
127+
sed -E -i -e 's/(color ,red .*\.\.\*)\?\\]/\1/' "${nanos[@]}"
128+
129+
# +++ b/x11basic.nanorc
130+
# -icolor brightwhite "\<[A-Z_][A-Za-z0-9_]*(|\$|\%|\&|\||\(\))\>"
131+
# +icolor brightwhite "\<[A-Z_][A-Za-z0-9_]*(\$|\%|\&|\||\(\))\>"
132+
sed -E -i -e 's/(icolor\s+brightwhite.*\*\()\|/\1/' "${nanos[@]}"
133+
134+
# +++ b/yaml.nanorc
135+
# -color red "(^|\s+).*+\s*:(\s|$)"
136+
# +color red "(^|\s+).*\+\s*:(\s|$)"
137+
sed -E -i -e 's/(color\s+red\s+.*\)\.\*)\+/\1\\+/' "${nanos[@]}"
138+
git commit -am "fix: pre-4.?: Fix bad regexes" || true
139+
140+
################################
141+
git checkout -b "${branches[pre-2.9.5]}"
142+
color_replace "normal" "white"
143+
git commit -am "fix: pre-2.9.5: Replace 'normal' => 'white'" || true
144+
145+
color_replace "brightnormal" "brightwhite"
146+
git commit -am "fix: pre-2.9.5: Replace 'brightnormal' => 'brightwhite'" || true
147+
148+
sed -E -i -e 's/^(\s*syntax\s+)([^"[:space:]]+)(\s.*)/\1"\2"\3/' "${nanos[@]}"
149+
git commit -am "fix: pre-2.9.4: Add quotes around syntax names" || true
150+
151+
################################
152+
git checkout -b "${branches[pre-2.6.0]}"
153+
sed -E -i -e 's/^\s*comment\b/# \&/' "${nanos[@]}"
154+
git commit -am "fix: pre-2.6.0: Comment out 'comment'" || true
155+
156+
################################
157+
git checkout -b "${branches[pre-2.3.3]}"
158+
sed -E -i -e 's/^\s*linter\b/# \&/' "${nanos[@]}"
159+
git commit -am "fix: pre-2.3.3: Comment out 'linter'" || true
160+
161+
sed -E -i -e 's/^\s*magic\b/# \&/' "${nanos[@]}"
162+
git commit -am "fix: pre-2.3.0: Comment out 'magic'" || true
163+
164+
################################
165+
git checkout -b "${branches[pre-2.1.6]}"
166+
sed -E -i -e 's/^\s*header\b/# \&/' "${nanos[@]}"
167+
git commit -am "fix: pre-2.1.6: Comment out 'header'" || true
168+
169+
# sed -E -i -e 's/^(\s*icolor\s+cyan\s+.*Add-AppPackage)/# \1/' "${nanos[@]}"
170+
# git commit -am "fix: pre-2.1.6: Comment out long string causing out-of-memory" || true
171+
172+
################################
173+
174+
# git checkout master
175+
# take care, this will delete all your changes on your forked master
176+
# git push --force origin master
177+
178+
echo ""
179+
echo "Please confirm to force push the following branches:"
180+
for b in "${branches[@]}"; do
181+
echo "- ${b}"
182+
done
183+
read -rp "Confirm? [y/N] " ch
184+
if [ "$ch" != "y" ]; then
185+
echo "Aborted."
186+
exit 1
187+
fi
188+
189+
for b in "${branches[@]}"; do
190+
git checkout "${b}"
191+
git push --force -u origin "${b}"
192+
done
193+
194+
git checkout master

tool/switch-branch.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env bash
2+
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}')")
12+
13+
if [[ -z "${NANO_VER}" ]]; then
14+
printf "Cannot determine nano's version\n" >&2
15+
exit 1
16+
fi
17+
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
40+
41+
echo "Switching to branch ${target}"
42+
43+
git checkout "${target}"

0 commit comments

Comments
 (0)