-
Notifications
You must be signed in to change notification settings - Fork 2
/
g.zsh
801 lines (632 loc) · 19 KB
/
g.zsh
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# ================================================
# G ==============================================
# ================================================
# ------------------------------------------------
# COLORS -----------------------------------------
# ------------------------------------------------
# 30 - black
# 31 - red
# 32 - green
# 33 - yellow
# 34 - blue
# 35 - magenta
# 36 - cyan
# 37 - white
#
# 0 - normal
# 1 - bold
# 2 - normal again
# 3 - background color
# 4 - underline the text
# 5 - blinking
# http://stackoverflow.com/questions/16843382/colored-shell-script-output-library
RCol='\e[0m' # Text Reset
# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m';
Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m';
Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m';
Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m';
Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m';
Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m';
Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m';
Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m';
# TODO: g cmp should check to see if the last commit had the same message as the
# one you're trying to create and it should prompt.
# ------------------------------------------------
# CONSTANTS --------------------------------------
# ------------------------------------------------
_VERBOSE=false
# ------------------------------------------------
# GLOBALS ----------------------------------------
# ------------------------------------------------
_matches=()
_bodies=()
_commands_length=0
# Find the Git completion file path
_git_completion_path=`echo -n ${^fpath}/_git(N)`
# ------------------------------------------------
# CONFIG->ZSH ------------------------------------
# ------------------------------------------------
# We source the existing Git rules here so we have access to various
# autocompletion rules.
if [[ -f $_git_completion_path ]] ; then
source $_git_completion_path
fi
# Append g auxiliary scripts to $PATH
export PATH=$PATH:$(dirname $0)/bin
function _g_eza_tree() {
local options=(--icons --classify --oneline --long --no-permissions --no-user --time-style relative --git --level 1 --tree)
[[ $1 == 't' ]] && options+=(--sort time --reverse)
eza "${options[@]}"
}
_g_ls()
{
if [[ `uname` == "Darwin" && -n `which eza` ]] ; then
_g_eza_tree t
else
ls -lh --color
fi
}
# Change working directory Zsh rule
_g_chpwd()
{
# Check for shell level (SHLVL) beacuse we don't want this happening in scripts.
#
# Check for $0 to be 'zsh' because we don't want this happening in functions.
# FIX: Doesn't work. ^^^
#if [[ $SHLVL -lt 1 || $SKIP_CHPWD == true ]] ; then
if [ -n "${SKIP_CHPWD+x}" ]; then
if ( $SKIP_CHPWD ) ; then
return 0
fi
fi
# FIX: IMPORTANT: This is specific to you...
if [[ $SHLVL == 1 || -n $TMUX ]] ; then
_g_ls
fi
if ( `pwd-is-git-repo --root` ) ; then
git-status-display
fi
if ( `pwd-is-git-repo --root` ) ; then
echo -e "\033[30;1mpwd:\033[0m \033[33;2m`pwd-tilde`\033[0m"
fi
if [[ -f .directory-metadata ]] ; then
source .directory-metadata
_metadata_title=`echo -n "${BIRed}$_directory_title${RCol}"`
_metadata_title_length=${#_directory_title}
_metadata_bar=`head -c $_metadata_title_length < /dev/zero | tr '\0' '-'`"------"
if [[ -z $_directory_title_color ]] ; then
_directory_title_color="red"
fi
echo
#echo "${On_IWhi}$_metadata_bar${RCol}"
print -P '%B%F{$_directory_title_color}$_directory_title%f%b'
#echo "${On_IWhi}$_metadata_bar${RCol}"
echo
fi
}
# ------------------------------------------------
# UTILITY->SHELL ---------------------------------
# ------------------------------------------------
function _echo_verbose()
{
if ( $_VERBOSE ) ; then
echo $*
fi
}
function _read_prompt_response()
{
read _response
if [[ $_response == "y" ]] ; then
return 0
fi
return 1
}
function _prompt_success()
{
echo -en "\033[32;1m$1 (y/n) \033[0m"
_read_prompt_response
}
function _prompt_warning()
{
echo -en "\033[33;1m$1 (y/n) \033[0m"
_read_prompt_response
}
function _prompt_danger()
{
echo -en "\033[31;1m$1 (y/n) \033[0m"
_read_prompt_response
}
# ------------------------------------------------
# UTILITY->GIT -----------------------------------
# ------------------------------------------------
function _git_prompt_if_untracked_files()
{
# Any untracked files?
if [[ -n `git ls-files --other --exclude-standard` ]] ; then
# Show them
echo $g_lso
_prompt_warning "Untracked files exist. Commit anyways?"
return $?
fi
# There were no untracked files
return 0
}
function _prompt_and_force_push()
{
if ( _prompt_danger "Force push this branch '`git-branch-current`' to your default remote? WARNING: This is potentially destructive." ) ; then
git push --force
fi
}
# ------------------------------------------------
# COMMANDS ---------------------------------------
# ------------------------------------------------
function _define_command()
{
# Push the match and body for that match
_matches+=($1)
shift
_bodies+=($*)
let "_commands_length += 1"
}
function _find_command()
{
_find_match=$1
# FIX: This is breaking unless it's <= instead of <... Why?
for (( index = 0 ; index <= $_commands_length ; index++)) ; do
if [[ $_find_match == $_matches[$index] ]] ; then
echo $_bodies[$index]
return 0
fi
done
return 1
}
# ------------------------------------------------
# DEFINE->HELPERS --------------------------------
# ------------------------------------------------
# FIX: Use git-is-clean-work-tree
function _git_is_clean_work_tree() {
git rev-parse --verify HEAD >/dev/null || return 1
git update-index -q --ignore-submodules --refresh
if ! git diff-files --quiet --ignore-submodules ; then
return 1
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD -- ; then
return 1
fi
# Are there untracked files?
if [[ `git-count-untracked` -gt 0 ]] ; then
return 1
fi
return 0
}
#function _git_status_display()
#{
## FIX: this says that the dir is clean when we deleted some files and when we git-mv files. probably more
#git_count_untracked=`git-count-untracked`
#git_count_branches=`git branch | wc -l | awk '{print $1}'`
#git_branch_current=`git-branch-current`
#echo -en "\033[30;1mgit:\033[0m "
## Show branches
#echo -e "branch: \033[37;1m"$git_branch_current"\033[0m("$git_count_branches")"
## Show status
#echo -en " \033[37;mstatus:\033[0m "
## Check status
#_git_is_clean_work_tree
#if [[ $? == 0 ]] ; then
#echo -e "\033[32;1mclean\033[0m "
#else
#echo -e "\033[31;1munclean\033[0m "
#fi
## Show diff
#echo -e " \033[37;mdiff:\033[0m \033[37;1m"`git diff --shortstat`"\033[0m"
#git diff --numstat | sed "s/^/ /"
## Show untracked
#echo -e " \033[37;muntracked:\033[0m \033[37;1m"$git_count_untracked"\033[0m"
#g lso | sed "s/^/ /"
#}
function _git_all_tracked_or_prompt()
{
local g_lso
g_lso=`git ls-files --other --exclude-standard`
if [[ -n $g_lso ]] ; then
echo $g_lso
if ( _prompt_success "Add these files to be tracked?" ) ; then
git add .
fi
fi
}
function _git_remove_untracked_prompt()
{
local g_lso
g_lso=`git ls-files --other --exclude-standard`
if [[ -n $g_lso ]] ; then
echo $g_lso
echo -en "\033[31;1mPermanently remove these files (y/n)?\033[0m "
read add_files
if [[ $add_files == "y" ]] ; then
# FIX: Does this handle directories? I think so...
echo $g_lso | xargs rm
fi
fi
}
function _git_fallback()
{
_echo_verbose "g: Falling back to git with 'git $*'"
eval "git $*"
}
# ------------------------------------------------
# DEFINE->COMMANDS->ALIASES ----------------------
# ------------------------------------------------
# Define the commands and also define special
# special _git-* functions for aliases. See /usr/share/zsh/functions/Completion/Unix/_git for documentation on this.
_define_command g "git grep"
function _git-g () {
_git-grep
}
_define_command l "git log"
function _git-l () {
_git-log
}
_define_command ls "git ls-files"
function _git-ls () {
_git-ls-files
}
_define_command b "git branch"
function _git-b () {
_git-branch
}
_define_command d "git diff"
function _git-d () {
_git-diff
}
_define_command s "git status"
function _git-s () {
_git-status
}
_define_command ps "git push"
function _git-ps () {
_git-push
}
_define_command pl "git pull"
function _git-pl () {
_git-pull
}
_define_command co "git checkout"
function _git-co () {
_git-checkout
}
_define_command t "git tag"
function _git-t () {
_git-tag
}
_define_command ss "git status --short"
_define_command psa "git push --all"
_define_command lso "git ls-files --other --exclude-standard"
_define_command lsd "git ls-files --deleted"
_define_command a "git auto"
_define_command ff "git flow feature"
_define_command u "git up"
_define_command rnm "git branch --remote --no-merged"
_define_command rdf "git-rebase-develop-into-feature-branches"
_define_command rfm "git-rebase-from master"
_define_command rfmy "git-rebase-from master --yes"
_define_command asd "git-auto-smart-diff"
_define_command eg "git-edit-grep"
# IDEA: Add g cfp which makes a message with the files changed appended to the
# message.
# ------------------------------------------------
# DEFINE->COMMANDS->SPECIAL ----------------------
# ------------------------------------------------
# Untracked file handling
_define_command au "_git_all_tracked_or_prompt"
_define_command ru "_git_remove_untracked_prompt"
# Commit with message
_define_command cm "_git_commit_with_message"
_define_command cmp "_git_commit_with_message -p"
# Commit with short stat diff
_define_command cs "_git_commit_with_message -s"
_define_command csp "_git_commit_with_message -p -s"
# Commit with auto smart diff
_define_command ca "_git_commit_with_message -a"
_define_command cap "_git_commit_with_message -p -a"
# Commit with line diff
_define_command cl "_git_commit_line_diff"
_define_command clp "_git_commit_line_diff -p"
# Shell commands
# FIX: Make this _git_execute and "x"
_define_command c "_git_command"
# Chained grep
_define_command cg "_git_chained_grep"
# Ammend message
_define_command am "_git_ammend_message"
# Rebase Continue (adds and continues)
_define_command rc "_git_rebase_continue"
# cg: Chained Grep
# ------------------------------------------------
# Run a command and commit with the message of the command.
function _git_chained_grep()
{
query_string=""
multi_grep="(`echo $* | tr " " "|"`)"
for query in $*
do
query_string+=$query" | grep "
done
query_string+="--color -E '$multi_grep'"
eval "git grep $query_string"
}
# c: Git Command
# ------------------------------------------------
# Run a command and commit with the message of the command.
function _git_command()
{
initial_arguments=$*
# Execute all the arguments
$*
# Command was successful; Continue
if [[ $? == 0 ]] ; then
_git_all_tracked_or_prompt
git commit --all -m $initial_arguments
# Command failed
else
echo "fatal: Command failed. Not commiting."
return 1
fi
}
# c: Git Commit with Message
# ------------------------------------------------
# Commit all with a message and possibly push.
function _git_commit_with_message()
{
# Parse arguments
# FIX: Really? What's a better way to get opts as booleans?
zparseopts -- p=push s=status_as_message a=auto_as_message
_push=false
_status_as_message=false
_auto_as_message=false
if [[ $push == "-p" ]] ; then
_push=true
shift
fi
if [[ $status_as_message == "-s" ]] ; then
_status_as_message=true
shift
fi
if [[ $auto_as_message == "-a" ]] ; then
_auto_as_message=true
shift
fi
# Set commit message to something special?
local _commit_message
# Short stat:
if ( $_status_as_message ) ; then
# Replace line breaks with ; and chomp the trailing ;
_commit_message=`echo \`git status -s | tr "\n" ";"\` | sed "s/;$//g"`
# Auto smart diff:
elif ( $_auto_as_message ) ; then
local _prefix
local _auto_smart_diff
_prefix=""
_auto_smart_diff=`git-auto-smart-diff --short`
if [[ -z $_auto_smart_diff ]] ; then
echo "fatal: Cannot commit an auto smart diff if the auto smart diff is empty."
return 1
fi
# If we have more arguments prefix them to the commit message.
if [[ -n $* ]] ; then
_prefix="$*: "
fi
_commit_message="$_prefix$_auto_smart_diff"
# Otherwise, just the argument
else
_commit_message=$1
shift
# If we still have arguments, we want to only commit
# these files.
_select_files=""
if [[ $# -gt 0 ]] ; then
_select_files="$*"
fi
fi
# Check for commit message
if [[ -z $_commit_message ]] ; then
echo "fatal: Cannot commit with an empty message."
return 1
fi
# Confirm short stat commit message
if ( $_status_as_message ) ; then
echo $_commit_message
if ( ! _prompt_success "Commit with this short stat as message?" ) ; then
return 1
fi
fi
# Any untracked files?
_git_prompt_if_untracked_files
# Perform the commit
if [[ $? == 0 ]] ; then
# We're committing all files
if [[ -z $_select_files ]] ; then
git commit --all --message "$_commit_message"
# We're only commiting certain files
else
git commit --message "$_commit_message" $_select_files
fi
# Perform the push?
if ( $_push ) ; then
git push
fi
# Status display
g
fi
}
# cl: Git commit line diff
# ------------------------------------------------
# Commit all with a message of only the lines changed.
function _git_commit_line_diff()
{
zparseopts -- p=push s=status_as_message
_push=false
if [[ $push == "-p" ]] ; then
_push=true
fi
# Check if we have anything to commit.
if ( _git_is_clean_work_tree) ; then
echo "Nothing to commit."
return 1
fi
# Diff the repository to create the message:
#
# First, match the lines that begin with + or -... Then, omit the filename
# descriptor lines (i.e., +++ or ---).
local _commit_message
_commit_message=`git diff | grep -E "^\+|^\-" | grep -vE "^\+{3}|^\-{3}"`
if [[ $_commit_message == "" ]] ; then
echo "fatal: Cannot commit with an empty message (i.e., there is no line diff)."
return 1
fi
# FIX: This will echo in color if shell code contains color characters.
echo $_commit_message
if ( _prompt_success "Commit with this line diff as message?" ) ; then
git commit --all --message "$_commit_message"
if ( $_push ) ; then
git push
fi
fi
}
# am: Ammend message
# ------------------------------------------------
# Ammend the last commit message and prompt to force push.
function _git_ammend_message()
{
local _commit_message
if [[ -n $1 ]] ; then
_commit_message=$1
else
echo "Your latest commit is:"
echo
git log -n 1
echo
echo -n "Enter your ammended commit message: "
read _commit_message
fi
if ( git commit --all --amend --message "$_commit_message" ) ; then
echo
echo "Ammended."
echo
_prompt_and_force_push
fi
}
# rc: Rebase continue
# ------------------------------------------------
function _git_rebase_continue()
{
if ! git add .; then
echo "fatal: Could not add files to track."
exit 1
fi
git rebase --continue
}
# ------------------------------------------------
# USAGE/HELP -------------------------------------
# ------------------------------------------------
function _usage()
{
echo "usage: g [<aliased g command>|<git command>]"
echo
echo "The g git aliases are:"
for (( index = 1; index <= $_commands_length ; index++ )) ; do
echo " "$_matches[index]"\t"$_bodies[index]
done
}
# ------------------------------------------------
# GIT-COMMAND (GC) -------------------------------
# ------------------------------------------------
# Run a command and commit with the message of the command.
# FIX: Also add gcp which pushes after.
#
# This should be "g x" for git execute
function gc()
{
initial_arguments=$*
# Execute all the arguments
$*
if [[ $? == 0 ]] ; then
g_lso=`git ls-files --other --exclude-standard`
if [[ -n $g_lso ]] ; then
echo $g_lso
if ( _prompt_success "Add these files to be tracked?" ) ; then
git add .
fi
fi
git commit --all -m $initial_arguments
else
echo "fatal: Command failed. Not commiting."
return 1
fi
}
# ------------------------------------------------
# MAIN/G -----------------------------------------
# ------------------------------------------------
function g()
{
local _g_command
local _original_arguments
local _found_body
_g_command=$1
_original_arguments=$@
# With no arguments, print g's status
if [[ -z $_g_command ]] ; then
git-status-display
return 0
fi
# Check for --help
if [[ $_g_command == "--help" ]] ; then
_usage
return 0
fi
# Shift to get the arguments sans "g"
shift
# With arguments, attempt to find the command based on the match
_found_body=`_find_command $_g_command`
# Command was found
if [[ $? == 0 ]] ; then
_echo_verbose "g: $_found_body $*"
# Concatenate the found body and the quoted arguments passed to the proper
# 'g' function.
eval "$_found_body ${(q)@}"
# Return whatever the eval returned
return $?
fi
# Command was not found, fallback to git
# FIX: Multiple args are broken
_git_fallback $_original_arguments
}
# ------------------------------------------------
# COMPLETION -------------------------------------
# ------------------------------------------------
# Actually set the compdef to git
compdef g=git
#compctl -k "(ls lso lsd d cm cmp cpdm d s p cg)" g # FIX: compdef overrides these
#function _g () {
#local _ret=1
#local cur cword prev
#cur=${words[CURRENT]}
#prev=${words[CURRENT-1]}
#cmd=${words[2]}
#let cword=CURRENT-1
#emulate zsh -c _git
##case "$cmd" in
##ls)
##emulate zsh -c _git-ls-files
##;;
##g)
##emulate zsh -c _git-grep
##;;
##*)
##emulate zsh -c _git
##;;
##esac
#let _ret && _default && _ret=0
#return _ret
#}