-
Notifications
You must be signed in to change notification settings - Fork 1
/
zsh-git-fzf.plugin.zsh
88 lines (75 loc) · 2.74 KB
/
zsh-git-fzf.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
#!/usr/bin/env zsh
source "$(dirname "$0")/src/operations/worktree"
source "$(dirname "$0")/src/operations/status"
source "$(dirname "$0")/src/operations/branch"
source "$(dirname "$0")/src/operations/checkout"
source "$(dirname "$0")/src/operations/diff"
source "$(dirname "$0")/src/operations/log"
source "$(dirname "$0")/src/operations/stash"
source "$(dirname "$0")/src/operations/reflog"
source "$(dirname "$0")/src/helpers"
_help() {
local PREFIX="git-fzf"
echo -e "alias git-fzf = gfzf = gf\n"
echo "Usage:"
echo -e "\t${PREFIX} worktree: Worktree operations"
echo -e "\t${PREFIX} status: Show paths that have differences between the index file and the current HEAD commit"
echo -e "\t${PREFIX} branch: Show both local and remote branches"
echo -e "\t${PREFIX} checkout: Switch branches"
echo -e "\t${PREFIX} diff: Show changes between commits, commit and working tree, etc"
echo -e "\t${PREFIX} stash: Show stash entities"
echo -e "\t${PREFIX} reflog: Show reflog entities"
echo -e "\t${PREFIX} log: Show commit logs"
echo -e "\t${PREFIX} upgrade: Upgrade zsh-git-fzf plugin"
}
_upgrade_plugin() {
local HOLD_PATH=$PWD
pushd ~/.oh-my-zsh/custom/plugins/zsh-git-fzf > /dev/null
# TODO: is there anything better than this? for checking if the repository needs pull
git fetch &> /dev/null
diffs=$(git diff master origin/master)
if [ -z "$diffs" ]
then
colorful_echo "Already up to date"
pushd $HOLD_PATH > /dev/null
return 0
fi
git pull
colorful_echo "zsh-git-fzf plugin has been upgraded successfully. Restart your shell or reload config file(source ~/.zshrc)."
pushd $HOLD_PATH > /dev/null
}
git-fzf() {
if ! hash fzf 2>/dev/null; then
colorful_echo "You need to install fzf: https://github.com/junegunn/fzf" "RED"
return 1
fi
local OPERATION=$1
[ -z $OPERATION ] && _help && return 0
[ $OPERATION = "upgrade" ] && _upgrade_plugin && return 0
local IS_GIT_REPOSITORY="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [[ ! $IS_GIT_REPOSITORY ]]; then
colorful_echo "You need to be inside a git repository" "RED"
return 1
fi
if [ $OPERATION = "worktree" ]; then
_worktree ${@:2} # pass all arguments except the first one(add)
elif [ $OPERATION = "status" ]; then
_status
elif [ $OPERATION = "branch" ]; then
_branch
elif [ $OPERATION = "checkout" ]; then
_checkout
elif [ $OPERATION = "diff" ]; then
_diff
elif [ $OPERATION = "stash" ]; then
_stash
elif [ $OPERATION = "reflog" ]; then
_reflog
elif [ $OPERATION = "log" ]; then
_log
else
_help
fi
}
alias gfzf="git-fzf"
alias gf="git-fzf"