Skip to content

Commit 0efbb7d

Browse files
committed
Merge branch 'jn/setup-fixes'
* jn/setup-fixes: t1510: fix typo in the comment of a test Documentation updates for 'GIT_WORK_TREE without GIT_DIR' historical usecase Subject: setup: officially support --work-tree without --git-dir tests: compress the setup tests tests: cosmetic improvements to the repo-setup test t/README: hint about using $(pwd) rather than $PWD in tests Fix expected values of setup tests on Windows
2 parents 8c0db6f + 6abbee8 commit 0efbb7d

File tree

6 files changed

+749
-4476
lines changed

6 files changed

+749
-4476
lines changed

Documentation/config.txt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,26 @@ false), while all other repositories are assumed to be bare (bare
317317
= true).
318318

319319
core.worktree::
320-
Set the path to the working tree. The value will not be
321-
used in combination with repositories found automatically in
322-
a .git directory (i.e. $GIT_DIR is not set).
320+
Set the path to the root of the working tree.
323321
This can be overridden by the GIT_WORK_TREE environment
324-
variable and the '--work-tree' command line option. It can be
325-
an absolute path or relative path to the directory specified by
326-
--git-dir or GIT_DIR.
327-
Note: If --git-dir or GIT_DIR are specified but none of
322+
variable and the '--work-tree' command line option.
323+
The value can an absolute path or relative to the path to
324+
the .git directory, which is either specified by --git-dir
325+
or GIT_DIR, or automatically discovered.
326+
If --git-dir or GIT_DIR is specified but none of
328327
--work-tree, GIT_WORK_TREE and core.worktree is specified,
329-
the current working directory is regarded as the top directory
328+
the current working directory is regarded as the top level
330329
of your working tree.
330+
+
331+
Note that this variable is honored even when set in a configuration
332+
file in a ".git" subdirectory of a directory and its value differs
333+
from the latter directory (e.g. "/path/to/.git/config" has
334+
core.worktree set to "/different/path"), which is most likely a
335+
misconfiguration. Running git commands in the "/path/to" directory will
336+
still use "/different/path" as the root of the work tree and can cause
337+
confusion unless you know what you are doing (e.g. you are creating a
338+
read-only snapshot of the same index to a location different from the
339+
repository's usual working tree).
331340

332341
core.logAllRefUpdates::
333342
Enable the reflog. Updates to a ref <ref> is logged to the file

Documentation/git.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,12 @@ help ...`.
291291
path or relative path to current working directory.
292292

293293
--work-tree=<path>::
294-
Set the path to the working tree. The value will not be
295-
used in combination with repositories found automatically in
296-
a .git directory (i.e. $GIT_DIR is not set).
294+
Set the path to the working tree. It can be an absolute path
295+
or a path relative to the current working directory.
297296
This can also be controlled by setting the GIT_WORK_TREE
298297
environment variable and the core.worktree configuration
299-
variable. It can be an absolute path or relative path to
300-
current working directory.
301-
Note: If --git-dir or GIT_DIR are specified but none of
302-
--work-tree, GIT_WORK_TREE and core.worktree is specified,
303-
the current working directory is regarded as the top directory
304-
of your working tree.
298+
variable (see core.worktree in linkgit:git-config[1] for a
299+
more detailed discussion).
305300

306301
--bare::
307302
Treat the repository as a bare repository. If GIT_DIR

setup.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ static const char *setup_discovered_git_dir(const char *gitdir,
411411
if (check_repository_format_gently(gitdir, nongit_ok))
412412
return NULL;
413413

414+
/* --work-tree is set without --git-dir; use discovered one */
415+
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
416+
if (offset != len && !is_absolute_path(gitdir))
417+
gitdir = xstrdup(make_absolute_path(gitdir));
418+
if (chdir(cwd))
419+
die_errno("Could not come back to cwd");
420+
return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
421+
}
422+
414423
/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
415424
if (is_bare_repository_cfg > 0) {
416425
set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir));
@@ -443,6 +452,16 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
443452
if (check_repository_format_gently(".", nongit_ok))
444453
return NULL;
445454

455+
/* --work-tree is set without --git-dir; use discovered one */
456+
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
457+
const char *gitdir;
458+
459+
gitdir = offset == len ? "." : xmemdupz(cwd, offset);
460+
if (chdir(cwd))
461+
die_errno("Could not come back to cwd");
462+
return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
463+
}
464+
446465
inside_git_dir = 1;
447466
inside_work_tree = 0;
448467
if (offset != len) {

t/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ Do:
283283
Tests that are likely to smoke out future regressions are better
284284
than tests that just inflate the coverage metrics.
285285

286+
- When a test checks for an absolute path that a git command generated,
287+
construct the expected value using $(pwd) rather than $PWD,
288+
$TEST_DIRECTORY, or $TRASH_DIRECTORY. It makes a difference on
289+
Windows, where the shell (MSYS bash) mangles absolute path names.
290+
For details, see the commit message of 4114156ae9.
291+
286292
Don't:
287293

288294
- exit() within a <script> part.

t/t1501-worktree.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
343343
test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
344344
GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
345345
test-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
346-
echo "$TRASH_DIRECTORY/repo.git/work" >expected &&
346+
echo "$(pwd)/repo.git/work" >expected &&
347347
test_cmp expected actual
348348
'
349349

0 commit comments

Comments
 (0)