-
Notifications
You must be signed in to change notification settings - Fork 2
/
branchout-group
executable file
·163 lines (133 loc) · 4.43 KB
/
branchout-group
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
test -n "${DEBUG}" && set -x
set -e
function branchoutGroupDeriveStdin() {
# STDIN is always derived
while read -r each; do
branchoutGroupDerive "${each}"
done
}
function branchoutGroupPull() {
projectGroup="${1}"
if test -d "${PROJECTION_DIRECTORY}/${projectGroup}"; then
branchoutGroupUpdate
else
branchoutGroupClone
fi
}
function groupUpdateError() {
if projectBranch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); then
if test "${projectBranch}" = "HEAD"; then
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[38;5;202m<rebase>\\033[0m"
else
echo -e "\\033[0m${2}\\033[31m${projectGroup}\\033[0m \\033[70D\\033[70C\\033[1m $( git pull --rebase origin 2>&1| head -n1)\\033[0m"
fi
else
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[38;5;202m<empty>\\033[0m"
fi
}
function branchoutGroupUpdate() {
cd "${PROJECTION_DIRECTORY}/${projectGroup}" || usage "Failed to enter group directory"
if test ! -d "${PROJECTION_DIRECTORY}/${projectGroup}/.git"; then
echo -e "\\033[0mNothing ${projectGroup}\\033[70D\\033[70C\\033[35mNot a repository\\033[0m"
elif git pull --rebase origin > /dev/null 2>&1; then
groupStatus "${projectGroup}" "Pulled "
else
groupUpdateError "${projectGroup}" "Failed "
fi
}
function branchoutGroupClone() {
test -d "${PROJECTION_DIRECTORY}/${projectGroup}" && usage "Trying to clone a group that already exists"
echo -e "\\033[0mCloning \\033[92m${projectGroup}\\033[0m"
if git clone "${BRANCHOUT_GIT_BASEURL}/${projectGroup}" "${PROJECTION_DIRECTORY}/${projectGroup}" >/dev/null 2>&1; then
groupStatus "${projectGroup}" "Pulled "
else
echo -e "\\033[0mCloning failed: \\033[31m$(git clone "${BRANCHOUT_GIT_BASEURL}/${projectGroup}" 2>&1)\\033[0m"
echo -e "\\033[0mJust using plain directory \\033[32m${projectGroup}\\033[0m"
mkdir "${PROJECTION_DIRECTORY}/${projectGroup}"
fi
}
function branchoutGroups() {
"${BRANCHOUT_PATH}/branchout-project" list "${1}" | "${BRANCHOUT_PATH}/branchout-group" derive --stdin | sort | uniq
}
function branchoutGroupDerive() {
if test "${1#*-}" = "maven-plugin"; then
echo "plugins"
elif test -z "${BRANCHOUT_PREFIX}"; then
echo "${1%%-*}"
else
noPrefix="${1#"${BRANCHOUT_PREFIX}"-}"
echo "${noPrefix%%-*}"
fi
}
function groupStatus() {
projectGroup="${1}"
if test -d "${PROJECTION_DIRECTORY}/${projectGroup}"; then
cd "${PROJECTION_DIRECTORY}/${projectGroup}" || usage "Failed to enter group directory"
if projectBranch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); then
if test "${projectBranch}" = "master"; then
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C${projectBranch}\\033[0m"
elif test "${projectBranch}" = "HEAD"; then
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[38;5;202m<rebase>\\033[0m"
else
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[32m${projectBranch}\\033[0m"
fi
else
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[38;5;202m<empty>\\033[0m"
fi
cd "${PROJECTION_DIRECTORY}"
else
echo -e "\\033[0m${2}${projectGroup}\\033[70D\\033[70C\\033[35mNot cloned\\033[0m"
fi
}
function branchoutProjectDerive() {
echo "${1}"
}
function usage() {
test -n "${1}" && echo "${1}" && echo
echo "branchout-project list|status|pull [project]
status project-name
Return the status of the given project, Not cloned, branch, rebasing
pull project-name
Attempt to pull the project, show the branch or error
list [prefix]
List all the projects with the given prefix
"
exit 1
}
function main() {
test $# -gt 0 || usage
BRANCHOUT_PATH="$(dirname "$0")"
# shellcheck source=branchout-configuration
. "${BRANCHOUT_PATH}/branchout-configuration"
# shellcheck source=branchout-environment
. "${BRANCHOUT_PATH}/branchout-environment"
case $1 in
derive)
if test "${2}" = "--stdin"; then
branchoutGroupDeriveStdin
else
branchoutGroupDerive "${2}"
fi
;;
deriveProject)
branchoutProjectDerive "${2}"
;;
pull)
test -n "${2}" || usage "Must provide group"
if test "${BRANCHOUT_NAME}" != "${2}"; then
branchoutGroupPull "${2}"
fi
;;
list)
branchoutGroups "${2}"
;;
status)
branchoutGroupStatus "${2}"
;;
*)
usage
;;
esac
}
main "${@}"