Skip to content

Commit 8ce6300

Browse files
author
Pierre Ayoub
authored
fix(git-bulk): fix workspace selection when cd fails (#1197)
* fix(git-bulk): fix workspace selection when cd fails `cd` may fails for multiple reasons: - mistake when editing `.gitconfig` manually - previously existing workspace that have been removed - ... Currently, if `cd` fails, the `BulkOp` continue its execution ... in the workspace defined in a higher directory that where the user, despite the user specified a specific workspace (`-w`). The user should be noticed of a failed `cd` (this is really not expected for a valid configuration) and the operations should stop to not execute something unexpected. * fix(git-bulk): replace weak eval for better variable substitution Get rid of poor `eval` syntax because they are vulnerable to command injection, which may have unexpected side effects. However, they enabled a useful feature: using environment variable (*e.g.*, defined in a `.bashrc`) inside the `.gitconfig` to use dynamic paths as `bulk` workspaces. As such, I keep this feature possible by using the Bash's ${!VAR} syntax, which allows to get the value of one variable using the name of a another variable. However, arbitrary command injection is not possible anymore. * fix(git-bulk): missing check about empty environnement variable * style(git-bulk): typo * docs(Commands.md): git-bulk env var feature * docs(man/git-bulk): git-bulk env var feature * docs(man/git-bulk): mention .gitconfig for config storage * style(man/git-bulk): typo * docs(man/git-bulk): run make/ronn for .1 and .html
1 parent 1a9b0c2 commit 8ce6300

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

Commands.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,20 @@ usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>
280280
git bulk --listall
281281
```
282282

283-
Register a workspace so that `git bulk` knows about it (notice that <ws-root-directory> must be absolute path):
283+
Register a workspace so that `git bulk` knows about it (it will be registered in your `.gitconfig`):
284284

285285
```bash
286286
$ git bulk --addworkspace personal ~/workspaces/personal
287287
```
288+
289+
Notice that `<ws-root-directory>` must be an absolute path (or an environment variable pointing to an absolute path).
290+
In the case of a **single quoted environment variable**, it will be dereferenced at `git-bulk` runtime, suitable for dynamic workspaces (*e.g.*, defined in your `.bashrc`).
291+
As an illustration:
292+
293+
```bash
294+
$ git bulk --addworkspace personal '$PERSONAL_WORKSPACE'
295+
```
296+
288297
With option `--from` the URL to a single repository or a file containing multiple URLs can be added and they will be cloned directly into the workspace. Suitable for the initial setup of a multi-repo project.
289298

290299
```bash

bin/git-bulk

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ function checkWSName () {
111111
# parse out wsname from workspacespec
112112
function parseWsName () {
113113
local wsspec="$1"
114+
# Get the workspace value from its specification in the `.gitconfig`.
115+
# May be an absolute path or a variable name of the form: `$VARNAME`
114116
rwsdir=${wsspec#* }
117+
if [[ ${rwsdir:0:1} == '$' ]]; then
118+
# Dereference the `rwsdir` value which is a variable name.
119+
rwsdir_varname=${rwsdir:1}
120+
rwsdir=${!rwsdir_varname}
121+
if [[ -z "${rwsdir}" ]]; then
122+
echo 1>&2 "error: bad environment variable: $rwsdir_varname" && exit 1
123+
fi
124+
fi
115125
rwsname=${wsspec#*.} && rwsname=${rwsname%% *}
116126
}
117127

@@ -142,19 +152,19 @@ function executBulkOp () {
142152
listall | while read -r workspacespec; do
143153
parseWsName "$workspacespec"
144154
if [[ -n $wsname ]] && [[ $rwsname != "$wsname" ]]; then continue; fi
145-
eval cd "\"$rwsdir\""
155+
cd "$rwsdir" || exit 1
146156
local actual=$PWD
147157
[ "${quiet?}" != "true" ] && echo 1>&2 "Executing bulk operation in workspace ${inverse}$actual${reset}"
148158

149159
allGitFolders=( $(eval find -L . -name ".git" 2>/dev/null) )
150160

151161
for line in "${allGitFolders[@]}"; do
152162
local gitrepodir=${line::${#line}-5} # cut the .git part of find results to have the root git directory of that repository
153-
eval cd "\"$gitrepodir\"" # into git repo location
163+
cd "$gitrepodir" || exit 1 # into git repo location
154164
local curdir=$PWD
155165
local leadingpath=${curdir#"${actual}"}
156166
guardedExecution "$@"
157-
eval cd "\"$rwsdir\"" # back to origin location of last find command
167+
cd "$rwsdir" || exit 1 # back to origin location of last find command
158168
done
159169
done
160170
}

man/git-bulk.1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" generated with Ronn-NG/v0.9.1
22
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3-
.TH "GIT\-BULK" "1" "September 2024" "" "Git Extras"
3+
.TH "GIT\-BULK" "1" "February 2025" "" "Git Extras"
44
.SH "NAME"
55
\fBgit\-bulk\fR \- Run git commands on multiple repositories
66
.SH "SYNOPSIS"
@@ -64,10 +64,14 @@ git bulk \-\-listall
6464
List all registered repositories\.
6565
.SH "EXAMPLES"
6666
.nf
67-
Register a workspace so that git bulk knows about it:
67+
Register a workspace so that git bulk knows about it using an absolute path:
6868

6969
$ git bulk \-\-addworkspace personal ~/workspaces/personal
7070

71+
Or register a workspace using an environment variable pointing to an absolute path:
72+
73+
$ git bulk \-\-addworkspace personal '$PERSONAL_WORKSPACE'
74+
7175
Use option \-\-from in order to directly clone a repository or multiple repositories
7276

7377
$ git bulk \-\-addworkspace personal ~/workspaces/personal \-\-from https://github\.com/tj/git\-extras\.git
@@ -108,6 +112,10 @@ Remove all registered workspaces:
108112

109113
$ git bulk \-\-purge
110114
.fi
115+
.SH "FILES"
116+
.IP "\[ci]" 4
117+
\fB\.gitconfig\fR: Store the \fBgit\-bulk\fR registered workspaces under the \fBbulkworkspaces\fR key\.
118+
.IP "" 0
111119
.SH "AUTHOR"
112120
Written by Niklas Schlimm <\fIns103@hotmail\.de\fR>
113121
.SH "REPORTING BUGS"

man/git-bulk.html

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-bulk.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ git bulk adds convenient support for operations that you want to execute on mult
6060

6161
## EXAMPLES
6262

63-
Register a workspace so that git bulk knows about it:
63+
Register a workspace so that git bulk knows about it using an absolute path:
6464

6565
$ git bulk --addworkspace personal ~/workspaces/personal
6666

67+
Or register a workspace using an environment variable pointing to an absolute path:
68+
69+
$ git bulk --addworkspace personal '$PERSONAL_WORKSPACE'
70+
6771
Use option --from in order to directly clone a repository or multiple repositories
6872

6973
$ git bulk --addworkspace personal ~/workspaces/personal --from https://github.com/tj/git-extras.git
@@ -104,6 +108,10 @@ git bulk adds convenient support for operations that you want to execute on mult
104108

105109
$ git bulk --purge
106110

111+
## FILES
112+
113+
- `.gitconfig`: Store the `git-bulk` registered workspaces under the `bulkworkspaces` key.
114+
107115
## AUTHOR
108116

109117
Written by Niklas Schlimm &lt;<[email protected]>&gt;

man/index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ git-clear-soft(1) git-clear-soft
1212
git-clear(1) git-clear
1313
git-coauthor(1) git-coauthor
1414
git-commits-since(1) git-commits-since
15-
git-contrib(1) git-contrib
1615
git-continue(1) git-continue
16+
git-contrib(1) git-contrib
1717
git-count(1) git-count
1818
git-cp(1) git-cp
1919
git-create-branch(1) git-create-branch

0 commit comments

Comments
 (0)