xwmx / nb

CLI and local web plain text note‑taking, bookmarking, and archiving with linking, tagging, filtering, search, Git versioning & syncing, Pandoc conversion, + more, in a single portable script.
https://xwmx.github.io/nb
GNU Affero General Public License v3.0
6.67k stars 188 forks source link

Conditional Git config is not used #198

Open Weltraumschaf opened 2 years ago

Weltraumschaf commented 2 years ago

I have a conditional config for my git identities in my dotfiles:

[includeIf "gitdir:~/nb/"]
  path = ~/.gitconfig_private

When I start nb it asks me for a name and email and adds them as global Git options:

[user]
    name = Sven
    email = foo@bar.com

Since I assume that the git repository nb operates is in ~/nb/ on macOS I expect that nb uses the identity option set by the includeIf in my Git config.

xwmx commented 2 years ago

Thanks for the info. I’ll have to investigate this more. I’m still trying to get includeIf to work consistently in tests. The default directory for nb is ~/.nb (with a leading .).

Weltraumschaf commented 2 years ago

The default directory for nb is ~/.nb (with a leading .)

Sorry, that's a typo. I did the test with right path and introduced the typo for the write up here. I double checked now: Also with the right path ~/.nb it does not work.

felixdv commented 1 year ago

I have the same issue but with a slightly different config, where I have a ~/.gitconfig with the following:

[includeIf "hasconfig:remote.*.url:git@codeberg.org:*/**"]
        path = ~/.gitconfig-codeberg

This works fine for any of my other repositories that match the remote but nb seems to ignore this config. If I go to ~/.nb/home/ and then run git config -l | grep user, it shows the correct user settings.

It would be good to see this work, as I'm working with repositories across github/sourcehut/codeberg and nb forces me to have a global [user] config every time it syncs.

ahmedelgabri commented 2 days ago

I have the same issue but I store my git config inside $XDG_CONFIG_HOME/git/config which is already supported by git https://git-scm.com/docs/git-config#Documentation/git-config.txt---global

git config --list user.name and git config --list user.name work as expected but nb will write to ~/.gitconfig

xwmx commented 1 day ago

@ahmedelgabri Does nb recognize your email and username when there is no ~/.gitconfig file, or does it prompt you for it?

This git configuration seems to be contained in the _git_required() function. This function reads the values with git config --global --includes user.name and git config --global --includes user.email. If they are missing, the user is prompted for the values which are then set with git config --global user.name "${__name}" and git config --global user.email "${__email}". According to the documentation for git config --global, it writes "to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t."

So far I only see nb updating the git config with these particular commands. Let me know if you have any additional information that could help me identify the issue.

ahmedelgabri commented 1 day ago

@xwmx if it would recognize them, why would it prompt me to set them? And why would it set it in ~/.gitconfig?

xwmx commented 1 day ago

@ahmedelgabri If the name and email are configured in $XDG_CONFIG_HOME/git/config and ~/.gitconfig is not present, then nb should not prompt you for them or write them in ~/.gitconfig since the commands use git config --global which reads from and writes "to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t." I just checked manually on a VM and confirmed that the git config --global commands in nb read from and write to $XDG_CONFIG_HOME/git/config without creating ~/.gitconfig as long as $XDG_CONFIG_HOME/git/config is present.

If a pre-existing ~/.gitconfig file is already present, then git config --global gives ~/.gitconfig precedence and will read from and write to ~/.gitconfig instead of $XDG_CONFIG_HOME/git/config. This is probably what you are encountering.

The only case that I can see where git config --global creates a ~/.gitconfig is if $XDG_CONFIG_HOME/git/config is not present.

If you have both a $XDG_CONFIG_HOME/git/config file and a ~/.gitconfig file, then I recommend moving all of the configuration settings from ~/.gitconfig to $XDG_CONFIG_HOME/git/config and deleting the ~/.gitconfig file.

ahmedelgabri commented 1 day ago

@xwmx you are totally right, found out a ~/.gitconfig file that was created by another tool and that's why it didn't work as expected. Removed the file and everything works as expected now.

xwmx commented 16 hours ago

@ahmedelgabri Awesome. Glad it's working.

xwmx commented 16 hours ago

@felixdv I'm just catching up to this now. I added a test covering IncludeIf with hasconfig:remote and it appears to be working as intended at this time. If you are still encountering this issue, please let me know!