在 GitPython 中使用 GIT_SSH_COMMAND

问题描述 投票:0回答:1

我正在使用 GitPython 初始化一个新的本地存储库,创建初始提交并推送到规范存储库。不幸的是,最后一步失败了,我很难理解为什么。我确定我只是错误地使用了

GIT_SSH_COMMAND
变量,但我不确定如何使用。没有太多的例子可以继续。

我已经阅读了这个SO问题并深入研究了相关的issuecommit,但我显然没有设法将它们正确地组合在一起。

Git v2.3+的“证明”

$ git --version                                                                                                                          
git version 2.3.1

脚本片段

# I've init'd the repo and any variables
# have been defined and initialized.
git_ssh_identity_file = os.path.expanduser('~/.ssh/id_rsa')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
with git_project.git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
    git_project.remotes.origin.push(git_project.heads.master)

结果错误

Traceback (most recent call last):
  File "./ct-new-project.py", line 204, in <module>
    git_project.remotes.origin.push(git_project.heads.master)
  File "/Library/Python/2.7/site-packages/git/remote.py", line 667, in push
    return self._get_push_info(proc, progress or RemoteProgress())
  File "/Library/Python/2.7/site-packages/git/remote.py", line 588, in _get_push_info
    handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
  File "/Library/Python/2.7/site-packages/git/cmd.py", line 202, in handle_process_output
    return finalizer(process)
  File "/Library/Python/2.7/site-packages/git/util.py", line 158, in finalize_process
    proc.wait()
  File "/Library/Python/2.7/site-packages/git/cmd.py", line 300, in wait
    raise GitCommandError(self.args, status, self.proc.stderr.read())
git.exc.GitCommandError: 'git push --porcelain origin master' returned with exit code 128
python gitpython
1个回答
1
投票

当 GitPython 抛出此类错误时,总是值得检查它试图从命令行执行的实际命令是否有效。您的本地克隆中可能发生了一些变化,导致命令无法成功完成。

你试图用 GitPython 实现的等价物可以通过以下方式完成:

$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa' git push --porcelain origin master

至少在 Linux 上。在 Windows 上,您可能需要直接在单独的命令中设置环境变量。

像这样进行实验时,我发现拥有一个“本地”上游很有用,我可以将其推送到硬盘驱动器上的某个位置,以便我可以将其丢弃并重新启动 - 或者

git push --force
来自上游的第二个(原始)克隆..因为你只是知道你至少会把它搞砸一次。

© www.soinside.com 2019 - 2024. All rights reserved.