-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path.githelpers
More file actions
265 lines (231 loc) · 8.92 KB
/
.githelpers
File metadata and controls
265 lines (231 loc) · 8.92 KB
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
#!/bin/bash
# Based on Gary Bernhardt's .githelpers
# https://github.com/garybernhardt/dotfiles/blob/main/.githelpers
# Log output:
#
# * 51c333e (12 days) <Gary Bernhardt> add vim-eunuch
#
# Branch output:
#
# * release/v1.1 (13 days) <Leyan Lo> add pretty_git_branch
#
# The time massaging regexes start with ^[^<]* because that ensures that they
# only operate before the first "<". That "<" will be the beginning of the
# author name, ensuring that we don't destroy anything in the commit message
# that looks like time.
#
# The log format uses } characters between each field, and `column` is later
# used to split on them. A } in the commit subject or any other field will
# break this.
LOG_AUTHOR="%C(always,bold blue)<%aN>%C(always,reset)"
LOG_HASH="%C(always,yellow)%h%C(always,reset)"
LOG_REFS="%C(always,bold red)%d%C(always,reset)"
LOG_RELATIVE_TIME="%C(always,green)(%ar)%C(always,reset)"
LOG_SUBJECT="%s"
LOG_FORMAT="$LOG_HASH^$LOG_RELATIVE_TIME^$LOG_AUTHOR^$LOG_REFS $LOG_SUBJECT"
BRANCH_PREFIX="%(HEAD)"
BRANCH_REF="%(color:red)%(color:bold)%(refname:short)%(color:reset)"
BRANCH_HASH="%(color:yellow)%(objectname:short)%(color:reset)"
BRANCH_DATE="%(color:green)(%(committerdate:relative))%(color:reset)"
BRANCH_AUTHOR="%(color:blue)%(color:bold)<%(authorname)>%(color:reset)"
BRANCH_CONTENTS="%(contents:subject)"
BRANCH_FORMAT="$BRANCH_PREFIX^$BRANCH_REF^$BRANCH_HASH^$BRANCH_DATE^$BRANCH_AUTHOR^$BRANCH_CONTENTS"
# Print the name of the "primary" branch, be it main or master.
remote_origin_primary_branch() {
git branch -a | grep 'remotes.origin.\(main\|master\)' | sed 's|.*remotes/||g'
}
# Get the host of the remote origin
remote_origin_host() {
git remote get-url origin | sed -E -e 's|.*://([^/]+)/.*|\1|' -e 's|.*@([^:]+):.*|\1|'
}
git_browse_web() {
if [ "github.com" == "$(remote_origin_host)" ] && command -v gh > /dev/null; then
gh browse
else
local url=$(git remote get-url origin)
url="${url%.git}"
if [[ "$url" =~ ^git@ ]]; then
url="${url/://}"
url="https://${url#git@}"
fi
echo "Opening $url"
open "$url"
fi
}
git_browse_pull_request() {
if [ "github.com" == "$(remote_origin_host)" ] && command -v gh > /dev/null; then
gh pr view --web || gh browse --branch $(git branch --show-current)
else
git_browse_web
fi
}
# Show a diff of the most recent commit.
show_git_head() {
pretty_git_log -1
git show -p --pretty="tformat:"
}
pretty_git_log() {
git log --graph --pretty="tformat:${LOG_FORMAT}" $* | pretty_git_format | git_page_maybe
}
pretty_git_branch() {
git branch -v --color=always --format=${BRANCH_FORMAT} $* | pretty_git_format
}
pretty_git_branch_sorted() {
git branch -v --format=${BRANCH_FORMAT} --sort=-committerdate $* | head -n${1:-5} | pretty_git_format
}
pretty_git_format() {
# Replace (2 years ago) with (2 years)
sed -Ee 's/(^[^<]*) ago\)/\1)/' |
# Replace (2 years, 5 months) with (2 years)
sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?\)/\1)/' |
# Line columns up based on } delimiter
column -s '^' -t
}
git_page_maybe() {
# Page only if we're asked to.
if [ -n "$GIT_NO_PAGER" ]; then
cat
else
# Page only if needed.
less --quit-if-one-screen --no-init --RAW-CONTROL-CHARS --chop-long-lines
fi
}
# Accepts a pattern and attempts to stage all files that match it.
add_wildcard() {
git ls-files --modified | grep -i $1 | xargs -I{} git add {}
git ls-files --others --exclude-standard | grep -i $1 | xargs -I{} git add {}
git ls-files --deleted | grep -i $1 | xargs -I{} git rm {}
}
# Accepts a pattern and discards all changes to files that match it.
discard_wildcard() {
git ls-files --modified | grep -i $1 | xargs -I{} git restore {}
git ls-files --deleted | grep -i $1 | xargs -I{} git restore {}
git ls-files --others --exclude-standard | grep -i $1 | xargs -I{} rm {}
}
# Accepts a pattern and removes staged files back to unstaged/untracked.
reset_wildcard() {
git diff --cached --name-only | grep -i $1 | xargs -I{} git restore --staged {}
}
# Accepts a pattern and switches to the first branch that matches it.
# Provide the `--all` flag to include remote branches as well.
switch_wildcard() {
local all_branches=false
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-a | --all )
all_branches=true
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
if [ -z "$1" ]; then
echo "No pattern provided"
elif [ "$all_branches" = true ]; then
git branch --all --sort=-committerdate | sed 's|remotes/origin/||' | egrep "$1" | head -1 | xargs git switch
else
git branch --sort=-committerdate | egrep "$1" | head -1 | xargs git switch
fi
}
# Delete local-only branches with confirmation.
delete_remoteless_branches_interactive() {
git fetch origin --prune --tags -f
main_branch=$(remote_origin_primary_branch)
remote_branches=$(git branch --remote | grep "origin" | grep -v "HEAD" | cut -c10- | egrep -v "^${main_branch}$")
local_branches=$(git branch | grep -v "HEAD" | cut -c3- | egrep -v "^${main_branch}$")
for local_branch in $local_branches; do
if ! echo "$remote_branches" | grep -q "$local_branch"; then
read -p "Delete $local_branch? (y/n): " response
if [[ $response =~ ^[Yy] ]]; then
git branch -D "$local_branch"
fi
fi
done
}
# Delete all personally-prefixed non-primary branches with confirmation.
delete_all_my_branches_interactive() {
for branch in $(git branch | egrep '^\W*(aln|anorton)/'); do
printf "Delete $branch, both local and origin? (y/[n])";
read ans;
[[ "$ans" == "y" ]] && git branch -D $branch && git push origin :$branch
done
}
# Delete all non-personally-prefixed, non-primary branches.
delete_all_others_local_branches() {
for branch in $(git branch | grep '/' | egrep -v '^\W*(aln|anorton)/'); do
printf "Delete $branch? ";
read ans;
[[ "$ans" == "y" ]] && git branch -D $branch
done
}
# Accepts a sed substituion (e.g., s/foo/bar/) and applies it to the current branch, also unsetting the upstream branch.
rename_branch_regex() {
if [ -z "$1" ]; then
echo "No pattern provided"
else
local new_name=$(git branch --show-current | sed -e "$1")
git branch --unset-upstream 2> /dev/null
git branch -m "$new_name"
echo "$new_name"
fi
}
# Lists the 10 most recently committed branches, sorted by relative commit time.
# Highlights the current branch with an asterisk (*) and displays relative time in a concise format.
list_branches_by_relative_commit() {
current_branch=$(git branch --show-current)
git for-each-ref refs/heads/ --sort=-committerdate --format='%(refname:short) ~ %(committerdate:relative)' \
| awk -F~ '!seen[$1]++' \
| head -n 10 \
| awk -F' ~ ' '{
split($2, time_parts, " ");
relative_time = time_parts[1];
unit = substr(time_parts[2], 1, 1);
prefix = ($1 == "'$current_branch'") ? "*" : " ";
printf("%s%-2s %-5s %-s\n", prefix, NR, "-"relative_time unit, $1);
}'
}
# Interactively switch to a recent branch using a selection menu with j/k or arrow keys, and Enter to confirm.
switch_recent_branches_by_index() {
local branches=($(list_branches_by_relative_commit | awk '{print $NF}'))
local current_branch=$(git branch --show-current)
# Use fzf for interactive selection
local selected_branch=$(list_branches_by_relative_commit | \
fzf --ansi --height=10 --reverse --header="Select a branch to switch to:" \
--bind="enter:accept" \
--bind="j:down,k:up" \
--color=hl:green --color=fg+:green --color=bg+:black)
if [ -n "$selected_branch" ]; then
local branch_name=$(echo "$selected_branch" | awk '{print $NF}')
git switch "$branch_name"
else
echo "No branch selected."
fi
}
switch_all_branches_by_index() {
local current_branch=$(git branch --show-current)
local selected_branch=$(git for-each-ref --sort=-committerdate --format='%(refname:short) ~ %(committerdate:relative)' refs/heads/ refs/remotes/ \
| awk -F' ~ ' '{print $1 " ~ " $2}' \
| grep -vE '^origin[^/]' \
| awk -F~ '!seen[$1]++' \
| head -n 20 \
| awk -F' ~ ' '{
split($2, time_parts, " ");
relative_time = time_parts[1];
unit = substr(time_parts[2], 1, 1);
prefix = ($1 == "'$current_branch'") ? "*" : " ";
printf("%s%-2s %-5s %-s\n", prefix, NR, "-"relative_time unit, $1);
}' \
| fzf --ansi --height=20 --reverse --header="Select a branch to switch to (local & remote):" \
--bind="enter:accept" \
--bind="j:down,k:up" \
--color=hl:green --color=fg+:green --color=bg+:black)
if [ -n "$selected_branch" ]; then
local branch_name=$(echo "$selected_branch" | awk '{print $NF}' | sed 's/origin\///g')
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
git switch "$branch_name"
elif git show-ref --verify --quiet "refs/remotes/origin/$branch_name"; then
git switch --track "origin/$branch_name"
else
echo "Branch not found: $branch_name"
fi
else
echo "No branch selected."
fi
}