Skip to content

Commit 02263ca

Browse files
authored
Merge pull request #957 from jackwasey/quiet-or-stderr
quiet option, errors to stderr
2 parents f556430 + c036c9c commit 02263ca

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

bin/git-bulk

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ bldred=${txtbld}$(tput setaf 1)
88
guardedmode=false
99
singlemode=false
1010
allwsmode=false
11+
quiet=false
1112

1213
#
1314
# print usage message
1415
#
1516
usage() {
16-
echo 1>&2 "usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>"
17+
echo 1>&2 "usage: git bulk [-q|--quiet] [-g] ([-a]|[-w <ws-name>]) <git command>"
1718
echo 1>&2 " git bulk --addworkspace <ws-name> <ws-root-directory> (--from <URL or file>)"
1819
echo 1>&2 " git bulk --removeworkspace <ws-name>"
1920
echo 1>&2 " git bulk --addcurrent <ws-name>"
@@ -25,7 +26,7 @@ usage() {
2526
function addworkspace {
2627
git config --global bulkworkspaces."$wsname" "$wsdir";
2728
if [ ! -z "$source" ]; then
28-
if [ ! -d "$wsdir" ]; then echo "Path of workspace doesn't exist, make it first."; exit 1; fi
29+
if [ ! -d "$wsdir" ]; then echo 1>&2 "Path of workspace doesn't exist, make it first."; exit 1; fi
2930
regex='http(s)?://|ssh://|(git@)?.*:.*/.*'
3031
if [[ "$source" =~ $regex ]]; then
3132
pushd "$wsdir" > /dev/null
@@ -59,8 +60,8 @@ function listall { git config --global --get-regexp bulkworkspaces; }
5960
# guarded execution of a git command in one specific repository
6061
function guardedExecution () {
6162
if $guardedmode; then
62-
echo -n "${inverse}git $gitcommand${reset} -> execute here (y/n)? "
63-
read -n 1 -r </dev/tty; echo
63+
echo 1>&2 -n "${inverse}git $gitcommand${reset} -> execute here (y/n)? "
64+
read -n 1 -r </dev/tty; echo 1>&2
6465
if [[ $REPLY =~ ^[Yy]$ ]]; then atomicExecution "$@"; fi
6566
else
6667
atomicExecution "$@"
@@ -69,18 +70,18 @@ function guardedExecution () {
6970

7071
# atomic git command execution with log
7172
function atomicExecution () {
72-
echo "${bldred}->${reset} executing ${inverse}git $gitcommand${reset}" && git "$@"
73+
echo 1>&2 "${bldred}->${reset} executing ${inverse}git $gitcommand${reset}" && git "$@"
7374
}
7475

7576
# check if the passed command is known as a core git command
7677
function checkGitCommand () {
7778
if git help -a | grep -o -q "\b${corecommand}\b"; then
78-
echo "Core command \"$corecommand\" accepted."
79+
echo 1>&2 "Core command \"$corecommand\" accepted."
7980
else
8081
if git config --get-regexp alias | grep -o -q "\.${corecommand} "; then
81-
echo "Alias ${corecommand} accepted."
82+
echo 1>&2 "Alias ${corecommand} accepted."
8283
else
83-
usage && echo "error: unknown GIT command: $corecommand" && exit 1
84+
usage && echo 1>&2 "error: unknown GIT command: $corecommand" && exit 1
8485
fi
8586
fi
8687
}
@@ -92,7 +93,7 @@ function checkWSName () {
9293
if [[ $rwsname == "$wsname" ]]; then return; fi
9394
done <<< "$(echo "$(listall)")"
9495
# when here the ws name was not found
95-
usage && echo "error: unknown workspace name: $wsname" && exit 1
96+
usage && echo 1>&2 "error: unknown workspace name: $wsname" && exit 1
9697
}
9798

9899
# parse out wsname from workspacespec
@@ -107,11 +108,11 @@ function wsnameToCurrent () {
107108
while read workspace; do
108109
if [ -z "$workspace" ]; then continue; fi
109110
parseWsName "$workspace"
110-
if echo "$PWD" | grep -o -q "$rwsdir"; then wsname="$rwsname" && return; fi
111+
if echo 1>&2 "$PWD" | grep -o -q "$rwsdir"; then wsname="$rwsname" && return; fi
111112
done <<< "$(echo "$(listall)")"
112113
# when here then not in workspace dir
113-
echo "error: you are not in a workspace directory. your registered workspaces are:" && \
114-
wslist="$(echo "$(listall)")" && echo "${wslist:-'<no workspaces defined yet>'}" && exit 1
114+
echo 1>&2 "error: you are not in a workspace directory. your registered workspaces are:" && \
115+
wslist="$(echo "$(listall)")" && echo 1>&2 "${wslist:-'<no workspaces defined yet>'}" && exit 1
115116
}
116117

117118
# helper to check number of arguments.
@@ -131,13 +132,13 @@ function executBulkOp () {
131132
if [[ -n $wsname ]] && [[ $rwsname != "$wsname" ]]; then continue; fi
132133
eval cd "\"$rwsdir\""
133134
local actual=$(pwd)
134-
echo "Executing bulk operation in workspace ${inverse}$actual${reset}"
135+
[ "${quiet?}" != "false" ] && echo 1>&2 "Executing bulk operation in workspace ${inverse}$actual${reset}"
135136
eval find -L . -name ".git" | while read line; do
136137
local gitrepodir=${line::${#line}-5} # cut the .git part of find results to have the root git directory of that repository
137138
eval cd "\"$gitrepodir\"" # into git repo location
138139
local curdir=$(pwd)
139140
local leadingpath=${curdir#${actual}}
140-
echo "Current repository: ${leadingpath%/*}/${bldred}${curdir##*/}${reset}"
141+
[ "${quiet?}" != "false" ] && echo 1>&2 "Current repository: ${leadingpath%/*}/${bldred}${curdir##*/}${reset}"
141142
guardedExecution "$@"
142143
eval cd "\"$rwsdir\"" # back to origin location of last find command
143144
done
@@ -152,6 +153,7 @@ if [[ $paramcount -le 0 ]]; then usage; fi
152153
# parse command parameters
153154
while [ "${#}" -ge 1 ] ; do
154155
case "$1" in
156+
--quiet|-q) quiet='true' ;;
155157
--listall|--purge)
156158
butilcommand="${1:2}" && break ;;
157159
--removeworkspace|--addcurrent|--addworkspace)
@@ -162,10 +164,10 @@ while [ "${#}" -ge 1 ] ; do
162164
guardedmode=true ;;
163165
-w)
164166
singlemode=true && shift && wsname="$1" && checkWSName ;;
165-
-*)
166-
usage && echo 1>&2 "error: unknown argument $1" && exit 1 ;;
167167
--*)
168168
usage && echo 1>&2 "error: unknown argument $1" && exit 1 ;;
169+
-*)
170+
usage && echo 1>&2 "error: unknown argument $1" && exit 1 ;;
169171
*) # git core commands
170172
butilcommand="executBulkOp" && corecommand="$1" && gitcommand="$*" && break ;;
171173
esac && shift
@@ -175,7 +177,7 @@ done
175177
if $allwsmode && $singlemode; then echo 1>&2 "error: options -w and -a are incompatible" && exit 1; fi
176178

177179
# if single mode check the supplied workspace name
178-
if $singlemode; then echo "Selected single workspace mode in workspace: $wsname" && checkWSName; fi
180+
if $singlemode; then echo 1>&2 "Selected single workspace mode in workspace: $wsname" && checkWSName; fi
179181

180182
# check right number of arguments
181183
case $butilcommand in

0 commit comments

Comments
 (0)