python-3.xgitgitpython

Add safe directory with gitpython


How do I run this git command with gitpython(v2.1.*), please ?

git config --global --add safe.directory "some_dir"

Couldn't find in doc. I tried this:

repo = git.Repo.init(some_dir)
config = repo.config_writer()
config.set_value("user", "email", "mail@truc.com")
config.set_value("user", "name", "truc")
git_cmd = git.cmd.Git(some_dir)
git_cmd.config("--global", "--add", "safe.directory", some_dir)

But I get this error:

'error: could not lock config file /nonexistant/.gitconfig: No such file or directory'

I'm pretty sure this has something to do with user rights, because if I go to some_dir and directly execute the git command.

  1. as my user, it fails,
  2. as root, it works. But in my Python script config.set_value() commands are working fine for my user, so I guess there might be a way to add safe directory too.

Solution

  • Indeed an issue with user rights, I realised I did not need to do the update globally.

    Nevertheless, correct gitpython syntax was:

    config = repo.config_writer()
    config.set_value("user", "email", "mail@truc.com")
    config.set_value("user", "name", "truc")
    config.set_value("safe", "directory", "some_dir")