-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.shell
282 lines (248 loc) · 9.03 KB
/
git.shell
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/env bash
#
# Copyright (c) Roberto A. Foglietta, 2022-2023
#
# Authors:
# Roberto A. Foglietta <[email protected]>
#
# SPDX-License-Identifier: GPLv3
#
#set -x && resetx="set +x" # RAF: source, paired with set +x at the end of file
set -u # RAF: only for devel, comment in main
if [ ${GFRELOAD:-0} -eq 0 ]; then
echo "${BASH_SOURCE[0]##*/} not reloaded, try again with 'GFRELOAD=1 source'"
return 1
fi
type gitshell 2>&1 | grep -q "is a function" && return 0
trap 'echo -e "\n${ERROR:-ERROR}: in '${BASH_SOURCE[0]##*/}\
' at line ${LINENO} occured, try again with set -x\n" >&2' ERR
###############################################################################
function gitshell() { false; }; export -f gitshell
if [ ${GITSHLVL:-0} -lt 1 ]; then
# RAF: restricted shell cannot redirect to file but it is fine
# to redirect to a open file descriptor towards /dev/null
# So, also exec could fail but in gitshell &3 is just open
exec 3>/dev/null || return $?
SRC_FILE=${SRC_FILE:-${gfreload_pathname:-}}
SRC_FILE=${SRC_FILE:-$(readlink -f ${BASH_SOURCE[0]})}
export gfbasicenv_pathname="${SRC_FILE%/*}/git.basicenv"
GFRELOAD=1 source "${gfbasicenv_pathname}" || return $?
: #RAF: the line above will be stripped
fi #>&3
#RAF: git functions wrap layer available only in gitshell, true in devel
if false; then
if ! type _op_action 2>&3 >&3; then
echo "gitshell p2: $gfunctions_pathname"
GFRELOAD=1 source "${gfunctions_pathname}" || return $?
: #RAF: the line above will be stripped
fi #>&3
fi
echo "Loading ${BASH_SOURCE[0]##*/} into current bash enviroment"
###############################################################################
if ! type grnc >&3 2>&3; then
function grnc() { command grep --color=never "$@"; }
fi
gitglobign=$(command git config --global --get core.excludesfile)
if [ -z "${gitglobign}" ]; then
git config --global core.excludesfile "$HOME/.gitignore"
gitglobign=$(command git config --global --get core.excludesfile)
fi
if [ -n "${gitglobign}" ]; then
touch "${gitglobign}"
if ! grnc -q "^${gf_gitshell_rcinit}\*$" "${gitglobign}"; then
echo "${gf_gitshell_rcinit}"'*' | tee -a "${gitglobign}" >&3
fi
fi
unset gitglobign
###############################################################################
function ps1p() {
echo 'export PS1="\[${nlcyn}\]${gitshell_prefix:-+}\[${crst}\]:\[${nlgrn}\]git-'\
'\$( (opst||echo shell) 2>&3)\[${crst}\]:\[${nlylw}\]\$( (bcur -c||hcur) 2>&3)\[${crst}\]'\
':\[${nlred}\]${PWD##*/}\[${crst}\]> "'
}
export -f ps1p
function ps1s() {
eval "$(ps1p)"
}
export -f ps1s
function _gitshell() {
function stripsrc() {
# This function changed name and these strings have been splited by ""
# because they should not fit into the grep -ve cancellation, bugfix.
local va="source.*g""fcolors_pathname"
local vb="source.*g""functions_pathname"
local vc="source.*g""itshell_pathname"
local vd="source.*g""fbasicenv_pathname"
echo -e "\nGFRELOAD=1 ## START IMPORTING $1"
# This function should also work before git.functions, so it uses grep
command grep --color=never -ve "$va" -ve "$vb" -ve "$vc" -ve "$vd" "$1"
echo -e "GFRELOAD=0 ### STOP IMPORTING $1\n"
}
local gitshrc="./${gf_gitshell_rcinit}"
export gitshell_prefix=${gitshell_prefix:-}
if [ "x${gitshell_prefix:0:1}" == "x+" -o -z "${gitshell_prefix}" ]; then
gitshell_prefix="+${gitshell_prefix}"
fi
if [ ! -e "${gitshrc}" ]; then
if true; then
echo "#!/bin/env bash"
echo "declare -rx GITSHELL=1 2>&1 | head -n0"
echo "export SRC_FILE='${gfreload_pathname}'"
stripsrc ${gfbasicenv_pathname}
stripsrc ${gfcolors_pathname}
stripsrc ${gitshell_pathname}
stripsrc ${gfunctions_pathname}
echo "export gfunctions_pathname='${gitshrc}'"
echo 'gfreload() { echo -e "\n'$WARNING': reload is not allowed is this gitshell\n"; }'
echo 'gfupdate() { echo -e "\n'$WARNING': update is not allowed is gitshell\n"; }'
echo 'exit() { command exit ${1:-} 2>&3; }'
echo 'gitshell() { _gitshell "$@"; }'
echo 'gfexit() { exit 123; }'
ps1p
fi | tee "${gitshrc}" >&3
chmod a-w "${gitshrc}"
fi
local tmpf=$(mktemp ${gitshrc}.XXXX)
if true; then
echo "#!/bin/env bash"
echo "trap 'rm -f $tmpf' EXIT"
echo "trap 'rm -f $tmpf; exit' QUIT KILL"
echo "GFRELOAD=1 source ${gitshrc} >&3"
GITSHLVL=$(set +u; declare -i i=${GITSHLVL:-0}; echo $i) # RAF: sanitize the integer
if [ $GITSHLVL -le 0 ]; then
echo 'gfupdate() { command exit 234 2>&3; }'
echo 'gfreload() { if [ "x${1:-}" == "x-f" ]; then shift; gfreloadafunc "$@"; return $?; fi; command exit 2>&3; }'
echo 'echo -e "${blcyn}WELCOME${crst}: enter ${blwht}gfexit${crst} to leave\n"'
echo "export GITSHLVL=1"
else
echo "export GITSHLVL=$[GITSHLVL+1]"
fi
echo "$@"
echo "set +x"
echo "declare -rxf gfreload gitshell gfupdate gfexit"
echo "echo local gitshrc: ${tmpf}; echo"
fi | tee ${tmpf} >&3
if [ ! -e "${gitshrc}" -o ! -e "${tmpf}" ]; then
rm -f "${gitshrc}" "${tmpf}"
return 1
fi
declare -i gitshellplus=${gitshellplus:-0} ret=0
export gitshellplus
let gitshellplus++
local gitbash="gitshellplus=${gitshellplus} bash --rcfile '${tmpf}' -ri"
if [ $gitshellplus -eq 1 ]; then
eval $gitbash 3>/dev/null || ret=$?
else
eval $gitbash || ret=$?
fi
rm -f ${tmpf}
let gitshellplus--
if [ ${gitshellplus} -le 0 ]; then
rm -f "${gitshrc}"
unset gitshell_prefix gitshellplus
else
gitshell_prefix="$(eval printf '+%.0s' {1..$gitshellplus})"
fi
return $ret
}
export -f _gitshell
function gitshell() {
if [ ${GITSHLVL:-0} -gt 0 ]; then
echo "${ERROR}: this is a sub-shell wrap mismatch, abort."
return 1
fi
declare -i ret=1
if [ "x${1:-}" == "x-r" ]; then
shift
ret=0
fi
local i
# for i in $(ls --color=never -1 ${gf_gitshell_rcinit}.???? 2>/dev/null); do
for i in ${gf_gitshell_rcinit}.???? ; do
command ps -aef | grnc -qe "[0-9] *bash.*rcfile.*$i" || rm -f $i
done
while [ $ret -ne 123 ]; do
if [ $ret -eq 0 ]; then
gfreload
elif [ $ret -eq 234 ]; then
gfupdate
else
echo
fi
GITSHLVL=0 _gitshell "$@"
ret=$?
done
echo
return $ret
}
export -f gitshell
function gfreloadafunc() {
test -z "${1:-}" && return 1
local f="" fn=${2:-git.functions}
declare -i n m
n=$(grnc -n "^function $1(" "$fn" 2>&3 | cut -d: -f1 | tail -n1)
m=$(grnc -n "^export -f $1" "$fn" 2>&3 | cut -d: -f1 | tail -n1)
test $n -le 0 -o $m -le 0 ||\
f=$(head -n$[m-1] "$fn" 2>&3 | tail -n$[m-n])
if [ -z "${f:0:1}" -a -z "${2:-}" ]; then
gfreloadafunc "$1" git.shell
return $?
fi
test -z "${f:0:1}" && return 1
eval "$f" && echo -e "\n${DONE}: function '$1' reloaded\n"
}
export -f gfreloadafunc
function gfreload() {
local p
if [ -z "${1:-}" ]; then
p="${gfreload_pathname}"
elif [ -r "$1" ]; then
p="$(readlink -e $1)"
test -n "$p" || return 1
grep -q "export -f _gitshell" "$p" || return 1
export gfreload_pathname="$p"
elif [ "x$1" == "x-f" ]; then
shift
gfreloadafunc "$@"
return $?
else
return 1
fi
# RAF: forget the past source file path
unset SRC_FILE
# RAF: a function that has been removed should not remain
unset -f $(gflist 2>&3 | grnc -vw $FUNCNAME) _gitshell _git
# RAF: needed to grants that every script will be reloaded
unset -f colors_codes_generation _op_action gitshell
GFRELOAD=1 source "$p" >&3 || return $?
echo -e "\n${DONE}: reload path ${gfreload_pathname%/*}\n"
}
export -f gfreload
function gfupdate() {
local tmpf=$(mktemp -p "${TMPDIR:-/tmp}" gfupdate.XXXX)
${gfrefdir_pathdir}/install.sh update 2>&1 | tee -a "$tmpf"
local strn=$(sed -ne "s,.*DONE.* install path \(.*\),\\1,p" "$tmpf")
test -z "$strn" && return 1
if [ "$gfrefdir_pathdir" != "$strn" ]; then
echo -e "${WARNING}: install and reload paths differ, no reload\n"
return 1
fi
# export gfrefdir_pathdir="$strn"
gfreload "${gfrefdir_pathdir}/git.shell" | tail -n +2
return ${PIPESTATUS[0]}
}
export -f gfupdate
function gflist() {
test -r ${gfunctions_pathname} || return 1
sed -ne "s,^export -f \([^_][^ ]*\).*,\\1,p" ${gfunctions_pathname} | sort | uniq | grnc .
}
export -f gflist
function gfhelp() {
echo -e "\n${USAGE}: $(gflist | tr '\n' ' ')\n"
echo -e "${NOTICE}: install path ${gfrefdir_pathdir}\n"
}
export -f gfhelp
###############################################################################
${resetx:-}
trap -- ERR
true