如何使用 gitPython 'pipe' 密码到 remote.update()

How to 'pipe' password to remote.update() with gitPython

我正在尝试编写一个脚本(可能 python),它需要获取一个远程仓库(通过 git 与 Stash 一起存放),并检查一个特定的提交(基于哈希) .问题是这需要对用户发生 'blindly',但它会暂停输入密码。我需要找出一种方法将密码(或 proc.communicate()(或其他东西)通过管道传输到 repo.fetch()origin.update() 过程。

目前,我有这样的代码:

remoteUrl = 'http://uname@build:7990'
commitId = '9a5af460615'
pw = 'MyPassword'
repo = git.Repo('C:\Myfolder\MyRepo')
proc = subprocess.Popen('echo pw | repo.git.fetch()', shell=True, stdin = PIPE)

不过好像不行 如果我尝试了 echo/pipe,但是在 repo.git.fetch() 后面加上 proc.communicate(pw),我得到错误:

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Program Files (x86)\Python34\lib\subprocess.py", line 941, in communicate
    self.stdin.write(input)
TypeError: 'str' does not support the buffer interface

最后,我也尝试添加:

o = repo.remotes.origin
proc = subprocess.Popen('o.update()', shell=True, stdin = PIPE)
proc.communicate(pw)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Program Files (x86)\Python34\lib\subprocess.py", line 941, in communicate
    self.stdin.write(input)
TypeError: 'str' does not support the buffer interface

但是没有用,从错误中可以看出。

我想我把这个复杂化了,因为看起来 gitpython 可能有一个很好的方法可以将 pw 发送到 o.update()repo.git.fetch() 而无需使用 subprocess?

编辑: 我希望按照这些思路编写代码:

remoteUrl = 'http://uname@build:7990'
commitId = '9a5af460615'
pw = 'MyPassword'
repo = git.Repo('C:\Myfolder\MyRepo')
repo.fetch(remoteUrl)
repo.checkout(commitId) # or maybe repo.pull, or something?

这是比其他任何东西都更多的伪代码,但它可以帮助您了解我所希望的。另外,我想强制通过任何 'hiccups' 我不想要分离的头部警告或任何东西,我想在指定的提交时用远程完全替换本地工作副本。

使用 ssh 代替 http。
一旦您设置了您的 ssh 密钥,身份验证将使用 ssh 密钥“静默”完成,并且无需再次输入密码。

正在生成 ssh 密钥:

ssh-keygen -t rsa -C 'email@address'

创建密钥后,将它们添加到中央计算机或中央 git 软件并更新您的提取 url 以使用 ssh 而不是 http

进行克隆