RichiH / vcsh

config manager based on Git
GNU General Public License v2.0
2.2k stars 124 forks source link

write-gitignore confusion when using git-add #346

Open madduck opened 4 months ago

madduck commented 4 months ago

The gitignore handling by vcsh is confusing. It does seem to work as intended, but there are confusing error messages:

lotus:~% vcsh init bugdemo
Initialized empty Git repository in /home/madduck/.config/vcsh/repo.d/bugdemo.git/
vcsh: info: '/home/madduck/.gitignore.d/bugdemo' differs from new data, moving it to '/home/madduck/.gitignore.d/bugdemo.bak'
The following paths are ignored by one of your .gitignore files:
.gitignore.d
hint: Use -f if you really want to add them.
hint: Disable this message with "git config advice.addIgnoredFile false"
[main (root-commit) ca6eacc] seed gitignore file
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore.d/bugdemo

That message about having to use -f is confusing, especially since I haven't called git-add. Update: except I did… there's a hook that does it. So my fault. But the issue still stands.

Let's move on:

lotus:~% vcsh enter bugdemo

+lotus:vcsh:bugdemo|main|~% mkdir -p sub/dir

+lotus:vcsh:bugdemo|main|~% date > sub/dir/date

+lotus:vcsh:bugdemo|main|~% git add sub/dir/date
The following paths are ignored by one of your .gitignore files:
sub
hint: Use -f if you really want to add them.
hint: Disable this message with "git config advice.addIgnoredFile false"

Okay, that makes perfect sense. It's a new file. Moving right along…

+lotus:vcsh:bugdemo|main|~% git add -f sub/dir/date

+lotus:vcsh:bugdemo|main|~% vcsh write-gitignore bugdemo
vcsh: info: '/home/madduck/.gitignore.d/bugdemo' differs from new data, moving it to '/home/madduck/.gitignore.d/bugdemo.bak'

+lotus:vcsh:bugdemo|main|~% git add .gitignore.d/bugdemo
The following paths are ignored by one of your .gitignore files:
.gitignore.d
hint: Use -f if you really want to add them.
hint: Disable this message with "git config advice.addIgnoredFile false"

Yeah well, .gitignore.d is ignored, but I am not trying to do anything with that directory. The file I am actually adding is excluded from gitignore, and it is also properly staged:

+lotus:vcsh:bugdemo|main|~% git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   .gitignore.d/bugdemo
        new file:   sub/dir/date

The same happens with any other file:

+lotus:vcsh:bugdemo|main|~% git commit -am.
[main 35cec3a] .
 2 files changed, 2 insertions(+)
 create mode 100644 sub/dir/date

+lotus:vcsh:bugdemo|main|~% cat .gitignore.d/bugdemo
*
!/.gitignore.d/bugdemo
!/sub/dir/date

+lotus:vcsh:bugdemo|main|~% date >| sub/dir/date

+lotus:vcsh:bugdemo|main|~% git add sub/dir/date
The following paths are ignored by one of your .gitignore files:
sub
hint: Use -f if you really want to add them.
hint: Disable this message with "git config advice.addIgnoredFile false"

+lotus:vcsh:bugdemo|main|~% git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   sub/dir/date

The way to fix this is IMHO by adding exclusions for all parent directories, i.e. the .gitignore.d/bugdemo file should be:

*
!/.gitignore.d
!/.gitignore.d/bugdemo
!/sub
!/sub/dir
!/sub/dir/date

I believe this was the case in the past, and has changed. FTR, setting VCSH_GITIGNORE=recursive has no effect on the above.

alerque commented 4 months ago

Thanks for the detailed report. Without actually having dived into the nitty gritty yet, this seems like it is probably a regression related to #196/#334. Somewhene in between #311 also touched the ignore code.