-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-commands.sh
executable file
Β·1561 lines (1363 loc) Β· 40.8 KB
/
git-commands.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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
GIT_CMDS_FILE_LOCATION="$HOME/.git-commands.sh"
# portable sed by using gnu-sed on macos
# sed -i -e ... - does not work on OS X as it creates -e backups
# sed -i'' -e ... - does not work on OS X 10.6 but works on 10.9+
# sed -i '' -e ... - not working on GNU
[[ "$(uname -s)" == "Darwin"* ]] && SED_PORTABLE="gsed" || SED_PORTABLE="sed"
#---------------------------- internal ----------------------------
function git_cmd() {
printf "\e[36mβ git \e[32m$(echo "$@" | sed 's/%/%%/g')\e[0m\n"
git $*
}
function git_cmd_err() {
printf "\e[31merror: $@\e[0m\n" >&2
false
}
function git_cmd_confirm() {
if [ -z "$1" ]; then
git_cmd_err "no question to confirm"
false
return
fi
printf "\e[33m$@ [y/N]\e[0m "
read confirm
local conf=$(echo "$confirm" | awk '{print tolower($0)}')
if [[ "$conf" == 'y' || "$conf" == 'yes' ]]; then
return
fi
false
}
# git_cmd_branch_protection || return
# git_cmd_branch_protection <branch_name> || return
# Error function to be called by other functions to prevent those
# functions from being called on main, dev, or release branches.
function git_cmd_branch_protection() {
git_cmd_help $1 && return
if [ -z "$1" ]; then
local check_branch=$(git branch --show-current)
else
local check_branch=$1
fi
if [ $check_branch = $(git_main_branch) ]; then
git_cmd_err "this command doesn't work on main"
return
elif [[ "$check_branch" == "dev"* ]]; then
git_cmd_err "this command doesn't work on a dev branch"
return
elif [[ "$check_branch" == "release"* ]]; then
git_cmd_err "this command doesn't work on a release branch"
return
fi
}
# git_cmd_branch_protection_main || return
# git_cmd_branch_protection_main <branch_name> || return
# Error function to be called by other functions to prevent those
# functions from being called on the main branch.
function git_cmd_branch_protection_main() {
git_cmd_help $1 && return
if [ -z "$1" ]; then
local check_branch=$(git branch --show-current)
else
local check_branch=$1
fi
if [ $check_branch = $(git_main_branch) ]; then
git_cmd_err "this command doesn't work on main"
return
fi
}
#---------------------------- help ----------------------------
# print comment prefix for calling function if $1 is a help flag
function git_cmd_help() {
if [[ "$1" != "-h" && "$1" != "--help" ]]; then
return 1
fi
# find calling function, bash || zsh
[[ -n $BASH_VERSION ]] && local fn_name="${FUNCNAME[1]}" || local fn_name="${funcstack[@]:1:1}"
# find line number of calling fn
local n=$(grep -n "function $fn_name()" "$GIT_CMDS_FILE_LOCATION" | cut -d : -f 1)
local help=""
while : ; do
n=$(( n - 1 ))
local line=$(sed -n $n'p' "$GIT_CMDS_FILE_LOCATION")
if [[ "$line" = "#"* ]]; then
# prettify each comment line
local line=$(printf "$line" | sed 's/^# //')
if [[ "$line" != " "* ]]; then
line=$(printf "$line" | sed 's/\(<.*>\)/\\e[35m\1\\e[36m/g' | sed 's/\(\[.*\]\)/\\e[33m\1\\e[36m/g')
line="\e[36m$line\e[0m"
else
line=$(printf "$line" | sed 's/`\(.*\)`/\\e[33m\1\\e[0m/g')
fi
help="$line\n$help"
else
# if we run out of comment lines, we're done
break
fi
done
printf "$help"
}
# display help for an alias
function git_cmd_help_alias() {
if [[ -z "$1" ]]; then
git_cmd_err "alias not specified"
return
fi
# find line number of alias
local n=$(grep -n "alias $1=" "$GIT_CMDS_FILE_LOCATION" | cut -d : -f 1)
if ! [[ "$n" =~ ^[0-9]+$ ]]; then
git_cmd_err "alias not found"
return
fi
local help=""
while : ; do
n=$(( n - 1 ))
local line=$(sed -n $n'p' "$GIT_CMDS_FILE_LOCATION")
if [[ "$line" = "#"* ]]; then
local line=$(printf "$line" | sed 's/^# / /')
help="$line\n$help"
else
# if we run out of comment lines, we're done
break
fi
done
if [ -z "$help" ]; then
git_cmd_err "no documentation found"
return
fi
printf "\e[36m$1\e[0m\n"
printf "$help"
}
# help function
function git_cmd_info() {
if [[ "$1" == "-f" || "$1" == "--functions" ]]; then
git_cmd_help_functions
elif [[ "$1" == "-l" || "$1" == "--helpers" ]]; then
git_cmd_help_helpers
elif [[ "$1" == "-a" || "$1" == "--all" ]]; then
git_cmd_help_aliases
elif [[ "$1" == "-h" || "$1" == "--help" || "$1" == "--alias" ]]; then
git_cmd_help_alias $2
else
printf "\e[30mGIT BASH SHORTCUTS\e[0m\n"
printf "\e[36mgcmd \e[33m-f\e[0m Display help for common functions.\n"
printf "\e[36mgcmd \e[33m-l\e[0m Display help for helper functions.\n"
printf "\e[36mgcmd \e[33m-a\e[0m Display help for aliases.\n"
printf "\e[36mgcmd \e[33m-h \e[37m<alias_name>\e[0m Display help for an alias.\n"
printf "\e[37m<function_name> \e[33m-h\e[0m Display help for a function.\n"
fi
}
alias gcmd='git_cmd_info'
alias gcmds='git_cmd_info'
# info on common functions
function git_cmd_help_functions() {
local -a functions=( \
"git_force_push" \
"git_switch_branch_by_search" \
"git_checkout_branch_by_search" \
"git_rebase_n_commits" \
"git_rebase_branch" \
"git_rebase_forward" \
"git_rebase_on_main" \
"git_rebase_on_branch" \
"git_squash" \
"git_squash_branch" \
"git_drop_drop_commits" \
"git_merge_ff" \
"git_merge_ff_this" \
"git_delete_branch_by_search" \
"git_reset" \
"git_reset_branch" \
"git_drop_last" \
"git_remote_reset" \
"git_wip" \
"git_wip_undo" \
"git_rename_branch" \
"git_backup_branch" \
"git_tag_push" \
"git_move_tag" \
"git_branch_num_commits" \
)
local help=""
for function in "${functions[@]}"; do
help="$help$($function -h)\n\n"
done
help="\e[30mCOMMON FUNCTIONS\e[0m\n\n$help"
if [[ "$1" == "-e" ]]; then
printf "$help"
else
printf "$help" | less -R
fi
}
# info on helper functions
function git_cmd_help_helpers() {
local -a functions=( \
"git_find_branch" \
"git_find_parent_branch" \
"git_commits_out_of_date" \
"git_commits_ahead" \
"git_commits_behind" \
"git_main_branch" \
"git_develop_branch" \
)
local help=""
for function in "${functions[@]}"; do
help="$help$($function -h)\n\n"
done
help="\e[30mHELPER FUNCTIONS\e[0m\n\n$help"
if [[ "$1" == "-e" ]]; then
printf "$help"
else
printf "$help" | less -R
fi
}
# info on aliases
function git_cmd_help_aliases() {
local -a aliases=( \
"gp" \
"ga" \
"gaa" \
"gcm" \
"gcd" \
"gcpb" \
"gco" \
"gcb" \
"gsw" \
"gswc" \
"gswm" \
"gswd" \
"gswp" \
"gb" \
"gba" \
"gbd" \
"gbnm" \
"gbr" \
"gbl" \
"gf" \
"gfo" \
"gfa" \
"gfprune" \
"gl" \
"gc" \
"gca" \
"gcam" \
"gcmsg" \
"gcf" \
"gcount" \
"grb" \
"grbi" \
"grba" \
"grbc" \
"grbs" \
"gcp" \
"gcpa" \
"gcpc" \
"gbs" \
"gbsb" \
"gbsg" \
"gbsr" \
"gbss" \
"gfg" \
"gd" \
"gdw" \
"gsb" \
"gss" \
"gst" \
"grt" \
"gcl" \
"gsi" \
"gsu" \
"glo" \
"glog" \
"gloga" \
"glol" \
"glols" \
"glola" \
"glod" \
"glods" \
"gt" \
)
local help=""
for alias in "${aliases[@]}"; do
help="$help$(git_cmd_help_alias $alias)\n"
done
help="\e[30mALIASES\e[0m\n\n$help"
if [[ "$1" == "-e" ]]; then
printf "$help"
else
printf "$help" | less -R
fi
}
#---------------------------- functions ----------------------------
# gremotereset
# git_remote_reset
# Reset current branch to remote.
function git_remote_reset() {
git_cmd_help $1 && return
if [ -z "$1" ]; then
local branch=$(git branch --show-current)
else
local branch=$1
if ! git rev-parse --quiet --verify $branch; then
git_cmd_err "branch $branch not found"
return
fi
fi
if ! git_branch_has_remote $branch; then
git_cmd_err "no remote found for $branch"
return
fi
local remote=$(git config branch.$branch.remote)
git_cmd fetch $remote $branch
git_cmd reset --hard "$remote/$branch"
}
alias gremotereset='git_remote_reset'
# git_commits_ahead
# Return number of commits current branch is ahead of remote.
function git_commits_ahead() {
git_cmd_help $1 && return
if git rev-parse --git-dir &>/dev/null; then
local commits="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
if [[ -n "$commits" && "$commits" != 0 ]]; then
echo $commits
fi
fi
}
# git_commits_behind
# Return number of commits current branch is behind remote.
function git_commits_behind() {
git_cmd_help $1 && return
if git rev-parse --git-dir &>/dev/null; then
local commits="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
if [[ -n "$commits" && "$commits" != 0 ]]; then
echo $commits
fi
fi
}
# git_find_branch <search_string>
# Find branch name by search string.
function git_find_branch() {
git_cmd_help $1 && return
if [ -z $1 ]; then
git_cmd_err "missing search string, e.g. ISSUE-1234"
return
fi
local branches=$(git branch --list "*$1*" --format '%(refname:short)')
local result_source="local"
if [ -z "$branches" ]; then
local result_source="remote"
# lstrip past the remote name
local branches=$(git branch -r --list "*$1*" --format '%(refname:lstrip=3)')
if [ -z "$branches" ]; then
git_cmd_err "unable to find branch matching: $1"
return
fi
fi
local num_results=$(echo "$branches" | wc -l | tr -d ' ')
if [ $num_results -gt 1 ]; then
git_cmd_err "unable to narrow results, $num_results $result_source matches"
if [ $num_results -lt 11 ]; then
printf "\e[33m$(echo "$branches" | sed 's/^/ /g')\e[0m\n" >&2
fi
return
fi
echo $branches
}
# git_find_local_branch <search_string>
# Find branch name by search string.
function git_find_local_branch() {
git_cmd_help $1 && return
if [ -z $1 ]; then
git_cmd_err "missing search string, e.g. ISSUE-1234"
return
fi
local branches=$(git branch --list "*$1*" --format '%(refname:short)')
if [ -z "$branches" ]; then
git_cmd_err "unable to find branch matching: $1"
return
fi
local num_results=$(echo "$branches" | wc -l | tr -d ' ')
if [ $num_results -gt 1 ]; then
git_cmd_err "unable to narrow results, $num_results matches"
if [ $num_results -lt 11 ]; then
printf "\e[33m$(echo "$branches" | sed 's/^/ /g')\e[0m\n" >&2
fi
return
fi
echo $branches
}
# git_find_remote_branch <search_string>
# Find branch name by search string.
function git_find_remote_branch() {
git_cmd_help $1 && return
if [ -z $1 ]; then
git_cmd_err "missing search string, e.g. ISSUE-1234"
return
fi
# lstrip past the remote name
local branches=$(git branch -r --list "*$1*" --format '%(refname:lstrip=3)')
if [ -z "$branches" ]; then
git_cmd_err "unable to find branch matching: $1"
return
fi
local num_results=$(echo "$branches" | wc -l | tr -d ' ')
if [ $num_results -gt 1 ]; then
git_cmd_err "unable to narrow results, $num_results matches"
if [ $num_results -lt 11 ]; then
printf "\e[33m$(echo "$branches" | sed 's/^/ /g')\e[0m\n" >&2
fi
return
fi
echo $branches
}
# gswf <search_string>
# git_switch_branch_by_search <search_string>
# Switch branch by search string, if found.
function git_switch_branch_by_search() {
git_cmd_help $1 && return
local branch=$(git_find_branch $1)
if [ -n "$branch" ]; then
git_cmd switch $branch
# git has info printed for this, but this makes it more obvious
local behind=$(git_commits_behind)
if [ -n "$behind" ]; then
printf "\e[33m$behind commits behind remote\e[0m\n"
else
local ahead=$(git_commits_ahead)
if [ -n "$ahead" ]; then
printf "\e[33m$ahead commits ahead of remote\e[0m\n"
fi
fi
fi
}
alias gswf='git_switch_branch_by_search'
# gcof <search_string>
# git_checkout_branch_by_search <search_string>
# Checkout branch by search string, if found.
function git_checkout_branch_by_search() {
git_cmd_help $1 && return
local branch=$(git_find_branch $1)
if [ -n "$branch" ]; then
git_cmd checkout $branch
fi
}
alias gcof='git_checkout_branch_by_search'
# gbdel <search_string>
# git_delete_branch_by_search <search_string>
# Switch branch by search string, if found.
function git_delete_branch_by_search() {
git_cmd_help $1 && return
# local branch?
local branch=$(git_find_local_branch $1)
if [ -n "$branch" ]; then
git_cmd_branch_protection $branch || return
git_cmd_confirm "Delete branch \e[34m$branch\e[33m?" || return
local remote=$(git config branch.$branch.remote)
git_cmd branch -D $branch
# has remote?
if [ -n "$remote" ] && git ls-remote $remote --exit-code --heads $branch; then
git_cmd_confirm "Also delete \e[34m$branch\e[33m on \e[34m$remote\e[33m?" || return
git_cmd push $remote --delete $branch
fi
else
# remote branch?
local branch=$(git_find_remote_branch $1)
if [ -n "$branch" ]; then
git_cmd_branch_protection $branch || return
local remote=$(git config branch.$branch.remote)
git_cmd_confirm "Delete branch \e[34m$branch\e[33m on \e[34m$remote\e[33m?" || return
git_cmd push $remote --delete $branch
fi
fi
}
alias gbdel='git_delete_branch_by_search'
# git_find_parent_branch
# git_find_parent_branch [-a] <branch_name>
# Find parent branch. Default behavior filters branches
# by regex, searching for main, dev, or release branches.
#
# `-a` Search all branches instead of limiting by regex.
#
# In the following example, 'release-20' would be returned:
#
# E---F---G current-branch
# /
# C---D release-20, feat--something
# /
# A---B main
function git_find_parent_branch() {
git_cmd_help $1 && return
# whether to search all branches or limit by regex (listed below)
if [ "$1" = "-a" ] || [ "$1" = "--all" ]; then
local search_all=1
shift
else
local search_all=0
fi
# start searching at start_branch
if [ -z "$1" ]; then
local start_branch=$(git branch --show-current)
else
local start_branch=$1
if ! git rev-parse --quiet --verify $start_branch >/dev/null; then
git_cmd_err "branch $start_branch not found"
return
fi
fi
# check branch isn't main
git_cmd_branch_protection_main $start_branch || return
# get all branches that aren't the current
local all_branches=( $(git rev-parse --symbolic --branches) )
# only look at branches that match these regexes
if [ $search_all -eq 0 ]; then
local -a regexes=(
"^$(git_main_branch)$" \
"^dev.*$" \
"^release.*$"
)
# filter branches by regexes
local -a candidate_branches=()
local -A branches_found
for branch in ${all_branches[@]}; do
for regex in $regexes; do
if [[ $branch != $start_branch && $branch =~ $regex && -z "${branches_found[$branch]}" ]]; then
candidate_branches+=( "$branch" )
# logging each in branches_found prevents duplicates
branches_found[$branch]=1
fi
done
done
else
local -a candidate_branches=( "${all_branches[@]}" )
fi
# `git show-branch` cannot show more than 29 branches and commits at a time
local max_branches_returned=28
local num_branches_left=${#candidate_branches[@]}
local last_count
# do while
while : ; do
local -a branches_to_check=( "${candidate_branches[@]}" )
local -a branches_narrowed=()
while [[ ${#branches_to_check[@]} -gt 0 ]]; do
# slice remaining branches to check to the max we can check at once
local -a check_branches=( "${branches_to_check[@]:0:$max_branches_returned}" )
local num_check_branches=${#check_branches[@]}
# create a map index of commits to branches
# the left column in `git show-branch` maps to the list of branches in the header, denoting
# whether the commit is on that branch by a '+' or '*'
# >> show branches we want to check in topographical order
# -> cut the header list out of the result
# -> remove everything after the symbols
# -> replace ' ' with '_'
# -> replace '*' with '+'
# -> limit to lines with '_' <= 1
# -> get the first line
# -> split each character to its own line
local map=(
$(git show-branch --topo-order "${check_branches[@]}" "$start_branch" \
| tail -n +$(($num_check_branches+3)) \
| sed "s/ \[.*$//" \
| sed "s/ /_/g" \
| sed "s/*/+/g" \
| sed "s/-/+/g" \
| egrep '^_*[^_].*[^_]$' \
| head -n1 \
| sed 's/\(.\)/\1\n/g'
)
)
# given the following list of potential branches
# main release-20
# and given the following result from `git show-branch`
# ----------------------------------------
# ! [main] main commit
# ! [release-20] release commit 2
# * [chore--current-branch] chore commit 2
# ---
# + [main] main commit
# + [release-20] release commit 2
# * [chore--current-branch] chore commit 2
# * [chore--current-branch^] chore commit
# +* [release-20^] release commit
# ++* [main^] test
# ----------------------------------------
# the resulting map _++ is derived from the following line
# +* [release-20^] release commit
# this maps to the header in the result, as
# _ 0 main
# + 1 release-20
# + 1
# narrowing main out of the candidate list
# loop through the branches, narrowing the list by whether it is in the map
local i=1
for branch in "${check_branches[@]}"; do
if [[ "${map[$i]}" == "+" ]]; then
branches_narrowed+=( $branch )
fi
((i=i+1))
done
# cut the branches we just checked out of the remaining list
branches_to_check=( "${branches_to_check[@]:$max_branches_returned}" )
done
# set candidate branches to the list we've just narrowed
last_count=$count
candidate_branches=( "${branches_narrowed[@]}" )
count=${#candidate_branches[@]}
# if we have no more branches to check, we're done
[[ $max_branches_returned -lt $num_branches_left && $num_branches_left -lt $last_count ]] || break
done
# check if we narrowed to a single result
if [[ ${#candidate_branches[@]} -gt 1 ]]; then
local -a candidates_without_main=()
# if more than one result, filter out main
for branch in "${candidate_branches[@]}"; do
if [[ "$branch" != "$(git_main_branch)" ]]; then
candidates_without_main+=( $branch )
fi
done
candidate_branches=( "${candidates_without_main[@]}" )
# if still more than one result, give up
if [[ ${#candidate_branches[@]} -gt 1 ]]; then
git_cmd_err "unable to narrow parent branch down from the following:"
for branch in "${candidate_branches[@]}"; do
printf " $branch\n" >&2
done
return
fi
fi
# we did it!
echo "${candidate_branches[@]}"
}
# git_commits_out_of_date
# git_commits_out_of_date <parent_branch>
# git_commits_out_of_date <parent_branch> <branch_name>
# Return number of commits branch is out of date from parent.
function git_commits_out_of_date() {
git_cmd_help $1 && return
if [ -n "$2" ]; then
local check_branch=$2
else
local check_branch=$(git branch --show-current)
fi
if [ -n "$1" ]; then
local parent=$1
else
local parent=$(git_find_parent_branch $check_branch)
if [ -z $parent ]; then
false
return
fi
fi
if [[ "$check_branch" == "$parent" ]]; then
git_cmd_err "comparing $check_branch to itself"
return
fi
if [[ "$check_branch" == "$(git_main_branch)" ]]; then
git_cmd_err "this command doesn't work on main"
return
fi
local commits=$(git rev-list --left-only --count $parent...$check_branch)
if [[ -n "$commits" && "$commits" > 0 ]]; then
echo $commits
fi
}
# gfp
# gfp [..]
# git_force_push [..]
# Force push with lease to remote with branch protection.
function git_force_push() {
git_cmd_help $1 && return
git_cmd_branch_protection_main || return
git_cmd push --force-with-lease $*
}
alias gfp='git_force_push'
alias gpf='git_force_push'
# gmff <branch_name>
# git_merge_ff <branch_name>
# Merge current branch with given branch, fast forward only.
function git_merge_ff() {
git_cmd_help $1 && return
if [ -z "$1" ]; then
git_cmd_err "missing argument for which branch to merge"
fi
local branch_to_merge=$1
[[ -z "$branch_to_merge" ]] && return
git update-index --refresh >/dev/null
if ! git diff-index --quiet HEAD --; then
git_cmd_err "unable to fast-forward merge, you have unstaged changes"
return
fi
local current=$(git branch --show-current)
local commits=$(git_commits_out_of_date $current $branch_to_merge)
if [[ -n "$commits" && "$commits" != 0 ]]; then
git_cmd_err "unable to fast-forward merge, $branch_to_merge is out of date by $commits commit(s)"
return
fi
git_cmd merge --ff-only $branch_to_merge
}
alias gmff='git_merge_ff'
# gmffthis
# git_merge_ff_this
# Merge current branch with parent branch, fast forward only.
function git_merge_ff_this() {
git_cmd_help $1 && return
git_cmd_branch_protection_main || return
local branch_to_merge=$(git branch --show-current)
[[ -z "$branch_to_merge" ]] && return
git update-index --refresh >/dev/null
if ! git diff-index --quiet HEAD --; then
git_cmd_err "unable to fast-forward merge, you have unstaged changes"
return
fi
local parent=$(git_find_parent_branch)
if [ -z $parent ]; then
false
return
fi
local remote=$(git config branch.$parent.remote)
git_cmd fetch $remote $parent:$parent || return
local commits=$(git_commits_out_of_date)
if [[ -n "$commits" && "$commits" != 0 ]]; then
git_cmd_err "unable to fast-forward merge, out of date with $parent by $commits commit(s)"
return
fi
git_cmd switch $parent
git_cmd merge --ff-only $branch_to_merge
}
alias gmffthis='git_merge_ff_this'
# gdevup
# git_update_dev_to_main
# Bring dev branch up to date with main if it's not branched off.
function git_update_dev_to_main() {
git_cmd_help $1 && return
local main=$(git_main_branch)
local dev=$(git_develop_branch)
local remote=$(git config branch.$dev.remote)
git_cmd switch $main || return
git_cmd pull --rebase $remote || return
git_cmd switch $dev || return
git_cmd pull --rebase $remote || return
git_cmd merge --ff-only $main || return
git_cmd push $remote
}
alias gdevup='git_update_dev_to_main'
# grbn <n>
# git_rebase_n_commits <n>
# Rebase interactively `n` commits back from HEAD.
function git_rebase_n_commits() {
git_cmd_help $1 && return
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
git_cmd_err "missing number of commits argument"
return
fi
git_cmd rebase -i HEAD~$1
}
alias grbn='git_rebase_n_commits'
# gbcount
# git_branch_num_commits
# Count number of commits on current branch,
# based on search for parent branch.
function git_branch_num_commits() {
git_cmd_help $1 && return
local current=$(git branch --show-current)
local parent=$(git_find_parent_branch 2>/dev/null)
if [ -z $parent ]; then
local commits=$(git rev-list --count HEAD)
else
local commits=$(git rev-list --count HEAD ^$parent)
fi
printf "\e[33m$commits commit(s)\e[0m on \e[32m$current\e[0m\n"
}
alias gbcount='git_branch_num_commits'
# gbrebase
# git_rebase_branch
# Interactively rebase all commits on current branch,
# based on search for parent branch.
function git_rebase_branch() {
git_cmd_help $1 && return
git_cmd_branch_protection || return
local parent=$(git_find_parent_branch)
if [ -z $parent ]; then
false
return
fi
local commits=$(git rev-list --count HEAD ^$parent)
if [ "$commits" -lt "1" ]; then
git_cmd_err "no commits to rebase"
return
fi
if [ "$commits" -gt "40" ] && [ "$1" != "-y" ]; then
git_cmd_confirm "Are you sure you want to rebase $commits commits?" || return
fi
git_cmd rebase -i HEAD~$commits
}
alias gbrebase='git_rebase_branch'
# gbsquash
# git_squash_branch
# Squash branch (automatically) via interactive rebase.
# If commits beginning with 'drop: ' are found, the
# option to automatically drop them is given.
function git_squash_branch() {
git_cmd_help $1 && return
git_cmd_branch_protection || return
# count commits in branch
local parent=$(git_find_parent_branch)
if [ -z $parent ]; then
false
return
fi
local commits=$(git rev-list --count HEAD ^$parent)
if [ "$commits" -lt "2" ]; then
git_cmd_err "no commits to squash"
return
fi
# drop 'drop:' conventional commits first
local commits_to_drop=$(git log -n $commits --pretty=format:%s | grep -c '^drop: ')
if [ "$commits_to_drop" -gt "0" ]; then
if [ "$1" == "-y" || git_cmd_confirm "Drop $commits_to_drop commit(s) in current branch?" ]; then
GIT_SEQUENCE_EDITOR="$SED_PORTABLE -i '/ drop: / s/pick /drop /g'" git rebase -i HEAD~$commits
# recount commits in branch after drop
local commits=$(git rev-list --count HEAD ^$parent)
if [ "$commits" -lt "2" ]; then
git_cmd_err "no commits to squash after drop"
return
fi
fi
fi
if [ "$1" != "-y" ]; then
git_cmd_confirm "Squash $commits commits?" || return
fi
GIT_SEQUENCE_EDITOR="$SED_PORTABLE -i 's/pick/squash/g;0,/^squash /s//pick /'" git rebase -i HEAD~$commits
}
alias gbsquash='git_squash_branch'
# gbdd
# git_drop_drop_commits
# Drop all commits in current branch with commit
# messages beginning with 'drop: '.
function git_drop_drop_commits() {
git_cmd_help $1 && return
git_cmd_branch_protection || return
local parent=$(git_find_parent_branch)
if [ -z $parent ]; then
false
return
fi
local commits=$(git rev-list --count HEAD ^$parent)
if [ "$commits" -lt "1" ]; then
git_cmd_err "no commits to rebase"
return
fi
local commits_to_drop=$(git log -n $commits --pretty=format:%s | grep -c '^drop: ')
if [ "$commits_to_drop" -lt "1" ]; then
git_cmd_err "no commits to drop"
return
fi
if [ "$1" != "-y" ]; then
git_cmd_confirm "Drop $commits_to_drop commit(s) in current branch?" || return
fi
GIT_SEQUENCE_EDITOR="$SED_PORTABLE -i '/ drop: / s/pick /drop /g'" git rebase -i HEAD~$commits
}
alias gbdd='git_drop_drop_commits'
# gbreset
# git_reset_branch
# Soft reset and unstage all commits on current branch.
function git_reset_branch() {
git_cmd_help $1 && return
git_cmd_branch_protection || return
local parent=$(git_find_parent_branch)
if [ -z $parent ]; then
false
return
fi
local commits=$(git rev-list --count HEAD ^$parent)
if [ "$commits" -lt "1" ]; then
git_cmd_err "no commits to reset"
return
fi
if [ "$commits" -gt "30" ] && [ "$1" != "-y" ]; then
git_cmd_confirm "Are you sure you want to reset $commits commits?" || return
fi
# go back