From a9aa363fdbd8e7bf01f61baa6c5b8b161ef0ac29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 23 Oct 2012 09:13:20 +0200 Subject: [PATCH 1/7] Added initial zsh completion support to git_index zsh now completes all arguments and all directories inside $GIT_REPO_DIR. This is till buggy because not all directories in $GIT_REPO_DIR are actual git projects. Next update will include completing the list in the .git_index file. --- lib/git/aliases.sh | 2 + lib/git/repo_index.sh | 88 ++++++++++++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/lib/git/aliases.sh b/lib/git/aliases.sh index a7d3bb9..f27eadd 100644 --- a/lib/git/aliases.sh +++ b/lib/git/aliases.sh @@ -146,5 +146,7 @@ if [ $shell = "bash" ]; then # If you know how to rewrite _git_index_tab_completion() for zsh, please send me a pull request! complete -o nospace -F _git_index_tab_completion git_index complete -o nospace -F _git_index_tab_completion $git_index_alias +else + compdef _git_index_tab_completion git_index $git_index_alias fi diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 0c0c181..8ec1c95 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -281,40 +281,66 @@ function _git_index_batch_cmd() { } -# Bash tab completion function for git_index() -function _git_index_tab_completion() { - _check_git_index - local curw - IFS=$'\n' - COMPREPLY=() - curw=${COMP_WORDS[COMP_CWORD]} +if [ $shell = 'bash' ]; then + # Bash tab completion function for git_index() + function _git_index_tab_completion() { + _check_git_index + local curw + IFS=$'\n' + COMPREPLY=() + curw=${COMP_WORDS[COMP_CWORD]} - # If the first part of $curw matches a high-level directory, - # then match on sub-directories for that project - local project=$(echo "$curw" | cut -d "/" -f1) - local base_path=$(\grep "/$project$" "$GIT_REPO_DIR/.git_index" | sed 's/ /\\ /g') + # If the first part of $curw matches a high-level directory, + # then match on sub-directories for that project + local project=$(echo "$curw" | cut -d "/" -f1) + local base_path=$(\grep "/$project$" "$GIT_REPO_DIR/.git_index" | sed 's/ /\\ /g') - # If matching project path was found and curr string contains a /, then complete project sub-directories - if [[ -n "$base_path" && $curw == */* ]]; then - local search_path=$(echo "$curw" | sed "s:^${project/\\/\\\\\\}::") - COMPREPLY=($(compgen -d "$base_path$search_path" | \grep -v "/.git" | sed -e "s:$base_path:$project:" -e "s:$:/:" )) + # If matching project path was found and curr string contains a /, then complete project sub-directories + if [[ -n "$base_path" && $curw == */* ]]; then + local search_path=$(echo "$curw" | sed "s:^${project/\\/\\\\\\}::") + COMPREPLY=($(compgen -d "$base_path$search_path" | \grep -v "/.git" | sed -e "s:$base_path:$project:" -e "s:$:/:" )) - # If curr string starts with /, tab complete top-level directories in root project dir - elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \ - ([ $shell = "zsh" ] && [ "${curw[1,1]}" = "/" ]); then - COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:")) + # If curr string starts with /, tab complete top-level directories in root project dir + elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \ + ([ $shell = "zsh" ] && [ "${curw[1,1]}" = "/" ]); then + COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:")) - # If curr string starts with --, tab complete commands - elif ([ $shell = "bash" ] && [ "${curw:0:2}" = "--" ]) || \ - ([ $shell = "zsh" ] && [ "${curw[1,2]}" = "--" ]); then - local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host" - COMPREPLY=($(compgen -W '$(echo -e "\n"$commands)' -- $curw)) + # If curr string starts with --, tab complete commands + elif ([ $shell = "bash" ] && [ "${curw:0:2}" = "--" ]) || \ + ([ $shell = "zsh" ] && [ "${curw[1,2]}" = "--" ]); then + local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host" + COMPREPLY=($(compgen -W '$(echo -e "\n"$commands)' -- $curw)) - # Else, tab complete the entries in .git_index - else - COMPREPLY=($(compgen -W '$(sed -e "s:.*/::" -e "s:$:/:" "$GIT_REPO_DIR/.git_index" | sort)' -- $curw)) - fi - IFS=$' \t\n' - return 0 -} + # Else, tab complete the entries in .git_index + else + COMPREPLY=($(compgen -W '$(sed -e "s:.*/::" -e "s:$:/:" "$GIT_REPO_DIR/.git_index" | sort)' -- $curw)) + fi + IFS=$' \t\n' + return 0 + } +else + function _git_index_tab_completion() { + typeset -A opt_args + _arguments \ + "--rebuild[Rebuild repository index]" \ + "--update-all[Update all indexed repositories]" \ + "--update-all-with-notifications[Update all indexed repositories with notifications]" \ + "--list[List all repositories currently present in the index]" \ + "--count-by-host[Count all repositories per host]" \ + "--batch-cmd+[Run a git command on all repositories]:git command:->git_command" \ + ":Project name:->projects" \ + && return 0 + + + case "$state" in + projects) + _files -/ -W $GIT_REPO_DIR + ;; + git_command) + ;; + esac + + return 1; + } +fi From 4dec6b288fea34fd7517914d9e366556394f42ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 30 Oct 2012 11:19:24 +0100 Subject: [PATCH 2/7] Eliminated zsh references in completion Since there is another, totally different completion function, this patch eliminates all references to zsh in _git_index_tab_completion. --- lib/git/repo_index.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 8ec1c95..d4d10ab 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -301,13 +301,11 @@ if [ $shell = 'bash' ]; then COMPREPLY=($(compgen -d "$base_path$search_path" | \grep -v "/.git" | sed -e "s:$base_path:$project:" -e "s:$:/:" )) # If curr string starts with /, tab complete top-level directories in root project dir - elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \ - ([ $shell = "zsh" ] && [ "${curw[1,1]}" = "/" ]); then + elif [ "${curw:0:1}" = "/" ]; then COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:")) # If curr string starts with --, tab complete commands - elif ([ $shell = "bash" ] && [ "${curw:0:2}" = "--" ]) || \ - ([ $shell = "zsh" ] && [ "${curw[1,2]}" = "--" ]); then + elif [ "${curw:0:2}" = "--" ]; then local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host" COMPREPLY=($(compgen -W '$(echo -e "\n"$commands)' -- $curw)) From 7194283dfac03e0a242e2bd675aa4d7df0065da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 20 Aug 2013 22:25:30 +0200 Subject: [PATCH 3/7] Declare local variables Since we are using the _arguments helper, we need to define some local variables in order to avoid altering the global environment. --- lib/git/repo_index.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index e517755..dcd16c8 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -319,6 +319,7 @@ if [ $shell = 'bash' ]; then else function _git_index_tab_completion() { typeset -A opt_args + local state state_descr context line _arguments \ "--rebuild[Rebuild repository index]" \ From 025351c48606ac1884723bbf62e764a7bf80d063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 20 Aug 2013 22:30:16 +0200 Subject: [PATCH 4/7] Change completion title It makes more sense to name the category 'Git projects' instead of 'Project name'. --- lib/git/repo_index.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index dcd16c8..41a8dfe 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -328,7 +328,7 @@ else "--list[List all repositories currently present in the index]" \ "--count-by-host[Count all repositories per host]" \ "--batch-cmd+[Run a git command on all repositories]:git command:->git_command" \ - ":Project name:->projects" \ + "1::Git projects:->projects" \ && return 0 From 9f548c417576509deedc5fbe9d44e84e25d5c539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 20 Aug 2013 22:35:16 +0200 Subject: [PATCH 5/7] Basic zsh completion for git_index The script now completes all projects in the index and all files in GIT_PROJECTS directory. --- lib/git/repo_index.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 41a8dfe..dd8220d 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -334,8 +334,15 @@ else case "$state" in projects) - _files -/ -W $GIT_REPO_DIR - ;; + # Only check and rebuild index if necessary + _check_git_index + if [[ $PREFIX == /* ]]; then + PREFIX=$PREFIX[2,-1] + _files -X "Files in project directory" -W $GIT_REPO_DIR + else + compadd -X "Git projects" $(sed -e 's:.*/::' -e 's:$:/:' "$GIT_REPO_DIR/.git_index") && return 0 + fi + ;; git_command) ;; esac From 18fe0accf1f2d8db7e8d989c8eecb1a35652fce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 20 Aug 2013 23:03:01 +0200 Subject: [PATCH 6/7] Complete commands when using batch-cmd --- lib/git/repo_index.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index dd8220d..242a2c8 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -327,7 +327,7 @@ else "--update-all-with-notifications[Update all indexed repositories with notifications]" \ "--list[List all repositories currently present in the index]" \ "--count-by-host[Count all repositories per host]" \ - "--batch-cmd+[Run a git command on all repositories]:git command:->git_command" \ + "--batch-cmd+[Run a command on all repositories]:command:->command" \ "1::Git projects:->projects" \ && return 0 @@ -343,8 +343,11 @@ else compadd -X "Git projects" $(sed -e 's:.*/::' -e 's:$:/:' "$GIT_REPO_DIR/.git_index") && return 0 fi ;; - git_command) - ;; + command) + local ret=1 + _call_function ret _command_names + return ret + ;; esac return 1; From 0de1579dc4dd96c8e312188ddd37974c2c8ae075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljaz=CC=8C=20=22g5pw=22=20Srebrnic=CC=8C?= Date: Tue, 20 Aug 2013 23:03:34 +0200 Subject: [PATCH 7/7] No need to have semicolons --- lib/git/repo_index.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 242a2c8..44964f9 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -350,6 +350,6 @@ else ;; esac - return 1; + return 1 } fi