]> git.phdru.name Git - dotfiles.git/blob - .bash_completion.d/git-funcs
Feat(recode-filenames-recursive): Allow to omit parameters
[dotfiles.git] / .bash_completion.d / git-funcs
1 if test -x /usr/bin/git >/dev/null 2>&1; then
2     # chdir to a remote's directory (if the remote is on the local FS)
3     cdremote() {
4         if [ $# -gt 1 ]; then
5             echo "Usage: cdremote [remote_name]" >&2
6             return 1
7         fi
8
9         if [ -z "$1" ]; then
10             branch="`git rev-parse --abbrev-ref HEAD`"
11             remote="`git config --get branch.$branch.remote`"
12         else
13             remote="$1"
14         fi
15
16         if [ -n "$remote" ] && git config --get remote.$remote.url |
17                 grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
18             cdgitpath remote.$remote.url
19             return
20         fi
21
22         if [ -n "$1" ]; then
23             echo "Cannot find directory for remote $1" >&2
24             echo "Usage: cdremote [remote_name]" >&2
25             return 1
26         fi
27
28         _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
29         if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
30             remote=${GIT_REMOTES[0]}
31             unset GIT_REMOTES
32             cdgitpath remote.$remote.url
33         else
34             unset GIT_REMOTES
35             echo "Cannot find directory for any remote" >&2
36             echo "Usage: cdremote [remote_name]" >&2
37             return 1
38         fi
39     }
40
41     # completion for `cd_worktree`
42
43     _cd_worktree_comp() {
44         local cur="${COMP_WORDS[COMP_CWORD]}"
45         COMPREPLY=(`compgen -W "$(
46             git worktree list | awk '{s=$3; gsub("[\\\\[\\\\]]", "", s); print s}'
47         )" -- "$cur"`)
48     }
49
50     _cd_worktree_comp_loader() {
51         _completion_loader git
52         unset _cd_worktree_comp_loader
53         complete -F _cd_worktree_comp cd_worktree
54         return 124
55     }
56
57     complete -F _cd_worktree_comp_loader cd_worktree
58
59     # completion for aliases in global .gitconfig
60
61     # fixup rbi rbia rbiap rbip - do refs name completion
62     _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
63     _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
64     _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
65     _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
66     _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
67     #
68     # push-to-all-remotes - do branch name completion
69     _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
70
71     _cdgitpath_complete() {
72         local cur="${COMP_WORDS[COMP_CWORD]}"
73         COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
74     }
75
76     _git_open() {
77         _cdgitpath_complete
78     }
79
80     complete -F _cdgitpath_complete cdgitpath git-open
81
82     # list remotes with URLs matching a regexp
83     _list_remotes() {
84         if [ $# -ne 1 ]; then
85             echo "Usage: _list_remotes remote_regexp" >&2
86             return 1
87         fi
88         declare -ag GIT_REMOTES=()
89         local remote
90         for remote in `git remote`; do
91             if git config --get remote.$remote.url | grep -q "$1"; then
92                 GIT_REMOTES+=($remote)
93             fi
94         done
95     }
96
97     # completion for cdremote and git-open-remote - list remotes with a pattern
98     _list_remotes_completion() {
99         local cur="${COMP_WORDS[COMP_CWORD]}"
100         _list_remotes "$1"
101         COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
102         unset GIT_REMOTES
103     }
104
105     # completion for cdremote - list remotes with directories as URLs
106     _cdremote_complete() {
107         _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
108         if [ ${#COMPREPLY[*]} -eq 0 ]; then
109             _list_remotes_completion .
110         fi
111     }
112
113     complete -F _cdremote_complete cdremote
114
115     # completion for git-open-remote - list remotes with http(s) URLs
116     _git_open_remote() {
117         _list_remotes_completion '^http\(s\)\?://'
118         if [ ${#COMPREPLY[*]} -eq 0 ]; then
119             _list_remotes_completion .
120         fi
121     }
122
123     complete -F _git_open_remote git-open-remote
124 fi