Renamed 'git_repo' to more appropriate 'git_index'

This commit is contained in:
Nathan Broadbent
2011-10-18 01:27:55 +08:00
parent b98f7b0b21
commit 425a8ab264
3 changed files with 67 additions and 67 deletions

View File

@@ -43,7 +43,7 @@ alias $git_commit_amend_alias='git commit --amend'
alias $git_commit_amend_no_msg_alias='git commit --amend -C HEAD' alias $git_commit_amend_no_msg_alias='git commit --amend -C HEAD'
# Git repo management alias # Git repo management alias
alias $git_repo_alias="git_repo" # The 's' stands for 'switch' or 'sourcecode' alias $git_index_alias="git_index"
# Tab completion for aliases # Tab completion for aliases
@@ -86,9 +86,9 @@ else
fi fi
# Git repo management & aliases. # Git repo management & aliases.
# If you know how to rewrite _git_repo_tab_completion() for zsh, please send me a pull request! # If you know how to rewrite _git_index_tab_completion() for zsh, please send me a pull request!
complete -o nospace -o filenames -F _git_repo_tab_completion git_repo complete -o nospace -o filenames -F _git_index_tab_completion git_index
complete -o nospace -o filenames -F _git_repo_tab_completion $git_repo_alias complete -o nospace -o filenames -F _git_index_tab_completion $git_index_alias
# Keyboard Bindings # Keyboard Bindings

View File

@@ -5,11 +5,11 @@
# ------------------------------------------------------- # -------------------------------------------------------
# ------------------------------------------------------- # -------------------------------------------------------
# Repository management scripts for Git projects # Repository Indexing scripts for Git projects
# ------------------------------------------------------- # -------------------------------------------------------
# * The `git_repo` function makes it easy to list & switch between # * The `git_index` function makes it easy to list & switch between
# git projects in $GIT_REPO_DIR (default = ~/src) # git projects in $GIT_REPO_DIR (default = ~/src)
# #
# * Change directory to any of your git repos or submodules, with recursive tab completion. # * Change directory to any of your git repos or submodules, with recursive tab completion.
@@ -18,61 +18,61 @@
# (Scanning for git projects and submodules can take a few seconds.) # (Scanning for git projects and submodules can take a few seconds.)
# #
# * Cache can be rebuilt by running: # * Cache can be rebuilt by running:
# $ git_repo --rebuild-index # $ git_index --rebuild-index
# ('--' commands have tab completion too.) # ('--' commands have tab completion too.)
# #
# * Ignores projects within an 'archive' folder. # * Ignores projects within an 'archive' folder.
# #
# * Allows you to run batch commands across all your repositories: # * Allows you to run batch commands across all your repositories:
# #
# - Update every repo from their remote: 'git_repo --update-all' # - Update every repo from their remote: 'git_index --update-all'
# - Produce a count of repos for each host: 'git_repo --count-by-host' # - Produce a count of repos for each host: 'git_index --count-by-host'
# - Run a custom command for each repo: 'git_repo --batch-cmd <command>' # - Run a custom command for each repo: 'git_index --batch-cmd <command>'
# #
# Examples: # Examples:
# #
# $ git_repo --list # $ git_index --list
# # => Lists all git projects # # => Lists all git projects
# #
# $ git_repo ub[TAB] # $ git_index ub[TAB]
# # => Provides tab completion for all project folders that begin with 'ub' # # => Provides tab completion for all project folders that begin with 'ub'
# #
# $ git_repo ubuntu_config # $ git_index ubuntu_config
# # => Changes directory to ubuntu_config, and auto-updates code from git remote. # # => Changes directory to ubuntu_config, and auto-updates code from git remote.
# #
# $ git_repo buntu_conf # $ git_index buntu_conf
# # => Same result as `git_repo ubuntu_config` # # => Same result as `git_index ubuntu_config`
# #
# $ git_repo # $ git_index
# # => cd $GIT_REPO_DIR # # => cd $GIT_REPO_DIR
function git_repo() { function git_index() {
local IFS=$'\n' local IFS=$'\n'
if [ -z "$1" ]; then if [ -z "$1" ]; then
# Just change to $GIT_REPO_DIR if no params given. # Just change to $GIT_REPO_DIR if no params given.
cd $GIT_REPO_DIR cd $GIT_REPO_DIR
else else
if [ "$1" = "--rebuild-index" ]; then if [ "$1" = "--rebuild-index" ]; then
_rebuild_git_repo_index _rebuild_git_index
elif [ "$1" = "--update-all" ]; then elif [ "$1" = "--update-all" ]; then
_git_repo_git_update_all _git_index_git_update_all
elif [ "$1" = "--batch-cmd" ]; then elif [ "$1" = "--batch-cmd" ]; then
_git_repo_git_batch_cmd "${@:2:$(($#-1))}" # Pass all args except $1 _git_index_git_batch_cmd "${@:2:$(($#-1))}" # Pass all args except $1
elif [ "$1" = "--list" ] || [ "$1" = "-l" ]; then elif [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
echo -e "$_bld_col$(_git_repo_count)$_txt_col Git repositories in $_bld_col$GIT_REPO_DIR$_txt_col:\n" echo -e "$_bld_col$(_git_index_count)$_txt_col Git repositories in $_bld_col$GIT_REPO_DIR$_txt_col:\n"
for repo in $(_git_repo_dirs_without_home); do for repo in $(_git_index_dirs_without_home); do
echo $(basename $repo) : $repo echo $(basename $repo) : $repo
done | sort | column -t -s ':' done | sort | column -t -s ':'
elif [ "$1" = "--count-by-host" ]; then elif [ "$1" = "--count-by-host" ]; then
echo -e "=== Producing a report of the number of repos per host...\n" echo -e "=== Producing a report of the number of repos per host...\n"
_git_repo_batch_cmd git remote -v | grep "origin.*(fetch)" | _git_index_batch_cmd git remote -v | grep "origin.*(fetch)" |
sed -e "s/origin\s*//" -e "s/(fetch)//" | sed -e "s/origin\s*//" -e "s/(fetch)//" |
sed -e "s/\(\([^/]*\/\/\)\?\([^@]*@\)\?\([^:/]*\)\).*/\1/" | sed -e "s/\(\([^/]*\/\/\)\?\([^@]*@\)\?\([^:/]*\)\).*/\1/" |
sort | uniq -c sort | uniq -c
echo echo
else else
_check_git_repo_index _check_git_index
# Figure out which directory we need to change to. # Figure out which directory we need to change to.
local project=$(echo $1 | cut -d "/" -f1) local project=$(echo $1 | cut -d "/" -f1)
# Find base path of project # Find base path of project
@@ -84,16 +84,16 @@ function git_repo() {
fi fi
# Try partial matches # Try partial matches
# - string at beginning of project # - string at beginning of project
if [ -z "$base_path" ]; then base_path=$(_git_repo_dirs_without_home | grep -m1 "/$project"); fi if [ -z "$base_path" ]; then base_path=$(_git_index_dirs_without_home | grep -m1 "/$project"); fi
# - string anywhere in project # - string anywhere in project
if [ -z "$base_path" ]; then base_path=$(_git_repo_dirs_without_home | grep -m1 "$project"); fi if [ -z "$base_path" ]; then base_path=$(_git_index_dirs_without_home | grep -m1 "$project"); fi
# -------------------- # --------------------
# Go to our base path # Go to our base path
if [ -n "$base_path" ]; then if [ -n "$base_path" ]; then
unset IFS unset IFS
eval cd "$base_path" # eval turns ~ into $HOME eval cd "$base_path" # eval turns ~ into $HOME
# Run git callback (either update or show changes), if we are in the root directory # Run git callback (either update or show changes), if we are in the root directory
if [ -z "${sub_path%/}" ]; then _git_repo_pull_or_status; fi if [ -z "${sub_path%/}" ]; then _git_index_pull_or_status; fi
else else
echo -e "$_wrn_col'$1' did not match any git repos in $GIT_REPO_DIR$_txt_col" echo -e "$_wrn_col'$1' did not match any git repos in $GIT_REPO_DIR$_txt_col"
fi fi
@@ -101,7 +101,7 @@ function git_repo() {
fi fi
} }
_git_repo_dirs_without_home() { _git_index_dirs_without_home() {
sed -e "s/--.*//" -e "s%$HOME%~%" $GIT_REPO_DIR/.git_index sed -e "s/--.*//" -e "s%$HOME%~%" $GIT_REPO_DIR/.git_index
} }
@@ -124,7 +124,7 @@ function _find_git_submodules() {
# Rebuilds index of git repos in $GIT_REPO_DIR. # Rebuilds index of git repos in $GIT_REPO_DIR.
function _rebuild_git_repo_index() { function _rebuild_git_index() {
if [ "$1" != "--silent" ]; then echo -e "== Scanning $GIT_REPO_DIR for git repos & submodules..."; fi if [ "$1" != "--silent" ]; then echo -e "== Scanning $GIT_REPO_DIR for git repos & submodules..."; fi
# Get repos from src dir and custom dirs, then sort by basename # Get repos from src dir and custom dirs, then sort by basename
local IFS=$'\n' local IFS=$'\n'
@@ -133,24 +133,24 @@ function _rebuild_git_repo_index() {
done | sort | cut -d " " -f2- > "$GIT_REPO_DIR/.git_index" done | sort | cut -d " " -f2- > "$GIT_REPO_DIR/.git_index"
if [ "$1" != "--silent" ]; then if [ "$1" != "--silent" ]; then
echo -e "===== Cached $_bld_col$(_git_repo_count)$_txt_col repos in $GIT_REPO_DIR/.git_index" echo -e "===== Cached $_bld_col$(_git_index_count)$_txt_col repos in $GIT_REPO_DIR/.git_index"
fi fi
} }
# Build index if empty # Build index if empty
function _check_git_repo_index() { function _check_git_index() {
if [ ! -f "$GIT_REPO_DIR/.git_index" ]; then if [ ! -f "$GIT_REPO_DIR/.git_index" ]; then
_rebuild_git_repo_index --silent _rebuild_git_index --silent
fi fi
} }
# Produces a count of repos in the tab completion index (excluding commands) # Produces a count of repos in the tab completion index (excluding commands)
function _git_repo_count() { function _git_index_count() {
echo $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | wc -l) echo $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | wc -l)
} }
# If the working directory is clean, update the git repository. Otherwise, show changes. # If the working directory is clean, update the git repository. Otherwise, show changes.
function _git_repo_pull_or_status() { function _git_index_pull_or_status() {
if ! [ `git status --porcelain | wc -l` -eq 0 ]; then if ! [ `git status --porcelain | wc -l` -eq 0 ]; then
# Fall back to 'git status' if git status alias isn't configured # Fall back to 'git status' if git status alias isn't configured
if type $git_status_command 2>&1 | grep -qv "not found"; then if type $git_status_command 2>&1 | grep -qv "not found"; then
@@ -179,19 +179,19 @@ function _git_repo_pull_or_status() {
} }
# Updates all git repositories with clean working directories. # Updates all git repositories with clean working directories.
function _git_repo_update_all() { function _git_index_update_all() {
echo -e "== Updating code in $_bld_col$(_git_repo_count)$_txt_col repos...\n" echo -e "== Updating code in $_bld_col$(_git_index_count)$_txt_col repos...\n"
for base_path in $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | sort); do for base_path in $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | sort); do
echo -e "===== Updating code in \e[1;32m$base_path\e[0m...\n" echo -e "===== Updating code in \e[1;32m$base_path\e[0m...\n"
cd "$base_path" cd "$base_path"
_git_repo_pull_or_status _git_index_pull_or_status
done done
} }
# Runs a command for all git repos # Runs a command for all git repos
function _git_repo_batch_cmd() { function _git_index_batch_cmd() {
if [ -n "$1" ]; then if [ -n "$1" ]; then
echo -e "== Running command for $_bld_col$(_git_repo_count)$_txt_col repos...\n" echo -e "== Running command for $_bld_col$(_git_index_count)$_txt_col repos...\n"
for base_path in $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | sort); do for base_path in $(sed -e "s/--.*//" "$GIT_REPO_DIR/.git_index" | grep . | sort); do
cd "$base_path" cd "$base_path"
$@ $@
@@ -202,9 +202,9 @@ function _git_repo_batch_cmd() {
} }
# Bash tab completion function for git_repo() # Bash tab completion function for git_index()
function _git_repo_tab_completion() { function _git_index_tab_completion() {
_check_git_repo_index _check_git_index
local curw local curw
local IFS=$'\n' local IFS=$'\n'
COMPREPLY=() COMPREPLY=()

View File

@@ -17,7 +17,7 @@ if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwords
# Load functions to test # Load functions to test
. "$scmbDir/lib/_shared.sh" . "$scmbDir/lib/_shared.sh"
. "$scmbDir/lib/git/repo_management.sh" . "$scmbDir/lib/git/repo_index.sh"
# Setup and tear down # Setup and tear down
@@ -77,7 +77,7 @@ oneTimeTearDown() {
} }
ensureIndex() { ensureIndex() {
_check_git_repo_index _check_git_index
} }
index_no_newlines() { index_no_newlines() {
@@ -90,7 +90,7 @@ index_no_newlines() {
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
test_repo_index_command() { test_repo_index_command() {
git_repo --rebuild-index > /dev/null git_index --rebuild-index > /dev/null
# Test that all repos are detected, and sorted alphabetically # Test that all repos are detected, and sorted alphabetically
assertIncludes "$(index_no_newlines)" "bitbucket.*\ assertIncludes "$(index_no_newlines)" "bitbucket.*\
@@ -105,45 +105,45 @@ test_repo_1"
} }
test_check_git_repo_index() { test_check_git_index() {
ensureIndex ensureIndex
echo "should not be regenerated" >> $git_index_file echo "should not be regenerated" >> $git_index_file
_check_git_repo_index _check_git_index
# Test that index is not rebuilt unless empty # Test that index is not rebuilt unless empty
assertIncludes "$(index_no_newlines)" "should not be regenerated" assertIncludes "$(index_no_newlines)" "should not be regenerated"
rm $git_index_file rm $git_index_file
# Test the index is rebuilt # Test the index is rebuilt
_check_git_repo_index _check_git_index
assertTrue "[ -f $git_index_file ]" assertTrue "[ -f $git_index_file ]"
} }
test_git_repo_count() { test_git_index_count() {
assertEquals "9" "$(_git_repo_count)" assertEquals "9" "$(_git_index_count)"
} }
test_repo_list() { test_repo_list() {
ensureIndex ensureIndex
list=$(git_repo --list) list=$(git_index --list)
assertIncludes "$list" "bitbucket" || return assertIncludes "$list" "bitbucket" || return
assertIncludes "$list" "blue_submodule" || return assertIncludes "$list" "blue_submodule" || return
assertIncludes "$list" "test_repo_11" assertIncludes "$list" "test_repo_11"
} }
# Test matching rules for changing directory # Test matching rules for changing directory
test_git_repo_changing_directory() { test_git_index_changing_directory() {
ensureIndex ensureIndex
git_repo "github"; assertEquals "$GIT_REPO_DIR/github" "$PWD" git_index "github"; assertEquals "$GIT_REPO_DIR/github" "$PWD"
git_repo "github/"; assertEquals "$GIT_REPO_DIR/github" "$PWD" git_index "github/"; assertEquals "$GIT_REPO_DIR/github" "$PWD"
git_repo "bucket"; assertEquals "$GIT_REPO_DIR/bitbucket" "$PWD" git_index "bucket"; assertEquals "$GIT_REPO_DIR/bitbucket" "$PWD"
git_repo "green_sub"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/green_submodule" "$PWD" git_index "green_sub"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/green_submodule" "$PWD"
git_repo "_submod"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/blue_submodule" "$PWD" git_index "_submod"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/blue_submodule" "$PWD"
git_repo "test_repo_1"; assertEquals "/tmp/test_repo_1" "$PWD" git_index "test_repo_1"; assertEquals "/tmp/test_repo_1" "$PWD"
git_repo "test_repo_11"; assertEquals "/tmp/test_repo_11" "$PWD" git_index "test_repo_11"; assertEquals "/tmp/test_repo_11" "$PWD"
git_repo "test_repo_"; assertEquals "/tmp/test_repo_11" "$PWD" git_index "test_repo_"; assertEquals "/tmp/test_repo_11" "$PWD"
git_repo "github/videos/octocat/live_action"; assertEquals "$GIT_REPO_DIR/github/videos/octocat/live_action" "$PWD" git_index "github/videos/octocat/live_action"; assertEquals "$GIT_REPO_DIR/github/videos/octocat/live_action" "$PWD"
} }
test_git_repo_tab_completion() { test_git_index_tab_completion() {
# Only run tab completion test for bash # Only run tab completion test for bash
if [[ "$0" == *bash ]]; then if [[ "$0" == *bash ]]; then
ensureIndex ensureIndex
@@ -151,29 +151,29 @@ test_git_repo_tab_completion() {
# Test that '--' commands have tab completion # Test that '--' commands have tab completion
COMP_WORDS="--" COMP_WORDS="--"
_git_repo_tab_completion _git_index_tab_completion
assertEquals "Incorrect number of tab-completed '--' commands" "5" "$(tab_completions | wc -w)" assertEquals "Incorrect number of tab-completed '--' commands" "5" "$(tab_completions | wc -w)"
COMP_WORDS="gith" COMP_WORDS="gith"
_git_repo_tab_completion _git_index_tab_completion
assertIncludes "$(tab_completions)" "github/" assertIncludes "$(tab_completions)" "github/"
# Test completion for project sub-directories when project ends with '/' # Test completion for project sub-directories when project ends with '/'
COMP_WORDS="github/" COMP_WORDS="github/"
_git_repo_tab_completion _git_index_tab_completion
assertIncludes "$(tab_completions)" "github/videos/" assertIncludes "$(tab_completions)" "github/videos/"
# Check that '.git/' is filtered from completion, but other hidden dirs are available # Check that '.git/' is filtered from completion, but other hidden dirs are available
assertNotIncludes "$(tab_completions)" "github/.git/" assertNotIncludes "$(tab_completions)" "github/.git/"
assertIncludes "$(tab_completions)" "github/.im_hidden/" assertIncludes "$(tab_completions)" "github/.im_hidden/"
COMP_WORDS="github/videos/" COMP_WORDS="github/videos/"
_git_repo_tab_completion _git_index_tab_completion
assertIncludes "$(tab_completions)" "github/videos/octocat/" assertIncludes "$(tab_completions)" "github/videos/octocat/"
# Test that completion checks for other matching projects even if one matches perfectly # Test that completion checks for other matching projects even if one matches perfectly
COMP_WORDS="test_repo_1" COMP_WORDS="test_repo_1"
_git_repo_tab_completion _git_index_tab_completion
assertIncludes "$(tab_completions)" "test_repo_1/ test_repo_11/" assertIncludes "$(tab_completions)" "test_repo_1/ test_repo_11/"
fi fi
} }