-
Notifications
You must be signed in to change notification settings - Fork 58
/
entrypoint.sh
executable file
·139 lines (113 loc) · 3.46 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Checks out each of your branches
# copies the current version of
# certain files to each branch
echo "==================================="
git status
echo "-----------------------------------"
git branch -r --list|sed 's/origin\///g'
echo "-----------------------------------"
ls -la
echo "==================================="
# Show this help screen if bad options are passed
showHelp() {
echo "Usage: $0 -k args_key -f parameter_files -b parameter_branches -b parameter_exclude -l parameter_action"
echo "\t-k The name of the key branch, otherwise main/master if available"
echo "\t-f List of files you want to copy to the branches"
echo "\t-b List of branches you want to copy the files to"
echo "\t-e List of branches you want to exclude"
echo "\t-l Local changes only. Don't push"
exit 1 # Exit script after printing help
}
# Get the options from arguments passed to project
while getopts "lk:f:b:e:" opt
do
case "$opt" in
l ) args_action=LOCAL ;;
k ) args_key="$OPTARG" ;;
f ) set -f
args_files=($OPTARG)
set +f ;;
b ) set -b
args_branches=($OPTARG)
set +f ;;
e ) set -b
args_exclude=($OPTARG)
set +f ;;
? ) showHelp ;;
esac
done
# Set default list of branches to use
if [ ! -z "${args_branches}" ];
then
ALL_THE_BRANCHES=( "${args_branches[@]}" )
else
ALL_THE_BRANCHES=`git branch -r --list|sed 's/origin\///g'`
fi
# Set the KEY branch
if [ ! -z "${args_key}" ];
then
KEY_BRANCH=$args_key
elif [[ $ALL_THE_BRANCHES[*]} =~ 'master' ]];
then
KEY_BRANCH='master'
elif [[ $ALL_THE_BRANCHES[*]} =~ 'main' ]];
then
KEY_BRANCH='main'
else
echo "Error: A key branch does not exist"
exit 1
fi
# Set default list of files to copy
if [ ! -z "${args_files}" ];
then
echo "\n\n\n\n===================================\n"
echo "FILES TO PROCESS: ${args_files[*]}"
ALL_THE_FILES=( "${args_files[@]}" )
else
ALL_THE_FILES=('LICENSE' 'NOTICE' 'README.md')
fi
# Loop through the array of branches and perform
# a series of checkouts from the KEY_BRANCH
for CURRENT_BRANCH in ${ALL_THE_BRANCHES[@]};
do
# exclude certain branches from processing if the user
# has added a -e flag with a list of branches in quotations
CONTINUE_BRANCH=false
for EXCLUDE_BRANCH in "${args_exclude[@]}"
do
if [ "$CURRENT_BRANCH" = "$EXCLUDE_BRANCH" ]
then
CONTINUE_BRANCH=true
fi
done
if [ "$CONTINUE_BRANCH" = true ]
then
continue
fi
# Check out the current branch, but only if
# the branch is NOT the same as the key branch
if [ "${KEY_BRANCH}" != "${CURRENT_BRANCH}" ];
then
echo "-------------------------------"
echo "CHECKOUT: $CURRENT_BRANCH"
git checkout -b $CURRENT_BRANCH origin/$CURRENT_BRANCH
# Go through each of the files
# Check out the selected files from the source branch
for CURRENT_FILE in ${ALL_THE_FILES[@]};
do
echo "\n--COPY: $CURRENT_FILE"
git checkout $KEY_BRANCH $CURRENT_FILE
done
# Commit the changes
git add -A && git commit -m "Moving: ${ALL_THE_FILES[@]} from $KEY_BRANCH branch"
# push the branch to the repository origin
if [ "$args_action" != "LOCAL" ];
then
git push --set-upstream origin $CURRENT_BRANCH
fi
fi
done
# Check out the key branch
git checkout $KEY_BRANCH
echo "\n===================================\n\n\n\n"