git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Philippe Blain <levraiphilippeblain@gmail.com>
To: Git mailing list <git@vger.kernel.org>
Cc: Eric Sunshine <sunshine@sunshineco.com>,
	Elijah Newren <newren@gmail.com>
Subject: [BUG?] Weird interaction between `git -C`, aliases and worktrees
Date: Wed, 14 Oct 2020 11:27:27 -0400	[thread overview]
Message-ID: <020EB674-6A95-4E53-B2E7-F4EEBD6324C3@gmail.com> (raw)

Hello all,

Up to recently I had an alias 'st' for `git status`, i.e. `git config alias.st status`. 
This works well in the main working tree as well as secondary worktrees (`git worktree add ...`),
and also shows paths relative to the current directory if I'm not at the root of the repo, 
just like `git status`.

Now I wanted to change my alias so that it ran `git status` and then an additional command, i.e.
`git config alias.st '!git status && date'` (for demonstration purposes).
This works correctly in the main worktree and secondary worktrees, but since aliases
run from the root of the repo, the paths in the output of 'git st' are not relative
to the current directory.

So my next attempt was `git config alias.st '!git -C "${GIT_PREFIX}" status && date'`,
so that paths would be relative in the output. However, this works in the main worktree
but fails badly in the secondary worktree:

```
rm -rf test &&
rm -rf test2 &&
git init test &&  
cd test &&
mkdir folder &&
date>>folder/file &&
git add folder/file &&
git ci -m "commit" &&
git config alias.st '!git -C "${GIT_PREFIX}" status && date' &&
date >>folder/file &&
echo '=== `git st` in main worktree at root ===' &&
git st &&
cd folder &&  
echo '=== `git st` in main worktree in folder ===' &&
git st &&
git worktree add ../test2 &&
cd ../test2 &&
date >>folder/file &&
echo '=== `git st` in secondary worktree at root ===' &&
git st &&  
cd folder &&
echo '=== `git st` in secondary worktree in folder ===' &&
git st
```

The last commands ouputs: 

```
=== `git st` in secondary worktree in folder ===
On branch test2
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    folder/file

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file

no changes added to commit (use "git add" and/or "git commit -a")
```

So something is wrong in the automatic worktree path detection...
If I replace the alias with 

    git config alias.st '!git -C "${GIT_PREFIX}" --work-tree=$PWD status && date' 

then it works correctly, but I have a feeling this should not be necessary...

I've CC-ed Eric (because of his work on `git worktree`) and Elijah (because
I remember he worked on `dir.c` so maybe this is related to that code, but I'm
not sure).

Cheers,
Philippe


             reply	other threads:[~2020-10-14 15:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-14 15:27 Philippe Blain [this message]
2020-10-14 22:02 ` [BUG?] Weird interaction between `git -C`, aliases and worktrees Elijah Newren
2020-10-14 22:35   ` Philippe Blain
2020-10-16  0:23     ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=020EB674-6A95-4E53-B2E7-F4EEBD6324C3@gmail.com \
    --to=levraiphilippeblain@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=newren@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).