libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.58k stars 382 forks source link

pygit2 does not respect a global setting of safe.directory='*' #1156

Open onew4y opened 1 year ago

onew4y commented 1 year ago

We are currently working with dvc for multiple projects and the dvc exp run leads to a pygit2 exception on a azure machine learning instance. This leads to problems since folders on mounted drives in azure belong to user root whilst the working user is azureuser The exception looks like following:

_pygit2.GitError: /mnt/batch/tasks/shared/LS_root/mounts/clusters/<XXX>/.git/: repository path '/mnt/batch/tasks/shared/LS_root/mounts/clusters/<XXX>' is not owned by current user

A workaround for this problem should be setting the git safe.directory to ='*'. But unfortunately this does not work with dvc resp. pygit2.

I narrowed the problem down and was able to reproduce it with pure pygit2. Since it seems to be a pygit2 problem, I am opening the issue directly here. FYI @efiop, @skshetry, @dmpetrov

My git config looks as follows:

user.email=<XXX>
user.name=<XXX>
safe.directory=*

With my normal git (git version 2.36.1) everything works great with the wildcard *. But pygit2 seems to ignore this config.

import pygit2
repo = pygit2.Repository(pygit2.discover_repository('.'))
GitError: /mnt/batch/tasks/shared/LS_root/mounts/clusters/<XXX>/.git: repository path '/mnt/batch/tasks/shared/LS_root/mounts/clusters/<XXX>/' is not owned by current user

But the config seems to be loaded:

import pygit2
config = pygit2.Config()
for x in config.get_global_config():
    print(x.name + ":" + x.value)
user.email:<XXX>
user.name:<XXX>
safe.directory:*

Used Versions: pygit2 1.10.0

Any help would be really appreciated, since we can't run experiments anymore.

meierale commented 1 year ago

Thanks to @onew4y there seems to be a workaround for dvc ... but it's more of a dirty hack than a way to proceed with this.

  1. add an import pygit2 statement to _init_.py of dvc in your python environment
  2. set the OWNER_VALIDATION option of pygit2 to 0 in the same _init_.py of dvc

Here's how it can be done when working with conda:

echo "import pygit2" >> /anaconda/envs/$(ENV_NAME)/lib/python3.8/site-packages/dvc/_init_.py
echo "pygit2.option(pygit2.GIT_OPT_SET_OWNER_VALIDATION, 0)" >> /anaconda/envs/$(ENV_NAME)/lib/python3.8/site-packages/dvc/_init_.py

It is unclear why the global safe.directory setting of git is not taken into account and has to be set individually as pygit2.option. We still believe this is a pygit2 issue, not a dvc issue. (FYI @efiop)

skshetry commented 1 year ago

Related: libgit2/libgit2#6391?

onew4y commented 1 year ago

@skshetry seems related to me yes. so therefore the problem lies in libgit2 and not even in pygit2?

meierale commented 1 year ago

@skshetry I just noticed that https://github.com/libgit2/libgit2/pull/6429 was merged in libgit2, which seems to be a duplicate of https://github.com/libgit2/libgit2/issues/6391

skshetry commented 1 year ago

@meierale @onew4y, could you please check if this works with pygit2==1.12.0? Looks like this was fixed in libgit2>=1.6.1 which is used in latest pygit2 release.

Magentaize commented 8 months ago

@skshetry I have tried pygit2==1.13.1 and confirmed this issue has gone.