Skip to content

feat: support custom $GIT_DIR #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2023
Merged

Conversation

zhimsel
Copy link
Contributor

@zhimsel zhimsel commented Jun 10, 2023

About

While rarely used, it's possible to set the $GIT_DIR environment variable to instruct git to use a directory other than .git.

This checks if that environment variable is set; if it is, the plugin will watch that directory. If it's not set, it'll fall back to the default .git directory.

Why?

I use a non-standard .git folder in my home directory for my dotfiles (~/.git_dotfiles). If I have $GIT_DIR set in my env to work on my dotfiles and open up nvim-tree, it'll detect that we're "in a git repo".

Prior to this change, it would produce an error that it can't watch the .git directory (since it doesn't exist). With this change, it produces no error and correctly watches the $GIT_DIR directory.

Testing

I tested this in my environment by opening up nvim and nvim-tree in various scenarios, ensuring that no errors are produced and git changes are correctly detected (or correctly absent):

  • Not in a git repo and $GIT_DIR unset
  • In a git repo with a .git directory and $GIT_DIR unset
  • In a git repo with a non-standard git-dir and $GIT_DIR unset
  • In a git repo with a non-standard git-dir and $GIT_DIR set to that directory

@alex-courtis
Copy link
Member

We added such functionality in #2012 however that resulted in a regression and was backed out.

There's a PR open now to reintroduce that functionality #2194

This does seem a lot simpler: just using $GIT_DIR when set. In the previous PR we checked git -C /path/to/repo rev-parse --absolute-git-dir

I'm not sure how this works in test case 3; I'll test it out.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately there are also extra regular file system watchers created for the custom git directory.

Example with GIT_DIR=/home/alex/2263/nvt/.cgd

You can see two watchers for .cgd. The first one is the desired one, with the filtered set of files.

[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt' nil
[2023-06-12 15:35:52] [watcher] Event:new '/home/alex/2263/nvt'
[2023-06-12 15:35:52] [watcher] Event:start '/home/alex/2263/nvt'
[2023-06-12 15:35:52] [git] { "git", "-C", "/home/alex/2263/nvt", "rev-parse", "--show-toplevel" }
/home/alex/2263/nvt
[2023-06-12 15:35:52] [git] { "git", "-C", "/home/alex/2263/nvt", "config", "status.showUntrackedFiles" }
[2023-06-12 15:35:52] [git] running job with timeout 400ms
[2023-06-12 15:35:52] [git] git --no-optional-locks status --porcelain=v1 -z --ignored=matching -u
 M .editorconfig
?? .cgd/HEAD
?? .cgd/config
?? .cgd/description
?? .cgd/hooks/applypatch-msg.sample
?? .cgd/hooks/commit-msg.sample
?? .cgd/hooks/fsmonitor-watchman.sample
?? .cgd/hooks/post-update.sample
?? .cgd/hooks/pre-applypatch.sample
?? .cgd/hooks/pre-commit.sample
?? .cgd/hooks/pre-merge-commit.sample
?? .cgd/hooks/pre-push.sample
?? .cgd/hooks/pre-rebase.sample
?? .cgd/hooks/pre-receive.sample
?? .cgd/hooks/prepare-commit-msg.sample
?? .cgd/hooks/push-to-checkout.sample
?? .cgd/hooks/sendemail-validate.sample
?? .cgd/hooks/update.sample
?? .cgd/index
?? .cgd/info/exclude
?? .cgd/logs/HEAD
?? .cgd/logs/refs/heads/master
?? .cgd/logs/refs/remotes/origin/HEAD
?? .cgd/objects/pack/pack-86c3790f9768d4f5a4503823707e87ab8adfaea4.idx
?? .cgd/objects/pack/pack-86c3790f9768d4f5a4503823707e87ab8adfaea4.pack
?? .cgd/objects/pack/pack-86c3790f9768d4f5a4503823707e87ab8adfaea4.rev
?? .cgd/packed-refs
?? .cgd/refs/heads/master
?? .cgd/refs/remotes/origin/HEAD
!! .cgd/refs/tags/
[2023-06-12 15:35:52] [git] done
[2023-06-12 15:35:52] [git] job success    /home/alex/2263/nvt nil
[2023-06-12 15:35:52] [watcher] git start
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.cgd' { "FETCH_HEAD", "HEAD", "HEAD.lock", "config", "index" }
[2023-06-12 15:35:52] [watcher] Event:new '/home/alex/2263/nvt/.cgd'
[2023-06-12 15:35:52] [watcher] Event:start '/home/alex/2263/nvt/.cgd'
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.cgd' nil
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.github' nil
[2023-06-12 15:35:52] [watcher] Event:new '/home/alex/2263/nvt/.github'
[2023-06-12 15:35:52] [watcher] Event:start '/home/alex/2263/nvt/.github'
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.hooks' nil
[2023-06-12 15:35:52] [watcher] Event:new '/home/alex/2263/nvt/.hooks'
[2023-06-12 15:35:52] [watcher] Event:start '/home/alex/2263/nvt/.hooks'
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/doc' nil
[2023-06-12 15:35:52] [watcher] Event:new '/home/alex/2263/nvt/doc'
[2023-06-12 15:35:52] [watcher] Event:start '/home/alex/2263/nvt/doc'

Maybe take a look at #2194 and see if you can pull something from there.

@zhimsel zhimsel force-pushed the support-git-dir branch from d1ea271 to 93ab5fd Compare July 6, 2023 18:09
@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 6, 2023

^ rebase on latest master.

@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 6, 2023

Unfortunately there are also extra regular file system watchers created for the custom git directory.
You can see two watchers for .cgd. The first one is the desired one, with the filtered set of files.

@alex-courtis you're referring to these two lines?

[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.cgd' { "FETCH_HEAD", "HEAD", "HEAD.lock", "config", "index" }
[2023-06-12 15:35:52] [watcher] Watcher:new '/home/alex/2263/nvt/.cgd' nil

I'm guessing that second one is created just by the fact that it's a file in the current directory? I suppose we could have it ignore any files that are the value of $GIT_DIR if it's set...

@zhimsel zhimsel requested a review from alex-courtis July 6, 2023 19:02
@zhimsel zhimsel changed the title fix: Watch $GIT_DIR for git changes, if set fix: Watch $GIT_DIR for git changes, if set Jul 6, 2023
@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 6, 2023

@alex-courtis I modified the "is this a git dir?" function for the file ignores to account for $GIT_DIR being set, as well as accounting for possibly non-absolute $GIT_DIR values.

I tested it on my machine with various scenarios of $GIT_DIR being set and unset, being relative and absolute, and being in and out of git repos. Seems to work as expected with no duplicate watchers.

@zhimsel zhimsel force-pushed the support-git-dir branch from 978b052 to ae1a12f Compare July 6, 2023 19:10
@alex-courtis
Copy link
Member

Thanks for the updates @zhimsel

I'll get to review and test this weekend.

@alex-courtis
Copy link
Member

I don't have permission to rebase; I'll leave that to you. There look to be no conflicts.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for the following case:

:; export GIT_DIR=~/2263/cgd
:; export GIT_WORK_TREE=~/2263/nvt
:; cd ~/2263
:; mkdir nvt
:; cd nvt
:; echo bar > foo
:; git add foo
:; git commit -a -m "initial commit"

:; git rev-parse --show-toplevel
/home/alex/2263/nvt

:; git rev-parse --git-dir
/home/alex/2263/cgd

$GIT_WORK_TREE set or unset
$GIT_DIR set

My git knowledge is insufficient to test cloning an existing repo with non-standard --git-dir however it should behave in a similar manner.

  • Please rebase.

@alex-courtis alex-courtis changed the title fix: Watch $GIT_DIR for git changes, if set feat: support custom $GIT_DIR Jul 15, 2023
zhimsel and others added 3 commits July 15, 2023 09:54
While rarely used, it's possible to set the $GIT_DIR environment
variable to instruct git to use a directory other than `.git`.

This checks if that environment variable is set; if it is, the plugin
will watch that directory. If it's not set, it'll fall back to the
default `.git` directory.
This will ignore a path for watching if EITHER it's '.git', or the value
of $GIT_DIR (if it's set).

If $GIT_DIR is not set, the vim.env object returns `nil`, which will
never match `path`.
@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 15, 2023

Please rebase.

Done! Sidenote: I renamed the original commit to use feat: to match your reclassification of this PR.

My git knowledge is insufficient to test cloning an existing repo with non-standard --git-dir however it should behave in a similar manner.

It's as simple as just setting $GIT_DIR in your shell and cloning! Git will use that directory instead of .git. The --git-dir command argument should work as well, it just has to be set for every command against your cloned repo, including anything like shell prompts and vim plugins. You can find an example in the #Install section here.

@zhimsel zhimsel closed this Jul 15, 2023
@zhimsel zhimsel reopened this Jul 15, 2023
@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 15, 2023

Whoops, ignore that closure. I somehow managed to target the "close with comment" button with my keyboard.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for pushing this difficult one through.

@alex-courtis alex-courtis merged commit a6daf50 into nvim-tree:master Jul 16, 2023
@zhimsel
Copy link
Contributor Author

zhimsel commented Jul 16, 2023

No problem! Thanks for reviewing and merging.

@zhimsel zhimsel deleted the support-git-dir branch July 16, 2023 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants