diff --git a/bin/git-bulk b/bin/git-bulk index 553f9d5..a135dc5 100755 --- a/bin/git-bulk +++ b/bin/git-bulk @@ -1,8 +1,8 @@ #!/usr/bin/env bash invers=`tput rev` reset=`tput sgr0` -txtbld=$(tput bold) # Bold -bldred=${txtbld}$(tput setaf 1) # red +txtbld=$(tput bold) +bldred=${txtbld}$(tput setaf 1) shopt -s extglob guarded=false singlemode=false @@ -20,39 +20,26 @@ usage() { } # add another workspace to global git config -function addworkspace () { - git config --global bulkworkspaces.$wsname "$wsdir" -} +function addworkspace { git config --global bulkworkspaces.$wsname "$wsdir"; } # add current directory -function addcurrent () { - git config --global bulkworkspaces.$wsname "$PWD" -} +function addcurrent { git config --global bulkworkspaces.$wsname "$PWD"; } # remove workspace from global git config -function removeworkspace () { - checkWSName - git config --global --unset bulkworkspaces.$wsname -} +function removeworkspace { checkWSName && git config --global --unset bulkworkspaces.$wsname; } # remove workspace from global git config -function purge () { - git config --global --remove-section bulkworkspaces -} +function purge { git config --global --remove-section bulkworkspaces; } # list all current workspace locations defined -function listall () { - git config --global --get-regexp bulkworkspaces -} +function listall { git config --global --get-regexp bulkworkspaces; } # guarded execution of a git command in one specific repository function guardedExecution () { if $guarded; then echo -n "${invers}git $gitcommand${reset} -> execute here (y/n)? " read -n 1 -r ${reset} executing ${invers}git $gitcommand${reset}" - git $gitcommand + echo "${bldred}->${reset} executing ${invers}git $gitcommand${reset}" && git $gitcommand } # check if the passed command is known as a core git command function checkGitCommand () { - if git help -a | cat | grep -o -q "$corecommand"; then + if git help -a | cat | grep -o -q "\b${corecommand}\b"; then echo "Core command \"$corecommand\" accepted." else - usage - echo "error: unknown GIT command: $corecommand" - exit 1 + if git config --get-regexp alias | grep -o -q "\.${corecommand} "; then + echo "Alias ${corecommand} accepted." + else + usage && echo "error: unknown GIT command: $corecommand" && exit 1 + fi fi } @@ -80,30 +68,21 @@ function checkWSName () { found=0 while read workspace; do cwsname=$(echo $workspace | cut -f1 -d' ' | cut -f2 -d'.') - if [[ $cwsname == $wsname ]]; then return; fi + [[ $cwsname == $wsname ]] && return done <<< "$(echo "$(listall)")" # when here the ws name was not found - usage - echo "error: unknown workspace name: $wsname" - exit 1 + usage && echo "error: unknown workspace name: $wsname" && exit 1 } # helper to check number of arguments function allowedargcount () { - if [[ $initialcount -ne $1 ]]; then - usage - echo 1>&2 "error: wrong number of arguments" - exit 1 - fi + ( test $initialcount -ne $1 ) && ( usage && echo 1>&2 "error: wrong number of arguments" && exit 1 ) } # execute the bulk operation function executBulkOp () { checkGitCommand - if $singlemode; then - echo "Selected single workspace mode in workspace: $wsname" - checkWSName - fi + ( [[ $singlemode ]] ) && ( echo "Selected single workspace mode in workspace: $wsname" && checkWSName ) listall | while read workspacespec; do cwsname=$(echo $workspacespec | cut -f1 -d' ' | cut -f2 -d'.') if $singlemode && [[ $cwsname != $wsname ]]; then continue; fi @@ -123,52 +102,26 @@ function executBulkOp () { } initialcount="${#}" -initialarguments="${@}" # if no arguments show usage -if [[ $initialcount -le 0 ]]; then - usage -fi +( [[ $initialcount -le 0 ]] ) && usage # parse command parameters while [ "${#}" -ge 1 ] ; do - case "$1" in --listall|--purge|--removeworkspace|--addcurrent|--addworkspace) - butilcommand="${1:2}" - shift - wsname="$1" - shift - wsdir="$1" - break - ;; + butilcommand="${1:2}" && shift && wsname="$1" && shift && wsdir="$1" && break ;; -g) - guarded=true - ;; + guarded=true ;; -w) - singlemode=true - shift - wsname="$1" - checkWSName - ;; + singlemode=true && shift && wsname="$1" && checkWSName ;; -*) - usage - echo 1>&2 "error: unknown argument $1" - exit 1 - ;; + usage && echo 1>&2 "error: unknown argument $1" && exit 1 ;; --*) - usage - echo 1>&2 "error: unknown argument $1" - exit 1 - ;; + usage && echo 1>&2 "error: unknown argument $1" && exit 1 ;; *) # git core commands - butilcommand="executBulkOp" - corecommand="$1" - gitcommand="$@" - break - ;; - esac - shift + butilcommand="executBulkOp" && corecommand="$1" && gitcommand="$@" && break ;; + esac && shift done # check right number of arguments