简体   繁体   English

在 Ubuntu Docker 镜像中的 Tox 下运行 GitPython 的 Python PyTest:提交失败,提交者/作者身份未知

[英]Python PyTest running GitPython under Tox in Ubuntu Docker image: commit fails with Committer/Author identity unknown

Q+A (I'm posting this in case someone else goes down this niche rabbit hole - as it took me a while to figure this out).问答(我发布这个是为了防止其他人掉进这个小众兔子洞——因为我花了一段时间才弄明白)。

Scenario: You've decided to run a unit-test which 'plays' with creating a local git repo using GitPython.场景:您已决定运行一个单元测试,该单元测试“播放”使用 GitPython 创建本地 git 存储库。 Your tests run in Tox in a GitHub action which runs an Ubuntu docker image - which has correct.gitconfig file with user.name and user.email.您的测试在运行 Ubuntu docker 映像的 GitHub 操作中的 Tox 中运行 - 它具有包含 user.name 和 user.email 的 correct.gitconfig 文件。 You can even see this correct config if you use GitPython's GitConfigParser or just read the file at git.config.get_config_path.如果您使用 GitPython 的 GitConfigParser 或者只是阅读 git.config.get_config_path 中的文件,您甚至可以看到这个正确的配置。
With all the above looking good, you run your tests, which contain something like:在以上所有看起来不错的情况下,您运行测试,其中包含如下内容:

with open(os.path.join(remote_repo.working_dir, 'readme.md'), 'w') as file:
    file.write('# Remote\n')
remote_repo.git.add(A=True)
remote_repo.git.commit(m=commit_text)

in GH and get failures with something like:在 GH 中失败,例如:

          raise GitCommandError(redacted_command, status, stderr_value, stdout_value)
  E           git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  E             cmdline: git commit -m Initial commit
  E             stderr: 'Author identity unknown
  E           
  E           *** Please tell me who you are.
  E           
  E           Run
  E           
  E             git config --global user.email "you@example.com"
  E             git config --global user.name "Your Name"
  E           
  E           to set your account's default identity.
  E           Omit --global to set the identity only in this repository.
  E           
  E           fatal: unable to auto-detect email address (got '**********.(none)')'

EDIT Answer moved the answers below.编辑答案移动了下面的答案。

I tried so many things... but the last desperate attempt of calling remote_repo.git.config('--global', 'user.name', 'some user') failed with the message that '$HOME' wasn't set - which finally led me to the...我尝试了很多东西......但是最后一次调用 remote_repo.git.config('--global', 'user.name', 'some user') 的绝望尝试失败了,消息是 '$HOME' 不是设置 - 这最终把我带到了......

Solution: Add the following to tox.ini's testenv section [edited after comment] :解决方案:将以下内容添加到 tox.ini 的 testenv 部分[在评论后编辑]

[testenv]
  passenv = HOME
  ...

Explanation: Tox doesn't set the HOME environment variable, and it appears GitPython's repo.git.commit command only reads the config if HOME is set.说明: Tox 没有设置 HOME 环境变量,而且如果设置了 HOME,GitPython 的 repo.git.commit 命令似乎只会读取配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM