forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
50_vcs.sh
115 lines (103 loc) · 3.2 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
# 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 "${@:-master}"; } # Checkout master by default
alias gco='gc'
alias gcb='gc -b'
alias gbc='gc -b' # Dyslexia
alias gr='git remote'
alias grv='gr -v'
#alias gra='git remote add'
alias grr='git remote rm'
alias gcl='git clone'
alias gcd='git rev-parse 2>/dev/null && cd "./$(git rev-parse --show-cdup)"'
# 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=()
for f in $(git diff --name-only "$@"); do
[[ -e "$f" ]] && files=("${files[@]}" "$f")
done
local n=${#files[@]}
echo "Opening $n $([[ "$@" ]] || echo "modified ")file$([[ $n != 1 ]] && \
echo s)${@:+ modified in }$@"
q "${files[@]}"
}
# 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}')"
[[ "$remote" ]] || return
local user_repo="$(echo "$remote" | perl -pe 's/.*://;s/\.git$//')"
echo "https://github.com/$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'
# 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