-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-kw.plugin.zsh
1319 lines (1124 loc) · 39.5 KB
/
git-kw.plugin.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
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
# Git aliases
#
# $MyId$
# $Source$
# $Date$
#
alias -- gllf="is_git && git diff-tree --name-only --no-commit-id -r -a HEAD" # List last files committed
alias -- gs="is_git && git status"
alias -- gdo="is_git && git difftool" # uses opendiff
alias -- gdoy="is_git && git difftool -y" # uses opendiff, no prompting
alias -- glog="is_git && git glog"
alias -- glogg="is_git && git glog -G" # grep log entries for string
alias -- glogp="is_git && git glogp" # show patches
alias -- glogw="is_git && git glog --since '1 week'" # show past week commits
# works great when there are commits to be pushed
# else it shows the entire log
# alias -- gpl="is_git && git glog HEAD...ORIG_HEAD" # to-be-pushed log
alias -- glm="is_git && git log HEAD..FETCH_HEAD" # fetched log
alias -- grv="is_bare && git remote -v" # show remotes with url
alias -- gdu="is_git && git diff --stat --cached ORIG_HEAD" # needs to be in a function
alias -- gi="is_git && git fetch --dry-run -v"
# These aliases match those in OMZ/plugins/git - START
alias -- ga="is_git && git add"
alias -- gau="is_git && git add --update"
# from OMZ git.plugin.zsh
alias -- gtl='gtl(){ git tag --sort=-v:refname -n --list "${1}*" }; noglob gtl'
# 2023-05-25: remove --all from log output
# without --all, log output starts with current HEAD
# NOTE: --follow ONLY works for a single file, will generate an error on an entire repo
alias -- glg="is_git && git log --graph --abbrev-commit --decorate \
--format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold yellow)%d%C(reset)'"
# Show only tags or branches
alias -- glgb="is_git && git log --graph --simplify-by-decoration \
--pretty='format:%C(green)%as %C(auto)%d - %s'"
alias -- glgba="is_git && git log --graph --simplify-by-decoration \
--pretty='format:%C(cyan)%h %C(green)%as %C(yellow)%al%C(auto)%d - %s'"
alias -- glgf="is_git && git log --graph --abbrev-commit --decorate \
--name-status \
--format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold yellow)%d%C(reset)%n'"
function gltag() {
git log --decorate --oneline --pretty=format:"%h %d %s" $@ | \
awk '{ if ($2 ~ /\(tag:.*/) {gsub(/[()]/, "", $2); printf "(%-7s ", $3 } else { printf ("%-10s","") } print }' | \
sed 's/ tag:.*)/ /' | expand -t8 | $PAGER
}
# to use to get commit date for applying to tags
alias -- glgdt="git log --all --abbrev-commit --decorate \
--format=format:'%C(auto)%h|%C(bold blue)%ai%C(reset)|%s|%C(auto)%d'"
function gl2tag() { # show short log back to last tag
# if defined as an alias, $(git describe) gets defined when sourcing this file
glgdt $(git describe --tags --abbrev=0)..HEAD
}
function commit_date() {
[[ $1 == "" ]] && printf "Need Hash number\n" && return
glgdt | grep "$1" | awk -F'|' '{printf "%s\n", $2; }'
}
function git_tag() {
[[ $1 == "" ]] && printf "Need Hash number\n" && return
[[ $2 == "" ]] && printf "Need Version number\n" && return
[[ $3 == "" ]] && printf "Need Commit message\n" && return
glog $1 -1
rsp=$(prompt -e "is this correct ?" "yY" "nNq")
[[ "Yy" == *$rsp* ]] && \
GIT_COMMITTER_DATE="$( commit_date $1 )" git tag -a $2 $1 -m "$3" # add -f to force message change
}
alias -- gb="is_git && git branch"
alias -- gba="is_git && git branch --all"
# alias -- gd="is_git && git diff --ignore-space-change"
alias -- gdca="is_git && git diff --cached"
alias -- gds="is_git && git diff --staged"
alias -- gdw="is_git && git diff --word-diff"
alias -- gdf="is_git && git diff HEAD..FETCH_HEAD" # diff after gf
alias -- gdup="is_git && git diff @{upstream}"
alias -- gss="is_git && git status --short --branch"
# These aliases match those in OMZ/plugins/git - END
# Git Update Keywords
# guk : update keywords inlast committed files
# guk FILE: update keywords in FILE
function guk() {
if [[ $# -gt 0 ]]; then # use cmdline args
fl=($@)
else # use last modified files
fl=($( git diff-tree --name-only --no-commit-id -r -a HEAD )) # convert output to array
fi
printf "Update: %s\n" $fl
local rsp="X"
for file in $fl
do
if [[ $rsp != "Y" ]]; then
rsp=$(prompt -e "Delete ? $file" "yY" "nN" "aq")
fi
case "$rsp" in
n) ;; # skip
N|a|q) break ;; # skip and exit
y|Y) printf "Resetting %s\n" $file;
rm $file; # do this file
git checkout -f $file ;;
esac
done
}
function gsr() {
setopt localoptions noautopushd nopushdignoredups # disable options here only
for g in */.git
do
d=$( dirname $g )
printf "\n%s\n" "$d"
cd $d
git status -s
cd - > /dev/null
done
}
function gpr() {
setopt localoptions noautopushd nopushdignoredups
for g in */.git
do
d=$( dirname $g )
printf "%s\n" "$d"
cd $d
git pull
cd -
done
}
function gc() {
local root=$( git rev-parse --show-toplevel )
local cmd=$root/.git_cmt_cmd
local rc=0
git commit $@
rc=$?
[[ $rc -eq 0 ]] && guk
echo $root
[[ $rc -eq 0 && -x $cmd ]] && ( printf "%s\n" "$root"; $cmd; return 0 )
return $rc
}
# compdefs not getting auto loaded (will if file is sourced), see .zshrc-complete
compdef _git gc=git-commit
function gp() {
git status > /dev/null # only get result code or show error
[[ $? == 0 ]] || return 0
local root=$(basename $( git rev-parse --show-toplevel ) )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
printf "Root: %s\n" $root
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
local th arg tgr=()
for arg in $@; do
# verify user supplied repos exist in list
[[ ${gr[(ie)$arg]} -le ${#gr} ]] && tgr+=($arg) || {
printf "Invalid: %s\n" $arg
return
}
done
[[ $# -gt 0 ]] && gr=($tgr)
for h in $gr; do
cline 4
local url=$( git remote get-url $h )
local sch=$( echo $url | awk -F'[/:]' '{ print $1 }' )
local hst=$( echo $url | awk -F'[/:]' '{ print $4 }' )
[[ -n $sch ]] && printf "%s Remote: %s using %s on %s\n" "$0" "$h" "$sch" "$hst" \
|| printf "%s Local : %s using %s\n" "$0" "$h" "$url"
local rid=$( git rev-parse $h/$branch )
local err=0
local lcl=true # assume repo is local
if [[ -n $sch ]]; then
lcl=false;
ssh_ping -t 2 $hst
err=$? # 2> /dev/null
[[ $err -ne 0 ]] && printf "Unable to ping %s\n" $hst
else
[[ ! -e $url ]] && { printf "Not mounted: %s\n" $url; err=1 }
fi
if [[ $err -eq 0 ]]; then
if [[ $lid != $rid ]]; then
local cnt=$( git diff --name-only $h/$branch...HEAD | wc -l )
if [[ $cnt > 0 ]]; then
printf "Updating %d files on %s\n" "$cnt" "$h"
ssay "Updating $cnt files on $h!" | hl "^.*$"
# Checking OLD/NEW here shows if there is an actual transfer
# OLD_COMMIT=$( git rev-parse $h/$branch )
git push $mytags $h HEAD # |& hl -n -G "^Enum.*$|^Count.*$|^Delta.*$|^Comp.*$|^Writ.*$" -z -c "^To.*$"
th=$h
# NEW_COMMIT=$( git rev-parse $h/$branch )
[[ $? == 0 ]] && ((rc+=1))
[[ $? == 0 && $lcl == true ]] && { # IF we had something to push
# remove everything after /git/ subdir # AND repo is local
# append log filename # THEN create a log and msg file
local log="${url%%/git/*}/Log-$host".txt
printf "Local URL: %s\n" $url
printf "Local LOG: %s\n" $log
# put (local program) reads all stdin before opening output
grep -v $root $log | put $log # remove previous entries
printf "%s %s\n" "$(date +'%Y-%m-%d %H:%M')" $root >> $log
_gp_update
lbline 2
prism -Lw8 < $log
}
else
ssay "$h is current"
fi
else
ssay "$h matches local"
fi
fi # HST is pingable
done
[[ $rc == 1 ]] && ssay "Pushed files to $th"
[[ $rc > 1 ]] && ssay "Pushed files to $rc of $#gr hosts"
return 0 # $rc # 2022-12-02 stop zsh from announcing error code
}
compdef _gf gp
function _gp_update () {
# local url="." # Test file
local url=$( git remote get-url GitRepo )
local msg="${url%%/git/*}/Msg-$host".txt
local hsh="$(git rev-parse HEAD)"
printf "MSG: %s\n" $msg
# printf "HSH: >%s<\n" $hsh
# Testing lines #
# printf "=======\n"
# [[ -e $msg ]] && tr '\n' 'X' < $msg | hl X
# printf "=======\n"
# remove old entry - this is to keep multiple runs from stacking
[[ -e $msg ]] && tr '\n' '\a' < $msg | sed "s|<${hsh}>.*</${hsh}>||g" | tr '\a' '\n' | put $msg
# add new entry
printf "<%s>\n\n" $hsh >> $msg
printf "REPO: %s\n" $url >> $msg
printf "PWD : %s\n\n" $(pwd) >> $msg
git log -1 >> $msg
printf "\n</%s>\n" $hsh >> $msg
}
function gp_del_hash() { # remove hash record from file
local file=$1
[[ ! -e $file ]] && printf "%s does not exist, exiting\n" $file && return -1
shift # pop file off list
foreach hsh in $@; do
printf "HASH: %s\n" $hsh
# tr '\n' '\a' < $file | sed -n "s|<${hsh}>.*</${hsh}>|FOO|p" | tr '\a' '\n'
tr '\n' '\a' < $file | sed -n "s|<${hsh}>.*</${hsh}>||p" | tr '\a' '\n' | put $file
done
}
function gp_show_msg() { # show repo logs from Msg-*.txt
local all help
zparseopts -D -E -K a=all h=help
local url=$( git remote get-url GitRepo )
local vol="${url%%/git/*}"
local lmsg="$vol/Msg-$host".txt
# printf "VOL: %s\n" "$vol"
# printf "URL: %s\n" "$url"
# printf "MSG: %s\n" "$lmsg"
# printf "\n"
foreach msg in $vol/Msg-*.txt; do
# printf "MSG: %s\n" "$msg"
# [[ $msg != $lmsg ]] && printf "REM: %s\n" "$msg" || printf "LOC: %s\n" "$msg"
[[ $all || $msg != $lmsg ]] && { # only show remote Msg files
# split records into lines then search for matching url
# sed needs 2 patterns to match when only one entry exists
# | sed -n 's|><|>\n<|gp;s|<|\n<\n|p' \
# | sed -n 's|><|>\n<|gp;s|\(<.*$\)|\n\1\n|p;' \
# | sed -n 's|\(<.*$\)|\n\1\n|p;s|><|>\n<|gp;' \
local cnt=$(grep '</' $msg | wc -l)
[[ $cnt == 1 ]] && {
# No need to rewrite the record boundaries, there be only one
# | sed -n 's|<|\n<|p' \
tr '\n' '\a' < $msg \
| command grep $url \
| tr '\a' '\n' | command grep -v -e "REPO:|PWD :|Author" | hl "REPO:.*$|PWD.*$" -n -G "commit.*$|Auth.*$|^<.*>$" -z -c "Date.*$" -y "^.*$"
} || {
tr '\n' '\a' < $msg \
| sed -n 's|><|>\n<|gp' \
| command grep $url \
| tr '\a' '\n' | command grep -v -e "REPO:|PWD :|Author" | hl "REPO:.*$|PWD.*$" -n -G "commit.*$|Auth.*$|^<.*>$" -z -c "Date.*$" -y "^.*$"
}
}
done
return 0
}
function gp_del_msg() { # show repo logs from Msg-*.txt
local url=$( git remote get-url GitRepo )
local vol="${url%%/git/*}"
local lmsg="$vol/Msg-$host".txt
# printf "VOL: %s\n" "$vol"
# printf "URL: %s\n" "$url"
# printf "MSG: %s\n" "$lmsg"
# printf "\n"
foreach msg in $vol/Msg-*.txt; do
# printf "MSG: %s\n" "$msg"
# [[ $msg != $lmsg ]] && printf "REM: %s\n" "$msg" || printf "LOC: %s\n" "$msg"
[[ $msg != $lmsg ]] && { # only show remote Msg files
# split records into lines then search for matching url
# sed needs 2 patterns to match when only one entry exists
# | sed -n 's|><|>\n<|gp;s|<|\n<\n|p' \
# | sed -n 's|><|>\n<|gp;s|\(<.*$\)|\n\1\n|p;' \
# | sed -n 's|\(<.*$\)|\n\1\n|p;s|><|>\n<|gp;' \
# | sed -n 's|<|\n<|p' \
local cnt=$(grep '</' $msg | wc -l)
[[ $cnt == 1 ]] && {
tr '\n' '\a' < $msg \
| grep -v $url \
| tr '\a' '\n' | put $msg
} || {
tr '\n' '\a' < $msg \
| sed -n 's|><|>\n<|gp' \
| grep -v $url \
| tr '\a' '\n' | put $msg
}
}
done
return 0
}
function gflog() {
local gr=($( git remote show ));
local root=$(basename $( git rev-parse --show-toplevel ) )
local url=$( git remote get-url GitRepo )
local vol="${url%%/git/*}"
local llog="$vol/Log-$host".txt
for h in $gr; do
[[ -e /Volumes/$h ]] && {
local -a logs=($( echo /Volumes/$h/Log-*.txt ))
local log
for log in $logs; do
printf "Log: %s\n" $log
grep $root $log > /dev/null # just to get status
[[ $? == 0 ]] && {
grep $root $log | prism -Lw8
[[ $llog != $log ]] && {
rsp=$(prompt -e "Remove entry for $root" "yY" "nN" "qa")
case "$rsp" in
n) ;; # skip
N|a|q) break ;; # skip and exit
y|Y) printf "Removing %s\n" $root;
grep -v $root $log | put $log # remove previous entries
gp_del_msg # remove commit messages
esac
} || gp_show_msg -a
}
lbline 2
done
}
done
# gplog -l
return 0
}
function gplog() {
local lcl help
zparseopts -D -E -K l=lcl h=help
local gr=($( git remote show ));
for h in $gr; do
[[ -e /Volumes/$h ]] && {
local -a logs=($( echo /Volumes/$h/Log-*.txt ))
local log
for log in $logs; do
printf "Log: %s\n" $log
prism -Lw8 < $log
lbline 2
done
[[ $lcl ]] && gp_show_msg || gp_show_msg -a
}
done
[[ $#gr -gt 0 ]] && return 0 || return 1
}
function gpt() {
mytags=("--tags" "--follow-tags")
gp $@
unset mytags
}
compdef _gf gpt
# Git Show Commit to be pushed
# 2023-04-12 same as gcr() with different log command
function gpl() {
local arg silent=0
for arg in $@; do
case $arg in
-s|--silent) (( silent+=1 ));;
-ss) (( silent+=2 ));;
-h|--help )
printf "-s|--silent : (1) silence 'in sync' message\n"
printf "-s|--silent : (2) silence 'not in sync' message\n"
return;;
*) printf "Unexpected: %s\n" "$arg"
return;;
esac
done
# show commits to be pushed without connecting to each repo
cline 2
# 2024-02-09 : origin/HEAD is correct!!
# If the following gives an error, you need to:
# git remote set-head origin -a
git glog HEAD...origin/HEAD # ORIG_HEAD # to-be-pushed log
cline 2
# connect to each repo and show repo specific log
local root=$(basename $( git rev-parse --show-toplevel ) )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
echo $root
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
local md5
local ts
for h in $gr; do
cline 4
local url=$( git remote get-url $h )
local sch=$( echo $url | awk -F'[/:]' '{ print $1 }' )
local hst=$( echo $url | awk -F'[/:]' '{ print $4 }' )
[[ -n $sch ]] && printf "%s Remote: %s using %s on %s\n" "$0" "$h" "$sch" "$hst" \
|| printf "%s Local : %s using %s\n" "$0" "$h" "$url"
local cnt=$( git rev-list --count $branch...$h/$branch )
[[ $cnt -gt 0 ]] && printf "Cnt: %2d (%s...%s/%s)\n" "$cnt" "$branch" "$h" "$branch"
local err=0
# Do not need to verify host repo is reachable
# if [[ -n $sch ]]; then
# ssh_ping -t 2 $hst # 2> /dev/null
# err=$?
# [[ $err -ne 0 ]] && printf "Unable to ping %s\n" $hst
# else
# [[ ! -e $url ]] && { printf "Not mounted: %s\n" $url; err=1 }
# fi
if [[ $err -eq 0 ]]; then
# local rid=($( git ls-remote $h HEAD ))
# rid=$rid[1]
# if [[ $lid != $rid ]]; then
# printf "<%s> %s\n<%s> %s\n" "$lid" "$root" "$rid" "$h"
# printf "\n"
# git diff --name-only HEAD..$h/$branch
# printf "\n"
ts=$( git log --decorate=short --color HEAD...$h/$branch )
if [[ $md5 != $( echo $ts | md5 ) ]]; then
md5=$( echo $ts | md5 )
echo $md5
echo $ts
fi
# git log HEAD...FETCH_HEAD
printf "\n"
[[ $silent < 2 ]] && ssay "$root not in sync with $h"
((rc+=1))
# else
# [[ $silent < 1 ]] && ssay "$h matches $root"
# fi
fi # HST is pingable
done
return $rc
}
function gpb() { # push bare repo
local rs;
rs=$( git rev-parse --is-bare-repository )
[[ $rs == "true" ]] || return 0
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
local th
[[ $# == 1 ]] && {
[[ ${gr[(ie)$1]} -le ${#gr} ]] && gr=($1) || {
printf "Invalid: %s\n" $1
return
}
}
for h in $gr; do
cline 4
local url=$( git remote get-url $h )
local sch=$( echo $url | awk -F'[/:]' '{ print $1 }' )
local hst=$( echo $url | awk -F'[/:]' '{ print $4 }' )
[[ -n $sch ]] && printf "%s Remote: %s using %s on %s\n" "$0" "$h" "$sch" "$hst" \
|| printf "%s Local : %s using %s\n" "$0" "$h" "$url"
local rid=$( git rev-parse $h/$branch )
local err=0
if [[ -n $sch ]]; then
ssh_ping -t 2 $hst
err=$? # 2> /dev/null
[[ $err -ne 0 ]] && printf "Unable to ping %s\n" $hst
else
[[ ! -e $url ]] && { printf "Not mounted: %s\n" $url; err=1 }
fi
if [[ $err -eq 0 ]]; then
if [[ $lid != $rid ]]; then
local cnt=$( git diff --name-only $h/$branch...HEAD | wc -l )
if [[ $cnt > 0 ]]; then
printf "Updating %d files on %s\n" "$cnt" "$h"
ssay "Updating $cnt files on $h!"
# Checking OLD/NEW here shows if there is an actual transfer
# OLD_COMMIT=$( git rev-parse $h/$branch )
git push $mytags $h HEAD
th=$h
# NEW_COMMIT=$( git rev-parse $h/$branch )
[[ $? == 0 ]] && ((rc+=1))
else
ssay "$h is current"
fi
else
ssay "$h matches local"
fi
fi # HST is pingable
done
[[ $rc == 1 ]] && ssay "Pushed files to $th"
[[ $rc > 1 ]] && ssay "Pushed files to $rc of $#gr hosts"
return 0 # $rc # 2022-12-02 stop zsh from announcing error code
}
# Git Check Remote - are remotes and local in-sync ?
# 2023-04-12 does this do anything different for new fetch commits?
function gcr() {
local arg silent=0
for arg in $@; do
case $arg in
-s|--silent) (( silent+=1 ));;
-ss) (( silent+=2 ));;
-h|--help )
printf "-s|--silent : (1) silence 'in sync' message\n"
printf "-s|--silent : (2) silence 'not in sync' message\n"
return;;
*) printf "Unexpected: %s\n" "$arg"
return;;
esac
done
local root=$(basename $( git rev-parse --show-toplevel ) )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
echo $root
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
for h in $gr; do
cline 4
local sch=$( git remote get-url $h | awk -F'[/:]' '{ print $1 }' )
local hst=$( git remote get-url $h | awk -F'[/:]' '{ print $4 }' )
printf "Remote: %s using %s on %s\n" "$h" "$sch" "$hst"
[[ $sch > "" ]] && ssh_ping -t 2 $hst # 2> /dev/null
if [[ $sch > "" && $? != 0 ]]; then
printf "Unable to ping %s\n" $hst
else
local rid=($( git ls-remote $h HEAD ))
rid=$rid[1]
if [[ $lid != $rid ]]; then
printf "<%s> %s\n<%s> %s\n" "$lid" "$root" "$rid" "$h"
printf "\n"
git diff --name-only HEAD..$h/$branch
printf "\n"
# git log HEAD...$h/$branch
git log HEAD...FETCH_HEAD
printf "\n"
[[ $silent < 2 ]] && ssay "$root not in sync with $h"
((rc+=1))
else
[[ $silent < 1 ]] && ssay "$h matches $root"
fi
fi # HST is pingable
done
return $rc
}
function gcrr() {
setopt localoptions noautopushd nopushdignoredups
local dir
foreach dir in **/.git; do
cd $( dirname $dir )
gcr -s
cd -
done
}
# Git Fetch and report # of files changed from all repos
function gfa() {
git status > /dev/null # only get result code or show error
[[ $? == 0 ]] || return 0
local root=$( git rev-parse --show-toplevel )
local rdir=$(basename $root )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
printf "Root: %s\n" "$root"
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
local arg silent=0
for arg in $@; do
case $arg in
-s|--silent) (( silent+=1 ));;
-ss) (( silent+=2 ));;
-h|--help )
printf "-s|--silent : (1) silence 'in sync' message\n"
printf "-s|--silent : (2) silence 'not in sync' message\n"
return;;
*) printf "Unexpected: %s\n" "$arg"
return;;
esac
done
for h in $gr; do
cline 4
local sch=$( git remote get-url $h | awk -F'[/:]' '{ print $1 }' )
local hst=$( git remote get-url $h | awk -F'[/:]' '{ print $4 }' )
printf "Remote: %s using %s on %s\n" "$h" "$sch" "$hst"
pth=$( git remote get-url $h )
if [[ $pth == ssh* || -e $pth ]]; then
[[ $sch > "" ]] && ssh_ping -t 2 $hst # 2> /dev/null
if [[ $sch > "" && $? != 0 ]]; then
printf "Unable to ping %s\n" $hst
else
local rid=($( git ls-remote $h $branch )) # HEAD ))
rid=$rid[1]
if [[ $lid != $rid ]]; then
OLD_COMMIT=$( git rev-parse $h/$branch )
git fetch $h $branch # HEAD
NEW_COMMIT=$( git rev-parse FETCH_HEAD )
printf "<%s> %s\n<%s> %s\n" "$lid" "$root" "$rid" "$h"
git diff --name-only $OLD_COMMIT..$NEW_COMMIT # show filenames
printf "\n"
# git log HEAD...FETCH_HEAD
# determine which log approach is better
printf "git log %s/%s...FETCH_HEAD\n" "$h" "$branch"
git log $h/$branch...FETCH_HEAD
printf "\ngit lg HEAD...FETCH_HEAD\n"
git lg HEAD..FETCH_HEAD
printf "\n"
local cnt=$( git diff --name-only $OLD_COMMIT...$NEW_COMMIT | wc -l )
printf "%d files from %s\n" "$cnt" "$h"
ssay "Got $cnt files from $h for $rdir!"
# Checking OLD/NEW here shows if there is an actual transfer
((rc+=1))
else
printf "%s matches %s\n" "$h" "$branch"
[[ $silent < 1 ]] && ssay "$h matches local"
fi
fi # HST is pingable
else
printf "%s not available\n" $h
fi
done
[[ $rc == 1 ]] && ssay "Fetched files from $h"
[[ $rc > 1 ]] && ssay "Fetched files from $rc hosts"
return 0 # $rc # 2022-12-02 stop zsh from announcing error code
}
# Only fetch from origin
function gf() {
git status > /dev/null # only get result code or show error
[[ $? == 0 ]] || return 0
local root=$( git rev-parse --show-toplevel )
local rdir=$(basename $root )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
printf "Root: %s\n" "$root"
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
local repo="origin"
printf "%d repos\n" $#gr
# ${gr[(ie)$repo]} <-- this returns position of repo in gr, or size+1
if [[ ${gr[(ie)$repo]} -gt ${#gr} ]]; then
printf "origin repo not specified\n"
return
fi
local arg silent=0
for arg in $@; do
case $arg in
-s|--silent) (( silent+=1 ));;
-ss) (( silent+=2 ));;
-h|--help )
printf "-s|--silent : (1) silence 'in sync' message\n"
printf "-s|--silent : (2) silence 'not in sync' message\n"
printf "Repos: %s\n" "$gr"
return;;
*) [[ ${gr[(ie)$arg]} -le ${#gr} ]] && repo=$arg || {
printf "Unexpected: %s\n" "$arg"
return
} ;;
esac
done
cline 4
local sch=$( git remote get-url $repo | awk -F'[/:]' '{ print $1 }' )
local hst=$( git remote get-url $repo | awk -F'[/:]' '{ print $4 }' )
printf "Remote: %s using %s on %s\n" "$repo" "$sch" "$hst"
pth=$( git remote get-url $repo )
if [[ $pth == ssh* || -e $pth ]]; then
[[ $sch > "" ]] && ssh_ping -t 2 $hst # 2> /dev/null
if [[ $sch > "" && $? != 0 ]]; then
printf "Unable to ping %s\n" $hst
else
local rid=($( git ls-remote $repo $branch )) # HEAD ))
rid=$rid[1]
if [[ $lid != $rid ]]; then
OLD_COMMIT=$( git rev-parse $repo/$branch )
git fetch $mytags $repo $branch # HEAD
NEW_COMMIT=$( git rev-parse FETCH_HEAD )
printf "<%s> %s\n<%s> %s\n" "$lid" "$root" "$rid" "$repo"
git diff --name-only $OLD_COMMIT..$NEW_COMMIT # show filenames
printf "\n"
# git log HEAD...FETCH_HEAD
# determine which log approach is better
printf "git log %s/%s...FETCH_HEAD\n" "$repo" "$branch"
git log $repo/$branch...FETCH_HEAD
printf "\ngit lg HEAD...FETCH_HEAD\n"
git lg HEAD..FETCH_HEAD
printf "\n"
local cnt=$( git diff --name-only $OLD_COMMIT...$NEW_COMMIT | wc -l )
printf "%d files from %s\n" "$cnt" "$repo"
ssay "Got $cnt files from $repo for $rdir!"
# Checking OLD/NEW here shows if there is an actual transfer
((rc+=1))
else
printf "%s matches %s\n" "$repo" "$branch"
[[ $silent < 1 ]] && ssay "$repo matches local"
fi
fi # HST is pingable
else
printf "%s not available\n" $repo
fi
[[ $rc == 1 ]] && ssay "Fetched files from $repo"
[[ $rc > 1 ]] && ssay "Fetched files from $rc hosts"
return 0 # $rc # 2022-12-02 stop zsh from announcing error code
}
function gft() {
mytags="--tags"
gf $@
unset mytags
}
function gfr() { # check subdirs
setopt localoptions noautopushd nopushdignoredups
for repo in */.git; do
local rdir=$(dirname $repo )
echo $rdir
cd $rdir
gf -s # git fetch, silence no changes
cd -
yline
done
}
function gdr() { # check subdirs
setopt localoptions noautopushd nopushdignoredups
for repo in */.git; do
local rdir=$(dirname $repo )
echo $rdir
cd $rdir
git diff --ignore-space-change
cd -
yline
done
}
compdef _git gdr=git-diff
function gdcs() { # diff commits sequentially }
local ahash=($( git log --abbrev-commit --oneline $@ | awk '{print $1}' ))
local phash=$ahash[1];
local hash rsp prmpt=1
printf "%3d commits\n" ${#ahash[@]}
for hash in ${ahash[@]:1}; do # walk thru array starting on second element
# [[ $prmpt ]] &&
dt=$( git show -s --date=format:'%Y-%m-%d %H:%M' --format=%cd $phash )
su=$( git log --pretty=format:"%s" $phash -n 1 ) # get commit subject
fn=($( git show --name-only --pretty=format: $phash )) # get commit filenames
# fn=$( git show --name-only --pretty=format:"%s" $phash ) # get commit filenames+subj
printf "File: %s\n" $fn[@]
rsp=$(prompt -e "log $dt: $su ?" "yY" "nN" "qa")
[[ "qa" == *$rsp* && $prmpt ]] && printf "ABORT!!!\n" && return 0
[[ "Yy" == *$rsp* || ! $prmpt ]] && {
# printf ">>>\e[1m\e[38;5;6m %s \e[0m<<<\n\n" "$phash -> $hash";
lbline 1
git glog $phash -n 1
lbline 1
}
for file in ${fn[@]}; do
rsp=$(prompt -e "diff $file: $hash $phash?" "yY" "nN" "qa")
[[ "qa" == *$rsp* && $prmpt ]] && printf "ABORT!!!\n" && return 0
[[ "Yy" == *$rsp* || ! $prmpt ]] && {
# reversing order colorizes red-delete green-add properly
lbline 1
git diff $hash $phash $file | dwdiff -u
}
done
yline 2
phash=$hash
done
}
function gfb() { # fetch into bare repo
local rs;
rs=$( git rev-parse --is-bare-repository )
[[ $rs == "true" ]] || return 0
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local rc=0
local gr=($( git remote show ));
local lid=$( git rev-parse HEAD );
printf "repos: %s\n" "$gr"
repo=$gr[1];
local arg silent=0
for arg in $@; do
case $arg in
-s|--silent) (( silent+=1 ));;
-ss) (( silent+=2 ));;
-h|--help )
printf "-s|--silent : (1) silence 'in sync' message\n"
printf "-s|--silent : (2) silence 'not in sync' message\n"
printf "Repos: %s\n" "$gr"
return;;
*) [[ ${gr[(ie)$arg]} -le ${#gr} ]] && repo=$arg || {
printf "Unexpected: %s\n" "$arg"
return
} ;;
esac
done
cline 4
local pth=$( git remote get-url $repo )
local sch=$( echo $pth | awk -F'[/:]' '{ print $1 }' )
local hst=$( echo $pth | awk -F'[/:]' '{ print $4 }' )
printf "Remote: %s using %s on %s\n" "$repo" "$sch" "$hst"
if [[ $pth == ssh* || -e $pth ]]; then
[[ $sch > "" ]] && ssh_ping -t 2 $hst # 2> /dev/null
if [[ $sch > "" && $? != 0 ]]; then
printf "Unable to ping %s\n" $hst
else
local rid=($( git ls-remote $repo $branch )) # HEAD ))
rid=$rid[1]
if [[ $lid != $rid ]]; then
OLD_COMMIT=$( git rev-parse $repo/$branch )
git fetch $mytags $repo $branch # HEAD
NEW_COMMIT=$( git rev-parse FETCH_HEAD )
printf "<%s>\n<%s> %s\n" "$lid" "$rid" "$repo"
git diff --name-only $OLD_COMMIT..$NEW_COMMIT # show filenames
printf "\n"
# git log HEAD...FETCH_HEAD
# determine which log approach is better
printf "git log %s/%s...FETCH_HEAD\n" "$repo" "$branch"
git log $repo/$branch...FETCH_HEAD
printf "\ngit lg HEAD...FETCH_HEAD\n"
git lg HEAD..FETCH_HEAD
printf "\n"
local cnt=$( git diff --name-only $OLD_COMMIT...$NEW_COMMIT | wc -l )
printf "%d files from %s\n" "$cnt" "$repo"
ssay "Got $cnt files from $repo for $rdir!"
# Checking OLD/NEW here shows if there is an actual transfer
((rc+=1))
else
printf "%s matches %s\n" "$repo" "$branch"
[[ $silent < 1 ]] && ssay "$repo matches local"
fi
fi # HST is pingable
else
printf "%s not available\n" $repo
fi
[[ $rc == 1 ]] && ssay "Fetched files from $repo"
[[ $rc > 1 ]] && ssay "Fetched files from $rc hosts"
return 0 # $rc # 2022-12-02 stop zsh from announcing error code
}
function gir() { # check incoming dry-run
setopt localoptions noautopushd nopushdignoredups
for repo in */.git; do
local rdir=$(dirname $repo )
echo $rdir
cd $rdir
git fetch --dry-run -v
[[ $? != 0 ]] && ssay "Check $rdir"
cd -
yline
done
}
function gm() { # git merge
git status > /dev/null # only get result code or show error
[[ $? == 0 ]] || return 0
local root=$( git rev-parse --show-toplevel )
local branch=$( git_current_branch ) # defined in OMZ/lib/git.zsh
local cmd=$root/.git_upd_cmd
# local gr=($( git remote show ));
local rc=0
echo $root
# OLD_COMMIT=$( git rev-parse HEAD) # $h/$branch )
OLD_COMMIT=$( git rev-parse $branch)
NEW_COMMIT=$( git rev-parse FETCH_HEAD )
git diff --name-only $OLD_COMMIT..$NEW_COMMIT # show filenames
# -ff is disabled for 'git merge' in .gitconfig
# to avoid collapsing branches on merge.
# re-enable for a regular merge
if ( git merge --ff --log FETCH_HEAD ); then # GIT -n HEAD
ssay "merged from FETCH_HEAD"
else
((rc+=$?))
ssay "that was unexpected - $rc" # to determine when this happens
fi
[[ $rc -eq 0 && -x $cmd ]] && ( printf "%s\n" "$root"; $cmd; return 0 )
# 2023-04-20: what? rc is an error code, not host count
# [[ $rc == 1 ]] && ssay "Merged files"