-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_functions.sh
More file actions
executable file
·206 lines (188 loc) · 7.47 KB
/
base_functions.sh
File metadata and controls
executable file
·206 lines (188 loc) · 7.47 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
draw_chars()
{
local char=""
local char_start=0
local char_length=${1}
local char_type=${2:--}
until [ ${char_start} -eq ${char_length} ]
do
char="${char}${char_type}"
char_start=$(expr ${char_start} + 1 )
done
printf "%s" "${char}"
}
parse_git_repository_status()
{
local directory="${1}"
local old_dir="$(pwd)"
if [ -d "${directory}" ]
then
cd "${directory}" >/dev/null 2>/dev/null
git_status="$(git status -v)"
# -----------
# Local Repository Status
# -----------
# On branch main
if echo "${git_status}" | grep -q "^On branch [^ ][^ ]*$"
then
git_local_branch="$(echo "${git_status}" | grep "^On branch [^ ][^ ]*$" | sed "s/On branch //")"
elif echo "${git_status}" | grep -q "^HEAD detached at [0-9a-f][0-9a-f]*"
then
git_local_branch="[DETACHED_HEAD]"
else
git_local_branch="[ERROR]"
fi
# -----------
# Remote Repository Status
# -----------
local remote_repository_status=""
# Your branch is up to date with 'origin/main'.
if echo "${git_status}" | grep -q "^Your branch is up to date with '[^'][^']*'.$"
then
for LINE in $(echo "${git_status}" | grep "^Your branch is up to date with '[^'][^']*'.$" | sed "s/^Your branch is up to date with '\([^'][^']*\)'.$/\1\[sync]/")
do
remote_repository_status="${remote_repository_status}${LINE}\n"
done
fi
# Your branch is ahead of 'origin/main' by 2 commits.
if echo "${git_status}" | grep -qE "^Your branch is behind of '[^'][^']*' by [0-9]*[1-9] commits?.$"
then
for LINE in "$(echo "${git_status}" | grep -E "^Your branch is behind of '[^'][^']*' by [0-9]*[1-9] commits?.$" | sed "s/^Your branch is behind of '\([^'][^']*\)' by \([0-9]*[1-9]\) commits\?.$/\1[-\2]/")"
do
remote_repository_status="${remote_repository_status}${LINE}\n"
done
fi
# Your branch is behind of 'origin/main' by 2 commits.
if echo "${git_status}" | grep -qE "^Your branch is ahead of '[^'][^']*' by [0-9]*[1-9] commits?.$"
then
for LINE in "$(echo "${git_status}" | grep -E "^Your branch is ahead of '[^'][^']*' by [0-9]*[1-9] commits?.$" | sed "s/^Your branch is ahead of '\([^'][^']*\)' by \([0-9]*[1-9]\) commits\?.$/\1[+\2]/")"
do
remote_repository_status="${remote_repository_status}${LINE}\n"
done
fi
local remote_repository_status_output=""
if [ "${remote_repository_status}" = "" ]
then
remote_repository_status_output="no_remote_repository"
else
for LINE in $(echo "${remote_repository_status}" | sort)
do
remote_repository_status_output="${remote_repository_status_output}${LINE},"
done
remote_repository_status_output="$(echo "${remote_repository_status_output}" | sed "s/,$//")"
fi
# -----------
# Repository Status
# -----------
local repository_status=""
# nothing to commit, working tree clean
if echo "${git_status}" | grep -q "^nothing to commit, working tree clean$"
then
repository_status="clean"
else
# Changes not staged for commit:
if echo "${git_status}" | grep -q "^Changes not staged for commit:$"
then
repository_status="${repository_status}unstaged_files,"
fi
# Untracked files:
if echo "${git_status}" | grep -q "^Untracked files:$"
then
repository_status="${repository_status}untracked_files,"
fi
repository_status="$(echo "${repository_status}" | sed "s/,$//")"
fi
echo "${git_local_branch}:${repository_status}:${remote_repository_status_output}"
fi
cd ${old_dir} >/dev/null 2>/dev/null
}
parse_git_repository()
{
local directory="${1}"
local old_dir="$(pwd)"
if [ -d "${directory}" ]
then
cd $(dirname "${directory}") >/dev/null 2>/dev/null
directory="$(git rev-parse --show-toplevel)"
echo "git_repository:${directory}:$(parse_git_repository_status ${directory})"
if [ -f ${directory}/.gitmodules ]
then
for submodule_directory in $(cat ${directory}/.gitmodules | grep " *path = " | cut -f 2 -d "=")
do
if [ -f "${directory}/${submodule_directory}/.git" ]
then
cd ${directory}/${submodule_directory} >/dev/null 2>/dev/null
echo "git_submodule:${directory}/${submodule_directory}:$(parse_git_repository_status ${directory}/${submodule_directory})"
fi
done
fi
cd ${old_dir} >/dev/null 2>/dev/null
fi
}
render_git_repository()
{
local git_repository_list="$(echo "${1}" | grep -E "^(git_repository|git_submodule):[^:][^:]*:[^:][^:]*:[^:][^:]*:[^:][^:]*$")"
local headline_directory="Verzeichnis"
local headline_local_branch="Branch"
local headline_repository_status="Status"
local headline_remote_branch="Remote"
local length_directory=${#headline_directory}
local length_local_branch=${#headline_local_branch}
local length_repository_status=${#headline_repository_status}
local length_remote_branch=${#headline_remote_branch}
# local length_sync_status=0
local submodule_marker=" ==> "
for LINE in ${git_repository_list}
do
local type="$(echo "${LINE}" | cut -f 1 -d ":")"
local directory="$(echo "${LINE}" | cut -f 2 -d ":")"
local local_branch="$(echo "${LINE}" | cut -f 3 -d ":")"
local repository_status="$(echo "${LINE}" | cut -f 4 -d ":")"
local remote_branch="$(echo "${LINE}" | cut -f 5 -d ":")"
if [ "${type}" = "git_repository" ]
then
length_this_directory=${#directory}
else
length_this_directory=$(expr ${#directory} + ${#submodule_marker})
fi
if [ ${length_directory} -lt ${length_this_directory} ]
then
length_directory=${length_this_directory}
fi
if [ ${length_local_branch} -lt ${#local_branch} ]
then
length_local_branch=${#local_branch}
fi
if [ ${length_repository_status} -lt ${#repository_status} ]
then
length_repository_status=${#repository_status}
fi
if [ ${length_remote_branch} -lt ${#remote_branch} ]
then
length_remote_branch=${#remote_branch}
fi
done
printf "%-${length_directory}s %-${length_local_branch}s %-${length_repository_status}s %-${length_remote_branch}s\n" "${headline_directory}" "${headline_local_branch}" "${headline_repository_status}" "${headline_remote_branch}"
printf "%-${length_directory}s %-${length_local_branch}s %-${length_repository_status}s %-${length_remote_branch}s\n" "$(draw_chars ${length_directory})" "$(draw_chars ${length_local_branch})" "$(draw_chars ${length_repository_status})" "$(draw_chars ${length_remote_branch})"
echo "${git_repository_list}" | grep "^git_repository" | sort | while read LINE
do
local type="$(echo "${LINE}" | cut -f 1 -d ":")"
local directory="$(echo "${LINE}" | cut -f 2 -d ":")"
local local_branch="$(echo "${LINE}" | cut -f 3 -d ":")"
local repository_status="$(echo "${LINE}" | cut -f 4 -d ":")"
local remote_branch="$(echo "${LINE}" | cut -f 5 -d ":")"
printf "%-${length_directory}s %-${length_local_branch}s %-${length_repository_status}s %-${length_remote_branch}s\n" "${directory}" "${local_branch}" "${repository_status}" "${remote_branch}"
if echo "${git_repository_list}" | grep -q "^git_submodule:${directory}"
then
echo "${git_repository_list}" | grep "^git_submodule:${directory}" | sort | while read submodule_line
do
local type="$(echo "${submodule_line}" | cut -f 1 -d ":")"
local directory="$(echo "${submodule_line}" | cut -f 2 -d ":")"
local local_branch="$(echo "${submodule_line}" | cut -f 3 -d ":")"
local repository_status="$(echo "${submodule_line}" | cut -f 4 -d ":")"
local remote_branch="$(echo "${submodule_line}" | cut -f 5 -d ":")"
printf "%-${length_directory}s %-${length_local_branch}s %-${length_repository_status}s %-${length_remote_branch}s\n" "${submodule_marker}${directory}" "${local_branch}" "${repository_status}" "${remote_branch}"
done
fi
done
}