-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitctl.sh
executable file
·702 lines (548 loc) · 19 KB
/
gitctl.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#! /usr/bin/env bash
#>_____________________________________________________________________________
#>
#> [ gitctl.sh ]
#>
#> @@
#> @...@
#> @@@%%%@@@
#> %%% /@@..@@\
#> @%@\ .%%%%%%%%%%%@@@%%@@\
#> .%@%% /@.PushIt...%@/ .@%%@\
#> |%@%% |@..PullIt...@| .%@%/\
#> |(@%@ |@...BopIt...@| @@@/|
#> |/%@%@ \@....../%%@/ .@@..|
#> \/&@#@@@@/..._@@@@@@/ /@../
#> \((@@%%%%%%/ /////
#> \%%%%@@@@@@ |//
#> \%%%%%%@@@
#> \%%%%%@
#>
#> ABOUT:
#>
#> A simple git wrapper for basic local project operations
#>
#> By default, all printed or suppressed verbosity messages are logged to
#> local log files located in logs/ (relative to the location of the gitctl
#> script). This logging feature can be disabled (see the "USAGE" section).
#>
#> Source: https://github.com/h8rt3rmin8r/gitctl
#>
#> USAGE:
#>
#> gitctl <OPTION>
#> gitctl <OPTION> <OPERATION>
#>
#> where "OPTION" is one or more of the following:
#>
#> |
#> -d, --dump | Dump the contents of all gitctl log files to the standard
#> | output (overrides any specified "OPERATION" parameters)
#> |
#> -f, --find | Try to find a local project root directory and print
#> | the result to the standard output (overrides any
#> | specified "OPERATION" parameters)
#> |
#> -h, --help | Print this help text to the terminal (overrides
#> | verbosity and logging)
#> |
#> -l, --log | Log all verbosity messages (default)
#> |
#> -n, --no-log | Avoid writing verbosity messages to a log file
#> |
#> -p "X", |
#> --project="X" | Set the repository project directory to "X"; where "X"
#> | is the absolute path to a local project git repository
#> | (if not specified, gitctl will attempt to discover
#> | where the project is located)
#> |
#> | NOTE: You may need to wrap X in quotes if your directory
#> | path contains spaces
#> |
#> -s, --silent | Suppress verbosity messages
#> |
#> -v, --verbose | Display verbosity messages (default)
#> |
#>
#> and where "OPERATION" is one of the following:
#>
#> |
#> --bop "X", |
#> --bopit "X" | Stage all changes and subsequently push all staged
#> | changes to the remote repository (combines both the
#> | '--stage' and '--push' operations); This operation
#> | may include an optional commit message, "X"
#> |
#> --check, |
#> --checkit | Check the status of the remote repository as compared
#> | with the local one
#> |
#> --pull, |
#> --pullit | Pull the latest version of the remote repository
#> |
#> --push, |
#> --pushit | Push new updates to the remote repository (requires
#> | previous execution of '--stage')
#> |
#> --stage "X", |
#> --stageit "X" | Stage all modified, deleted, and new files (this is a
#> | required step before running '--push'); May include
#> | an optional commit message, "X"
#> |
#>
#> REFERENCE:
#>
#> # How to check the differences between local and github
#> https://stackoverflow.com/a/6000939
#>
#> # Staging all changes
#> https://stackoverflow.com/a/26982422
#>
#> ATTRIBUTION & LICENSE:
#>
#> Created by h8rt3rmin8r (161803398@email.tg) on 20200827
#>
#> Copyright 2020 Novx.ai
#>
#> Licensed under the Apache License, Version 2.0 (the "License");
#> you may not use this file except in compliance with the License.
#> You may obtain a copy of the License at
#>
#> http://www.apache.org/licenses/LICENSE-2.0
#>
#> Unless required by applicable law or agreed to in writing, software
#> distributed under the License is distributed on an "AS IS" BASIS,
#> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#> See the License for the specific language governing permissions and
#> limitations under the License.
#>
#>_____________________________________________________________________________
##-----------------------------------------------------------------------------
## Declare sub-functions
function gitctl_check() {
## Check the status of the remote repository
gitctl_logger --info "${FUNCNAME}: Running operation: git remote update"
git remote update
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: git remote update"
gitctl_logger --warning "${FUNCNAME}: Aborting remaining operations"
return ${e_c}
fi
gitctl_logger --success "${FUNCNAME}: Completed operation: git remote update"
gitctl_logger --info "${FUNCNAME}: Running operation: git status -uno"
git status -uno
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: git status -uno"
gitctl_logger --warning "${FUNCNAME}: Aborting remaining operations"
return ${e_c}
fi
gitctl_logger --success "${FUNCNAME}: Completed operation: git status -uno"
return ${e_c}
}
function gitctl_dumplog() {
## Dump the entire contents of the gitctl log file to STDOUT
if [[ ! -d "${lg_p}" ]]; then
return 1
fi
cat "${lg_p}"/* 2>/dev/null
return $?
}
function gitctl_hasgit() {
## Determine if an indicated directory has a ".git" directory inside
## If no input is detected, this function operates within the current directory
if [[ "x${1}" == "x" ]]; then
local i_n="${PWD}"
else
if [[ -d "${1}" ]]; then
local i_n=$(readlink -f "${1}")
else
return 1
fi
fi
gitctl_list_dir_names "${i_n}" \
| grep -E '^[.]git$' &>/dev/null
local e_c="$?"
echo "${e_c}"
return ${e_c}
}
function gitctl_help() {
## Print the gitctl help text to the terminal
cat "${0}" \
| grep -E '^#[>]' \
| sed 's/^..//'
return $?
}
function gitctl_list_dir_full() {
## List the absolute path to all directories within an indicated directory
## (includes hidden directories)
## If no input is detected, this function operates within the current directory
if [[ "x${1}" == "x" ]]; then
local i_n="${PWD}"
else
if [[ -d "${1}" ]]; then
local i_n=$(readlink -f "${1}")
else
return 1
fi
fi
find "${i_n}" -maxdepth 1 -type d -printf '%p\n' | tail -n +2
return $?
}
function gitctl_list_dir_names() {
## List the names of all directories within an indicated directory
## (includes hidden directories)
## If no input is detected, this function operates within the current directory
if [[ "x${1}" == "x" ]]; then
local i_n="${PWD}"
else
if [[ -d "${1}" ]]; then
local i_n=$(readlink -f "${1}")
else
return 1
fi
fi
find "${i_n}" -maxdepth 1 -type d -printf '%p\n' | tail -n +2 | sed 's/^.*\///'
return $?
}
function gitctl_logger() {
## Script logging and verbosity function
local lg_type="I" ## log message type
local lg_time="$(date '+%s')" ## log entry timestamp
local lg_name="${s_n}" ## source script name
local lg_ppid="$(gitctl_ppid)" ## parent process ID
local lg_code="${lg_x}${vb_x}" ## operations to perform
local lg_fnam="${lg_time:0:4}.log" ## full log file name
local lg_file="${lg_p}/${lg_fnam}" ## log file absolute path
## If the log file storage directory does not exist, then something went
## really wrong earlier when we tried to 'mkdir' it. Force suppress the
## log file writing operations but continue with verbosity.
if [[ ! -d "${lg_p}" ]]; then
local lg_x="N"
local lg_code="${lg_x}${vb_x}"
fi
## Overwrite the current logging type if type has been specified in the
## first input parameter and set the verbosity message colors
case "${1//[-]}" in
e|err|eror|error)
local lg_type="E"
local lg_color="R"
shift 1
;;
i|info)
local lg_type="I"
local lg_color="B"
shift 1
;;
s|succ|success)
local lg_type="S"
local lg_color="G"
shift 1
;;
w|warn|warning)
local lg_type="W"
local lg_color="O"
shift 1
;;
esac
local lg_message="$@"
if [[ "x${lg_message}" == "x" ]]; then
## Set a fallback message if no message was given
local lg_message="null"
fi
## Compose the final logging/verbosity strings and fire off the logging process
local lg_output="${lg_time}|${lg_ppid}|${lg_type}|${lg_name}|${lg_message}"
if [[ "${lg_code:1:1}" == "Y" ]]; then
case "${lg_color}" in
B)
## blue
local vc_type=$(printf "\e[0;49;94m${lg_type}\e[0m")
local vc_message=$(printf "\e[0;49;94m${lg_message}\e[0m")
;;
G)
## green
local vc_type=$(printf "\e[38;5;82m${lg_type}\e[0m")
local vc_message=$(printf "\e[38;5;82m${lg_message}\e[0m")
;;
O)
## orange
local vc_type=$(printf "\e[33m${lg_type}\e[0m")
local vc_message=$(printf "\e[33m${lg_message}\e[0m")
;;
R)
## red
local vc_type=$(printf "\e[38;5;196m${lg_type}\e[0m")
local vc_message=$(printf "\e[38;5;196m${lg_message}\e[0m")
;;
*)
## fallback to blue
local vc_type=$(printf "\e[0;49;94m${lg_type}\e[0m")
local vc_message=$(printf "\e[0;49;94m${lg_message}\e[0m")
;;
esac
local vc_output="${lg_time}|${lg_ppid}|${vc_type}|${lg_name}|${vc_message}"
fi
case "${lg_code}" in
YY)
## Write to log and STDOUT
echo "${lg_output}" >> "${lg_file}"
echo "${vc_output}" &>/dev/stderr
;;
YN)
## Write to log only
echo "${lg_output}" >> "${lg_file}"
;;
NY)
## Write to STDOUT only
echo "${vc_output}" &>/dev/stderr
;;
NN)
## Do not write to log or STDOUT
return 2
;;
esac
return $?
}
function gitctl_parentdir_path() {
## Isolate the parent directory to some input directory reference or name
if [[ "x${1}" == "x" ]]; then
return 1
fi
local i_n="${1}"
local out="${i_n%\/*}"
if [[ "x${out}" == "x" ]]; then
return 1
else
echo "${out}"
return 0
fi
}
function gitctl_ppid() {
## Print the parent process ID with a seven-digit zero padding
printf '%07d\n' "${PPID}"
return $?
}
function gitctl_projecthome() {
## Determine the absolute path to the current project home
##
## Search around to see if the project home is nearby
##
## Use both the script directory and the invoking user's current directory
## as a starting point and check up to three parent directories above both
## locations while searching for a .git directory.
##
local da_a="${s_d}"
local da_b=$(gitctl_parentdir_path "${da_a}")
local da_c=$(gitctl_parentdir_path "${da_b}")
local db_a="${h_n}"
local db_b=$(gitctl_parentdir_path "${db_a}")
local db_c=$(gitctl_parentdir_path "${db_b}")
declare -a search_places=( "${da_a}" "${da_b}" "${da_c}" "${db_a}" "${db_b}" "${db_c}" )
local dir_found="N"
gitctl_logger --info "${FUNCNAME}: No project home specified. Searching for project home ..."
for i in "${search_places[@]}"; do
local cycle_dir="${i}"
local cycle_check=$(gitctl_hasgit "${cycle_dir}")
if [[ "${cycle_check}" == "0" ]]; then
local dir_found="Y"
break
fi
done
if [[ "${dir_found}" == "Y" ]]; then
gitctl_logger --success "${FUNCNAME}: Project home found: ${cycle_dir}"
echo "${cycle_dir}"
return 0
else
gitctl_logger --error "${FUNCNAME}: Unable to find a valid repository home"
echo "NULL"
return 1
fi
}
function gitctl_pull() {
## Pull the latest version of hotwire
gitctl_logger --info "${FUNCNAME}: Beginning operation"
git pull origin main
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: 'git pull origin main'"
gitctl_logger --warning "${FUNCNAME}: Aborting remaining operations"
return ${e_c}
fi
gitctl_logger --success "${FUNCNAME}: Completed operation: git pull origin main"
return ${e_c}
}
function gitctl_push() {
## Push updates to the hotwire repository
gitctl_logger --info "${FUNCNAME}: Beginning operation"
git push origin main
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: 'git push origin main'"
gitctl_logger --warning "${FUNCNAME}: Aborting remaining operations"
return ${e_c}
fi
gitctl_logger --success "${FUNCNAME}: Completed operation: git push origin main"
return ${e_c}
}
function gitctl_stageall() {
## Stage all modified, deleted, and new files in git
gitctl_logger --info "${FUNCNAME}: Beginning operation"
git add --all
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: 'git add --all'"
gitctl_logger --error "${FUNCNAME}: Operation aborted while INCOMPLETE"
return ${e_c}
fi
if [[ "x$@" == "x" ]]; then
## No comments were detected; Auto-add a comment string
local str_message="Generated by gitctl: $(date '+%s%N')"
else
local str_message="$@"
fi
git commit -m "${str_message}"
local e_c="$?"
if [[ "${e_c}" -ne 0 ]]; then
gitctl_logger --error "${FUNCNAME}: Encountered an error while executing: ${_q1}git commit -m ${_q2}${str_message}${_q2}${_q1}"
gitctl_logger --error "${FUNCNAME}: Operation aborted while INCOMPLETE"
return ${e_c}
fi
gitctl_logger --success "${FUNCNAME}: All updates have been staged"
gitctl_logger --info "${FUNCNAME}: Inserted commit message: ${str_message}"
return ${e_c}
}
##-----------------------------------------------------------------------------
## Declare variables
h_n="${PWD}" ## here now (current directory)
p_h="" ## project home directory
s_p=$(readlink -f "${0}") ## script absoute path
s_d="${s_p%\/*}" ## script home directory
s_f="${0//*\/}" ## script file name
s_n="${s_f%[.]sh}" ## script short name
lg_x="Y" ## is logging turned on
vb_x="Y" ## is verbosity turned on
lg_r="logs" ## log files relative path
lg_p="${s_d}/${lg_r}" ## log files absolute path
##-----------------------------------------------------------------------------
## Execute operations
mkdir -p "${lg_p}"
if [[ "x${1}" == "x" ]]; then
## kill the script if no inputs are present
gitctl_logger --error "main: No inputs were detected"
gitctl_logger --info "main: Use --help for more information"
exit 1
fi
## process core options inputs
in_test=$(grep -E '^-{1,2}((d|f|h|H|l|n|p|s|v)$|(dump|find|help|log|nolog|project|silent|verbose))' <<<"${1}" &>/dev/null; echo $?)
while [[ "${in_test}" -eq 0 ]]; do
cycle_base_pre="${1#[-]}"
cycle_base="${cycle_base_pre#[-]}"
cycle_prefix_pre="${cycle_base//[=]*}"
cycle_prefix="${cycle_prefix_pre//[-]}"
cycle_suffix="${cycle_base#*[=]}"
case "${cycle_prefix}" in
d|dump)
vb_x="N"
gitctl_dumplog
exit $?
;;
f|find)
vb_x="N"
gitctl_projecthome
exit $?
;;
h|H|help)
gitctl_help
exit $?
;;
l|log)
lg_x="Y"
;;
n|nolog)
lg_x="N"
;;
p|project)
p_h=$(readlink -f "${cycle_suffix}")
if [[ ! -d "${p_h}" ]]; then
gitctl_logger --error "main: Indicated project directory does not exist: ${p_h}"
gitctl_logger --info "main: Use --help for more information"
exit 1
fi
;;
s|silent)
vb_x="N"
;;
v|verbose)
vb_x="Y"
;;
esac
shift 1
in_test=$(grep -E '^-{1,2}((d|f|h|H|l|n|p|s|v)$|(dump|find|help|log|nolog|project|silent|verbose))' <<<"${1}" &>/dev/null; echo $?)
done
## kill the script if no further inputs were supplied
if [[ "x${1}" == "x" ]]; then
gitctl_logger --error "main: No git process was indicated so no changes were made"
gitctl_logger --info "main: Use --help for more information"
exit 1
fi
## discover the project home directory if it still remains undefined
if [[ ! -d "${p_h}" ]]; then
p_h="$(gitctl_projecthome)"
if [[ "${p_h}" == "NULL" ]]; then
## kill the script if no project home directory could be found
exit 1
fi
fi
## process the main git operation indicated by the user
case "${1//[-]}" in
b|bop|bopit)
shift 1
cd "${p_h}"
if [[ "$#" -gt 0 ]]; then
gitctl_stageall "$@"
else
gitctl_stageall
fi
gitctl_push
e_c="$?"
cd "${h_n}"
;;
c|check|checkit)
shift 1
cd "${p_h}"
gitctl_check
e_c="$?"
cd "${h_n}"
;;
pull|pullit)
shift 1
cd "${p_h}"
gitctl_pull
e_c="$?"
cd "${h_n}"
;;
push|pushit)
shift 1
cd "${p_h}"
gitctl_push
e_c="$?"
cd "${h_n}"
;;
stage|stageit|stageall)
shift 1
cd "${p_h}"
if [[ "$#" -gt 0 ]]; then
gitctl_stageall "$@"
else
gitctl_stageall
fi
e_c="$?"
cd "${h_n}"
;;
*)
gitctl_logger --error "main: An unknown git operation was specified: ${1}"
exit 1
;;
esac
exit ${e_c}