-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch-git.plugin.zsh
98 lines (80 loc) · 2.52 KB
/
switch-git.plugin.zsh
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
# Switch Git Plugin for oh-my-zsh
#
# Easily navigate through repositories and branches on your machine using
# switch_git_repository and switch_git_branch.
#
# Requirements: fzf
alias sgr="switch_git_repository"
alias sgb="switch_git_branch"
alias sgl="switch_git_list"
alias sgu="switch_git_update"
SWITCH_GIT_BASE_PATH=$HOME
function switch_git_update() {
switch_git_repository switch-git pull
}
function switch_git_list_directories() {
if type "$may" > /dev/null; then
find $SWITCH_GIT_BASE_PATH -name .git -exec dirname {} \; -prune 2>&1 | grep -v "Permission denied" | grep -v "Keine Berechtigung"
else
may
fi
}
function switch_git_list() {
switch_git_list_directories | sed 's!.*/!!' | sort | uniq
}
function switch_git_repository() {
origin_directory=$(pwd)
if [ -z "$1" ]
then
switch_git_repository $(switch_git_list | fzf)
else
repo=$(switch_git_list_directories | grep $1 | sort -s | head -1)
if [ -z "$repo" ]
then
echo "Could not find repository related to $(tput setaf 226)$1"
else
cd $repo
# If additional arguments are supplied, run these through git in the destination repository and return
if [[ ! -z "$2" ]]
then
if [ -x "$(command -v tgit.sh)" ]
then
tgit.sh "${@:2}"
else
git "${@:2}"
fi
cd $origin_directory
fi
fi
fi
}
function switch_git_branch() {
local branches branch
branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") &&
if [ -z "$1" ]
then
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
else
branch=$(echo "$branches" | grep $1 | sort -s | head -1)
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
fi
}
## tab auto-completion
function _switch_git_repository {
local line
reponames=$(switch_git_list)
_arguments -C \
"1: :($reponames)" \
"*::arg:->args"
}
function _switch_git_branch() {
local line
reponames=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)")
_arguments -C \
"1: :($reponames)" \
"*::arg:->args"
}
compdef _switch_git_repository switch_git_repository
compdef _switch_git_branch switch_git_branch