Skip to content

Commit a47f9e9

Browse files
committed
for-each-repo: work correctly in a worktree
When run in a worktree, the GIT_DIR directory is set in a different way than in a typical repository. Show this by updating t0068 to include a worktree and add a test that runs from that worktree. This requires moving the repo.key config into a global config instead of the base test repository's local config (demonstrating that it worked with non-worktree Git repositories). The fix is simple: unset the environment variable before looping over the repos. Reported-by: Matthew Gabeler-Lee <fastcat@gmail.com> Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 86cd83f commit a47f9e9

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

builtin/for-each-repo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "builtin.h"
22
#include "config.h"
3+
#include "environment.h"
34
#include "gettext.h"
45
#include "parse-options.h"
56
#include "path.h"
@@ -60,6 +61,9 @@ int cmd_for_each_repo(int argc,
6061
else if (err)
6162
return 0;
6263

64+
/* Be sure to not pass GIT_DIR to children. */
65+
unsetenv(GIT_DIR_ENVIRONMENT);
66+
6367
for (size_t i = 0; i < values->nr; i++) {
6468
int ret = run_command_on_repo(values->items[i].string, argc, argv);
6569
if (ret) {

t/t0068-for-each-repo.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ test_description='git for-each-repo builtin'
77
test_expect_success 'run based on configured value' '
88
git init one &&
99
git init two &&
10-
git init three &&
10+
git -C two worktree add --orphan ../three &&
1111
git init ~/four &&
1212
git -C two commit --allow-empty -m "DID NOT RUN" &&
13-
git config run.key "$TRASH_DIRECTORY/one" &&
14-
git config --add run.key "$TRASH_DIRECTORY/three" &&
15-
git config --add run.key "~/four" &&
13+
git config --global run.key "$TRASH_DIRECTORY/one" &&
14+
git config --global --add run.key "$TRASH_DIRECTORY/three" &&
15+
git config --global --add run.key "~/four" &&
16+
1617
git for-each-repo --config=run.key commit --allow-empty -m "ran" &&
1718
git -C one log -1 --pretty=format:%s >message &&
1819
grep ran message &&
@@ -22,6 +23,7 @@ test_expect_success 'run based on configured value' '
2223
grep ran message &&
2324
git -C ~/four log -1 --pretty=format:%s >message &&
2425
grep ran message &&
26+
2527
git for-each-repo --config=run.key -- commit --allow-empty -m "ran again" &&
2628
git -C one log -1 --pretty=format:%s >message &&
2729
grep again message &&
@@ -30,7 +32,17 @@ test_expect_success 'run based on configured value' '
3032
git -C three log -1 --pretty=format:%s >message &&
3133
grep again message &&
3234
git -C ~/four log -1 --pretty=format:%s >message &&
33-
grep again message
35+
grep again message &&
36+
37+
git -C three for-each-repo --config=run.key -- commit --allow-empty -m "ran from worktree" &&
38+
git -C one log -1 --pretty=format:%s >message &&
39+
grep worktree message &&
40+
git -C two log -1 --pretty=format:%s >message &&
41+
! grep worktree message &&
42+
git -C three log -1 --pretty=format:%s >message &&
43+
grep worktree message &&
44+
git -C ~/four log -1 --pretty=format:%s >message &&
45+
grep worktree message
3446
'
3547

3648
test_expect_success 'do nothing on empty config' '

0 commit comments

Comments
 (0)