-
Notifications
You must be signed in to change notification settings - Fork 3
/
.githelpers
166 lines (143 loc) · 5.52 KB
/
.githelpers
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
#!/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'
}
# 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 attempts to unstage all files that match it.
reset_wildcard(){
git ls-files --modified | grep -i $1 | xargs -I{} git checkout {}
git ls-files --others --exclude-standard | grep -i $1 | xargs -I{} git checkout {}
git ls-files --deleted | grep -i $1 | xargs -I{} git checkout HEAD {}
}
# Accepts a pattern and switches to the first branch that matches it.
switch_wildcard() {
if [ -z "$1" ]; then
echo "No pattern provided"
else
git branch -a | 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? ";
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
}
TIMESINCE="function timeSince(date) {\
var seconds = Math.floor((new Date() - date) / 1000);\
var interval = Math.floor(seconds / 31536000);\
if (interval > 1) return interval + \" years\";\
interval = Math.floor(seconds / 2592000);\
if (interval > 1) return interval + \" months\";\
interval = Math.floor(seconds / 86400);\
if (interval > 1) return interval + \" days\";\
interval = Math.floor(seconds / 3600);\
if (interval > 1) return interval + \" hours\";\
interval = Math.floor(seconds / 60);\
if (interval > 1) return interval + \" minutes\";\
return Math.floor(seconds) + \" seconds\";\
}";