From 46637dc15c20d313efd2a60f9b771338e354aea0 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 9 Dec 2011 22:59:32 +0800 Subject: [PATCH] Allow change to a top-level directory within $GIT_REPO_DIR by prefixing the arg with '/'. Comes with tab completion. For example, if you have a directory ~/src/rails, you can move here by typing 's /ra[TAB]' --- lib/git/repo_index.sh | 19 +++++++++++++++++-- test/lib/git/repo_index_test.sh | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/git/repo_index.sh b/lib/git/repo_index.sh index 78e2639..7a06839 100644 --- a/lib/git/repo_index.sh +++ b/lib/git/repo_index.sh @@ -29,6 +29,9 @@ # - Produce a count of repos for each host: 'git_index --count-by-host' # - Run a custom command for each repo: 'git_index --batch-cmd ' # +# * You can also change to a top-level directory within $GIT_REPO_DIR by prefixing the argument +# with '/' +# # Examples: # # $ git_index --list @@ -52,7 +55,7 @@ function git_index() { if [ -z "$1" ]; then # Just change to $GIT_REPO_DIR if no params given. cd $GIT_REPO_DIR - else + else if [ "$1" = "--rebuild" ]; then _rebuild_git_index elif [ "$1" = "--update-all" ]; then @@ -71,6 +74,12 @@ function git_index() { sed -e "s/\(\([^/]*\/\/\)\?\([^@]*@\)\?\([^:/]*\)\).*/\1/" | sort | uniq -c echo + + # If $1 starts with '/', change to top-level directory within $GIT_REPO_DIR + elif ([ $shell = "bash" ] && [ "${1:0:1}" = "/" ]) || \ + ([ $shell = "zsh" ] && [ "${1[1]}" = "/" ]); then + if [ -d "$GIT_REPO_DIR$1" ]; then cd "$GIT_REPO_DIR$1"; fi + else _check_git_index # Figure out which directory we need to change to. @@ -231,7 +240,13 @@ function _git_index_tab_completion() { 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:$:/:" )) - # Else, tab complete all the entries in .git_index, plus '--' commands + + # If curr string starts with /, tab complete top-level directories in root project dir + elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \ + ([ $shell = "zsh" ] && [ "${curw[1]}" = "/" ]); then + COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:")) + + # Else, tab complete the entries in .git_index, plus '--' commands else local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host" COMPREPLY=($(compgen -W '$(sed -e "s:.*/::" -e "s:$:/:" "$GIT_REPO_DIR/.git_index" | sort)$(echo -e "\n"$commands)' -- $curw)) diff --git a/test/lib/git/repo_index_test.sh b/test/lib/git/repo_index_test.sh index 85d679a..bb60be6 100755 --- a/test/lib/git/repo_index_test.sh +++ b/test/lib/git/repo_index_test.sh @@ -179,6 +179,14 @@ test_git_index_tab_completion() { } +# Test changing to top-level directory (when arg begins with '/') +test_changing_to_top_level_directory() { + mkdir "$GIT_REPO_DIR/gems" + git_index "/gems" + assertEquals "$GIT_REPO_DIR/gems" "$PWD" +} + + # load and run shUnit2 # Call this function to run tests . "$scmbDir/test/support/shunit2"