Current workspace is default target of bulk operations

This commit is contained in:
Niklas Schlimm 2017-03-17 17:13:15 +01:00
parent 9260fd92bf
commit d3cccebbd2
3 changed files with 58 additions and 23 deletions

View file

@ -244,7 +244,7 @@ $ git effort bin/* lib/*
* use the "guarded mode" to check on each execution
```bash
usage: git bulk [-g] [-w <ws-name>] <git command>
usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>
git bulk --addworkspace <ws-name> <ws-root-directory>
git bulk --removeworkspace <ws-name>
git bulk --addcurrent <ws-name>
@ -271,7 +271,7 @@ $ git bulk --listall
bulkworkspaces.personal /Users/niklasschlimm/workspaces/personal
```
Run a git command on the repositories:
Run a git command on the repositories of the current workspace:
```bash
$ git bulk fetch
@ -279,7 +279,13 @@ $ git bulk fetch
![fetchdemo](https://cloud.githubusercontent.com/assets/876604/23709805/e8178406-041a-11e7-9a0c-01de5fbf8944.png)
Run a git command on only one workspace ans its repositories:
Run a git command on one specific workspace and its repositories:
```bash
$ git bulk -w personal fetch
```
Run a git command on all workspaces and their repositories:
```bash
$ git bulk -w personal fetch

View file

@ -3,15 +3,17 @@ invers=`tput rev`
reset=`tput sgr0`
txtbld=$(tput bold)
bldred=${txtbld}$(tput setaf 1)
shopt -s extglob
guarded=false
# default option settings
guardedmode=false
singlemode=false
allwsmode=false
#
# print usage message
#
usage() {
echo 1>&2 "usage: git bulk [-g] [-w <ws-name>] <git command>"
echo 1>&2 "usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>"
echo 1>&2 " git bulk --addworkspace <ws-name> <ws-root-directory>"
echo 1>&2 " git bulk --removeworkspace <ws-name>"
echo 1>&2 " git bulk --addcurrent <ws-name>"
@ -36,10 +38,10 @@ function listall { git config --global --get-regexp bulkworkspaces; }
# guarded execution of a git command in one specific repository
function guardedExecution () {
if $guarded; then
if $guardedmode; then
echo -n "${invers}git $gitcommand${reset} -> execute here (y/n)? "
read -n 1 -r </dev/tty; echo
[[ $REPLY =~ ^[Yy]$ ]] && atomicExecution
if [[ $REPLY =~ ^[Yy]$ ]]; then atomicExecution; fi
else
atomicExecution
fi
@ -67,13 +69,26 @@ function checkGitCommand () {
function checkWSName () {
found=0
while read workspace; do
cwsname=$(echo $workspace | cut -f1 -d' ' | cut -f2 -d'.')
[[ $cwsname == $wsname ]] && return
rwsname=$(echo $workspace | cut -f1 -d' ' | cut -f2 -d'.')
if [[ $rwsname == $wsname ]]; then return; fi
done <<< "$(echo "$(listall)")"
# when here the ws name was not found
usage && echo "error: unknown workspace name: $wsname" && exit 1
}
# detects the wsname of the current directory
function wsnameToCurrent () {
currdir=$PWD
while read workspace; do
if [ -z "$workspace" ]; then continue; fi
rwsdir=$(echo $workspace | grep -o ' /.*')
rwsname=$(echo $workspace | cut -f1 -d' ' | cut -f2 -d'.')
if echo $PWD | grep -o -q $rwsdir; then wsname=$rwsname && return; fi
done <<< "$(echo "$(listall)")"
# when here then not in workspace dir
echo "error: you are not in a workspace directory. your workspaces:" && wslist=$(echo "$(listall)") && echo ${wslist:-'<no workspaces defined yet>'}
}
# helper to check number of arguments
function allowedargcount () {
( test $initialcount -ne $1 ) && ( usage && echo 1>&2 "error: wrong number of arguments" && exit 1 )
@ -81,11 +96,11 @@ function allowedargcount () {
# execute the bulk operation
function executBulkOp () {
if ! $allwsmode && ! $singlemode; then wsnameToCurrent; fi # by default git bulk works within the 'current' workspace
checkGitCommand
( [[ $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
if [[ -n $wsname ]] && [[ $cwsname != $wsname ]]; then continue; fi
wslocation=$(echo $workspacespec | cut -f2 -d' ')
eval cd "$wslocation"
actual=$(pwd)
@ -104,15 +119,19 @@ function executBulkOp () {
initialcount="${#}"
# if no arguments show usage
( [[ $initialcount -le 0 ]] ) && usage
if [[ $initialcount -le 0 ]]; then usage; fi
# parse command parameters
while [ "${#}" -ge 1 ] ; do
case "$1" in
--listall|--purge|--removeworkspace|--addcurrent|--addworkspace)
--listall|--purge)
butilcommand="${1:2}" && break ;;
--removeworkspace|--addcurrent|--addworkspace)
butilcommand="${1:2}" && shift && wsname="$1" && shift && wsdir="$1" && break ;;
-a)
allwsmode=true ;;
-g)
guarded=true ;;
guardedmode=true ;;
-w)
singlemode=true && shift && wsname="$1" && checkWSName ;;
-*)
@ -124,10 +143,16 @@ while [ "${#}" -ge 1 ] ; do
esac && shift
done
# check option compatability
if $allwsmode && $singlemode; then echo 1>&2 "error: options -w and -a are incompatible" && exit 1; fi
# if single mode check the supplied workspace name
if $singlemode; then echo "Selected single workspace mode in workspace: $wsname" && checkWSName; fi
# check right number of arguments
case $butilcommand in
@(listall|purge) ) allowedargcount 1;;
@(addcurrent|removeworkspace)) allowedargcount 2;;
listall|purge) allowedargcount 1;;
addcurrent|removeworkspace) allowedargcount 2;;
addworkspace) allowedargcount 3;;
esac

View file

@ -3,7 +3,7 @@ git-bulk(1) -- Run git commands on multiple repositories
## SYNOPSIS
usage: git bulk [-g] [-w &lt;ws-name&gt;] &lt;git command&gt;
usage: usage: git bulk [-g] ([-a]|[-w &lt;ws-name&gt;]) &lt;git command&gt;
git bulk --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt;
git bulk --removeworkspace &lt;ws-name&gt;
git bulk --addcurrent &lt;ws-name&gt;
@ -20,17 +20,21 @@ git bulk adds covenien support for operations that you want to execute on multip
## OPTIONS
-a
Run a GIT command on all workspaces and their repositories.
-g
Ask the user for confitmation on every execution.
-w <ws-name>
-w &lt;ws-name&gt;
Run the GIT command only on the specified workspace. The workspace must be registered.
Run the GIT command on the specified workspace. The workspace must be registered.
&lt;git command&gt;
Any GIT Command you wish to execute on all the Repositories.
Any GIT Command you wish to execute on the repositories.
--addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt;
@ -66,11 +70,11 @@ git bulk adds covenien support for operations that you want to execute on multip
$ git bulk --listall
Run a git command on the repositories:
Run a git command on the repositories of the current workspace:
$ git bulk fetch
Run a git command on only one workspace and its repositories:
Run a git command on the specified workspace and its repositories:
$ git bulk -w personal fetch