git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: `git worktree list` when bare repository is named `.git`
@ 2024-10-11  3:52 Caleb White
  2024-10-11  4:59 ` Rebecca Turner
  0 siblings, 1 reply; 8+ messages in thread
From: Caleb White @ 2024-10-11  3:52 UTC (permalink / raw)
  To: rbt; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 2487 bytes --]

> It seems that `.git` is always stripped from the `git worktree list` output (including in `--porcelain` mode). This becomes relevant with bare repositories. Here is a bare repository functioning as expected:
> 
> $ mkdir bare1
> $ cd bare1
> $ git init --bare
> Initialized empty Git repository in /private/tmp/bare1/
> $ git worktree list
> /private/tmp/bare1  (bare)
> 
> But if we create a bare repository in a directory named `.git`, `git worktree list` displays the parent directory as the worktree, even though `git status` doesn't recognize it as a worktree:
> 
> $ mkdir -p bare2/.git
> $ cd bare2/.git
> $ git init --bare
> Initialized empty Git repository in /private/tmp/bare2/.git/
> $ git worktree list
> /private/tmp/bare2  (bare)
> $ cd /tmp/bare2
> $ git status
> fatal: this operation must be run in a work tree
> 
> However, Git _will_ recognize the parent directory (/tmp/bare2) as a Git repository for the purposes of commands like `git rev-parse --git-dir`. I suspect this can be fixed in `get_main_worktree` by only stripping a `.git` suffix from the path if the main worktree is not bare.

The behavior you are seeing is correct and expected (and I use bare
repositories with worktrees like this).

The actual bare repository can be the directory, in a .git directory,
or in any other directory (as long as it has a .git file that points
to the actual bare repository). The directory will still be considered a
bare repository. For example:

    mkdir -p repo/.bare
    cd repo/.bare
    Initialized empty Git repository in /private/tmp/repo/.bare/
    git init --bare
    cd ..
    echo 'gitdir: .bare' >.git
    git worktree list
    git worktree add master
    /private/tmp/repo  (bare)
    /private/tmp/repo/master

Bare repositories by definition do not have a working tree, and therefore it is
expected that `git status` fails (can only be executed in a worktree) while
other commands succeed.

For the purposes of `git worktree list`, the `.git` suffix **should not** be
shown. You should never try to use `git worktree list` to get the actual path
of the repository, the docs give the following warning:

    See gitrepository-layout[5] for more information. The rule of thumb is do
    not make any assumption about whether a path belongs to $GIT_DIR or
    $GIT_COMMON_DIR when you need to directly access something inside $GIT_DIR.
    Use git rev-parse --git-path to get the final path.

Best,

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 509 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread
* `git worktree list` when bare repository is named `.git`
@ 2024-10-11  1:12 Rebecca Turner
  0 siblings, 0 replies; 8+ messages in thread
From: Rebecca Turner @ 2024-10-11  1:12 UTC (permalink / raw)
  To: git

It seems that `.git` is always stripped from the `git worktree list` output (including in `--porcelain` mode). This becomes relevant with bare repositories. Here is a bare repository functioning as expected:

$ mkdir bare1
$ cd bare1
$ git init --bare
Initialized empty Git repository in /private/tmp/bare1/
$ git worktree list
/private/tmp/bare1  (bare)

But if we create a bare repository in a directory named `.git`, `git worktree list` displays the parent directory as the worktree, even though `git status` doesn't recognize it as a worktree:

$ mkdir -p bare2/.git
$ cd bare2/.git
$ git init --bare
Initialized empty Git repository in /private/tmp/bare2/.git/
$ git worktree list
/private/tmp/bare2  (bare)
$ cd /tmp/bare2
$ git status
fatal: this operation must be run in a work tree

However, Git _will_ recognize the parent directory (/tmp/bare2) as a Git repository for the purposes of commands like `git rev-parse --git-dir`. I suspect this can be fixed in `get_main_worktree` by only stripping a `.git` suffix from the path if the main worktree is not bare.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-10-21 19:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-11  3:52 `git worktree list` when bare repository is named `.git` Caleb White
2024-10-11  4:59 ` Rebecca Turner
2024-10-11  5:36   ` Caleb White
2024-10-11 16:19     ` Rebecca Turner
2024-10-11 16:45       ` Caleb White
2024-10-11 22:17       ` Patrick Callahan
     [not found]       ` <CACt=GQry+mR7fVSUEdpPjsgSoDURk4W-DRPqLkom-f9Q0KBuTQ@mail.gmail.com>
2024-10-21 19:09         ` Rebecca Turner
  -- strict thread matches above, loose matches on Subject: below --
2024-10-11  1:12 Rebecca Turner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).