1+ #! /bin/bash
2+
3+ input=$1
4+
5+ function fetch_latest_changes() {
6+ echo " Fetching the latest remote branches"
7+ git fetch --all
8+ }
9+
10+ fetch_latest_changes
11+
12+ function find_feature_branches() {
13+ echo " Searching for feature branches"
14+ feature_branches=($( git branch -r --list " *-main" | sed ' s|origin/||' ) )
15+
16+ if [ ${# feature_branches[@]} -eq 0 ]; then
17+ echo " ...none found"
18+ return
19+ fi
20+
21+ for feature_branch in " ${feature_branches[@]} " ; do
22+ echo " ...found feature branch: $feature_branch "
23+ done
24+ }
25+
26+ find_feature_branches
27+
28+ function find_exempt_branches() {
29+ echo " Searching for exempt branches"
30+ IFS=' ,' read -r -a exempt_branches <<< " $input"
31+
32+ if [ ${# exempt_branches[@]} -eq 0 ]; then
33+ echo " ...none found"
34+ return
35+ fi
36+
37+ for exempt_branch in " ${exempt_branches[@]} " ; do
38+ echo " ...found exempt branch: $exempt_branch "
39+ done
40+ }
41+
42+ find_exempt_branches
43+
44+ function filter_feature_branches() {
45+ echo " Filtering branches"
46+ branches=()
47+
48+ for feature_branch in " ${feature_branches[@]} " ; do
49+ if ! [[ " ${exempt_branches[*]} " =~ " $feature_branch " ]]; then
50+ echo " ...including feature branch: $feature_branch "
51+ branches+=(" $feature_branch " )
52+ else
53+ echo " ...excluding feature branch: $feature_branch "
54+ fi
55+ done
56+ }
57+
58+ filter_feature_branches
59+
60+ function merge_main() {
61+ echo " Merging main into branches"
62+
63+ if [ ${# branches[@]} -eq 0 ]; then
64+ echo " ...no branches to merge into"
65+ return
66+ fi
67+
68+ for branch in " ${branches[@]} " ; do
69+ echo " ...switching to branch: $branch "
70+ git switch " $branch "
71+ echo " ...merging main"
72+ git merge -m " misc: merge from main" main
73+ if [ $? -eq 0 ]; then
74+ echo " ...pushing to origin"
75+ git push origin " $branch "
76+ else
77+ echo " ...merge failed"
78+ git merge --abort
79+ fi
80+ done
81+ }
82+
83+ merge_main
0 commit comments