forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
50_vcs.sh
186 lines (167 loc) · 5.47 KB
/
50_vcs.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
# Git shortcuts
alias g='git'
function ga() { git add "${@:-.}"; } # Add all files by default
alias gp='git push'
alias gpa='gp --all'
alias gu='git pull'
alias gl='git log'
alias gg='gl --decorate --oneline --graph --date-order --all'
alias gs='git status'
alias gst='gs'
alias gd='git diff'
alias gdc='gd --cached'
alias gm='git commit -m'
alias gma='git commit -am'
alias gb='git branch'
alias gba='git branch -a'
function gc() { git checkout "${@:-main}"; } # Checkout main by default
alias gco='gc'
alias gcb='gc -b'
alias gbc='gc -b' # Dyslexia
alias gr='git remote'
alias grv='gr -v'
alias grr='git remote rm'
alias gcd='git rev-parse 2>/dev/null && cd "./$(git rev-parse --show-cdup)"'
alias gfum='git fetch upstream'
alias gbd='git branch --merged | egrep -v "(^\*|main|dev)" | xargs git branch -D'
# Current branch or SHA if detached.
alias gbs='git branch | perl -ne '"'"'/^\* (?:\(detached from (.*)\)|(.*))/ && print "$1$2"'"'"''
# Run commands in each subdirectory.
alias gu-all='eachdir git pull'
alias gp-all='eachdir git push'
alias gs-all='eachdir git status'
# open all changed files (that still actually exist) in the editor
function ged() {
local files
IFS=$'\n' files=($(git diff --name-status "$@" | grep -v '^D' | cut -f2 | sort | uniq))
echo "Opening files modified $([[ "$2" ]] && echo "between $1 and $2" || echo "since $1")"
gcd
q "${files[@]}"
cd - > /dev/null
}
# git clone directory into specific folders based on hostname/user/repo
function gcl() {
re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$"
if [[ $1 =~ $re ]]; then
protocol=${BASH_REMATCH[1]}
separator=${BASH_REMATCH[2]}
hostname=${BASH_REMATCH[3]}
user=${BASH_REMATCH[4]}
repo=${BASH_REMATCH[5]}
fi
dir="$HOME/Repos/$hostname/$user/"
echo "Creating directories ($dir) to clone repo in"
mkdir -p $dir && cd $dir && git clone $1
}
# add a github remote by github username
function gra() {
if (( "${#@}" != 1 )); then
echo "Usage: gra githubuser"
return 1;
fi
local repo=$(gr show -n origin | perl -ne '/Fetch URL: .*github\.com[:\/].*\/(.*)/ && print $1')
gr add "$1" "git://github.com/$1/$repo"
}
# GitHub URL for current repo.
function gurl() {
local remotename="${@:-origin}"
local remote="$(git remote -v | awk '/^'"$remotename"'.*\(push\)$/ {print $2}')"
re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$"
if [[ $remote =~ $re ]]; then
protocol=${BASH_REMATCH[1]}
separator=${BASH_REMATCH[2]}
hostname=${BASH_REMATCH[3]}
user=${BASH_REMATCH[4]}
repo=${BASH_REMATCH[5]}
fi
[[ "$remote" ]] || return
echo "https://$hostname/$user/$repo"
}
# GitHub URL for current repo, including current branch + path.
alias gurlp='echo $(gurl)/tree/$(gbs)/$(git rev-parse --show-prefix)'
# git log with per-commit cmd-clickable GitHub URLs (iTerm)
function gf() {
git log $* --name-status --color | awk "$(cat <<AWK
/^.*commit [0-9a-f]{40}/ {sha=substr(\$2,1,7)}
/^[MA]\t/ {printf "%s\t$(gurl)/blob/%s/%s\n", \$1, sha, \$2; next}
/.*/ {print \$0}
AWK
)" | less -F
}
# open last commit in GitHub, in the browser.
function gfu() {
local n="${@:-1}"
n=$((n-1))
git web--browse $(git log -n 1 --skip=$n --pretty=oneline | awk "{printf \"$(gurl)/commit/%s\", substr(\$1,1,7)}")
}
# open current branch + path in GitHub, in the browser.
alias gpu='git web--browse $(gurlp)'
# Just the last few commits, please!
for n in {1..5}; do alias gf$n="gf -n $n"; done
function gj() { git-jump "${@:-next}"; }
alias gj-='gj prev'
# Combine diff --name-status and --stat
function gstat() {
local file mode modes color lines range code line_regex
local file_len graph_len e r c
range="${1:-HEAD~}"
echo "Diff name-status & stat for range: $range"
IFS=$'\n'
lines=($(git diff --name-status "$range"))
code=$?; [[ $code != 0 ]] && return $code
declare -A modes
for line in "${lines[@]}"; do
file="$(echo $line | cut -f2)"
mode=$(echo $line | cut -f1)
modes["$file"]=$mode
done
file_len=0
lines=($(git diff -M --stat --stat-width=999 "$range"))
line_regex='s/\s*([^|]+?)\s*\|.*/$1/'
for line in "${lines[@]}"; do
file="$(echo "$line" | perl -pe "$line_regex")"
(( ${#file} > $file_len )) && file_len=${#file}
done
graph_len=$(($COLUMNS-$file_len-10))
(( $graph_len <= 0 )) && graph_len=1
lines=($(git diff -M --stat --stat-width=999 --stat-name-width=$file_len \
--stat-graph-width=$graph_len --color "$range"))
e=$(echo -e "\033")
r="$e[0m"
declare -A c=([M]="1;33" [D]="1;31" [A]="1;32" [R]="1;34")
for line in "${lines[@]}"; do
file="$(echo "$line" | perl -pe "$line_regex")"
if [[ "$file" =~ \{.+\=\>.+\} ]]; then
mode=R
line="$(echo "$line" | perl -pe "s/(^|=>|\})/$r$e[${c[R]}m\$1$r$e[${c[A]}m/g")"
line="$(echo "$line" | perl -pe "s/(\{)/$r$e[${c[R]}m\$1$r$e[${c[D]}m/")"
else
mode=${modes["$file"]}
color=0; [[ "$mode" ]] && color=${c[$mode]}
line="$e[${color}m$line"
fi
echo "$line" | sed "s/\|/$e[0m$mode \|/"
done
unset IFS
}
# OSX-specific Git shortcuts
if is_osx; then
alias gdk='git ksdiff'
alias gdkc='gdk --cached'
function gt() {
local path repo
{
pushd "${1:-$PWD}"
path="$PWD"
repo="$(git rev-parse --show-toplevel)"
popd
} >/dev/null 2>&1
if [[ -e "$repo" ]]; then
echo "Opening git repo $repo."
gittower "$repo"
else
echo "Error: $path is not a git repo."
fi
}
fi
function gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;}