Debian Codegraph @

tests/inputs/git_helpers.fish

raw
1 # https://raw.githubusercontent.com/Konstruktionist/fish/master/conf.d/git_helpers.fish
2 # Building Gary Bernhardt's githelpers bash script in fish
3 # ========================================================
4 #
5 # Log output:
6 #
7 # * 51c333e (12 days) <Gary Bernhardt> add vim-eunuch
8 #
9 # Branch output:
10 #
11 # * release/v1.1 (13 days) <Leyan Lo> add pretty_git_branch
12 #
13 # The time massaging regexes start with ^[^<]* because that ensures that they
14 # only operate before the first "<". That "<" will be the beginning of the
15 # author name, ensuring that we don't destroy anything in the commit message
16 # that looks like time.
17 #
18 # The log format uses ∬ characters between each field, and `column` is later
19 # used to split on them. A ∬ in the commit subject or any other field will
20 # break this. (GB used originally the } character, I think that may
21 # occasionally show up in a commit message)
22 # -----------------------------------------
23
24 set -l ycolor (set_color bryellow)
25 set -l ncolor (set_color normal)
26 set -l gcolor (set_color brgreen)
27 set -l bcolor (set_color blue)
28 set -l rcolor (set_color brred)
29
30 set -l log_hash "$ycolor%h"
31 set -l log_relative_time "$gcolor(%ar)"
32 set -l log_author "$bcolor<%an>"
33 set -l log_refs "$rcolor%d$ncolor"
34 set -l log_subject "%s"
35
36 # Use a special character to separate our fields so strings with spaces in
37 # them (like names & subjects) will not split on them.
38 # Character used is ∬ = U+222C (DOUBLE INTEGRAL). I'm fairly sure nobody uses
39 # that in commit messages.
40 # DO NOT use it between log_refs and log_subject to prevent hideous whitespace
41 # problems.
42
43 set log_format "$log_hash∬$log_relative_time∬$log_author∬$log_refs $log_subject"
44
45 set -l branch_prefix "%(HEAD)"
46 set -l branch_ref "$rcolor%(refname:short)"
47 set -l branch_hash "$ycolor%(objectname:short)"
48 set -l branch_date "$gcolor(%(committerdate:relative))"
49 set -l branch_author "$bcolor<%(authorname)>$ncolor"
50 set -l branch_contents "%(contents:subject)"
51
52 set branch_format "$branch_prefix∬$branch_ref∬$branch_hash∬$branch_date∬$branch_author∬$branch_contents"
53
54 # Logs
55 function gitl -d 'l = all commits, only current branch'
56 # 1st string replace: remove all ' ago' only in date column
57 # 2nd string replace: replace (2 years, 5 months) with (2 years)
58 # 3rd - 5th string replace: print Merge commit messages in cyan
59 # NOTE: to get the merge commit messages printed cyan we can not use the above
60 # declared variables rcolor & ncolor. If we do we get an error stating:
61 # 'Variables may not be used as commands' or 'string replace: Expected
62 # argument"
63 git log --graph --pretty="tformat:$log_format" $argv |\
64 string replace -r '(^[^<]*)\sago\)' '$1)' |\
65 string replace -r ',\s\d+?\s\w+\s?' '' |\
66 string replace -r 'Merge branch\s.*' (set_color cyan)'$0'(set_color normal) |\
67 string replace -r 'Merge pull request\s.*' (set_color cyan)'$0'(set_color normal) |\
68 string replace -r 'Merge remote-tracking branch\s.*' (set_color cyan)'$0'(set_color normal) |\
69 # TODO: Ideally these last 3 would be replaced by
70 #
71 # string replace -r ([\s]{3}Merge.*) (set_color cyan)'$1'(set_color normal)
72 #
73 # but somehow fish doesn't understand this and ignores it. There are no
74 # error messages, it just ignores the set_color commands & prints the
75 # commit message in normal color. It works in RegExR & online regex
76 # testers.
77 # Needs further investigation.
78 column -t -s '∬' |\
79 less -FXRS
80 end
81
82 function gitla -d 'la = all commits, all reachable refs'
83 gitl --all
84 end
85
86 function gitr -d 'r = recent commits, only current branch'
87 gitl -30
88 end
89
90 function gitra -d 'ra = recent commits, all reachable refs'
91 gitr --all
92 end
93
94 function gith -d 'h = head'
95 gitl -1
96 end
97
98 function githp -d 'hp = head with patch'
99 gitl -1
100 git show -p --pretty="tformat:"
101 end
102
103 # Branches
104 # This follows the same logic as in the Logs section
105 function gitb -d 'b = all branches'
106 git branch -v --format=$branch_format $argv |\
107 string replace -r '(^[^<]*)\sago\)' '$1)' |\
108 string replace -r ',\s\d+?\s\w+\s?' '' |\
109 string replace -r 'Merge branch\s.*' (set_color cyan)'$0'(set_color normal) |\
110 string replace -r 'Merge pull request\s.*' (set_color cyan)'$0'(set_color normal) |\
111 string replace -r 'Merge remote-tracking branch\s.*' (set_color cyan)'$0'(set_color normal) |\
112 column -t -s '∬' | less -FXRS
113 end
114
115 function gitbs -d 'bs = all branches, sorted by last commit date'
116 git branch -v --format=$branch_format --sort=-committerdate $argv |\
117 string replace -r '(^[^<]*)\sago\)' '$1)' |\
118 string replace -r ',\s\d+?\s\w+\s?' '' |\
119 string replace -r 'Merge branch\s.*' (set_color cyan)'$0'(set_color normal) |\
120 string replace -r 'Merge pull request\s.*' (set_color cyan)'$0'(set_color normal) |\
121 string replace -r 'Merge remote-tracking branch\s.*' (set_color cyan)'$0'(set_color normal) |\
122 column -t -s '∬' | less -FXRS
123 end