Skip to main content
 首页 » 编程设计

python之如何使用 gitPython 将 'pipe' 密码发送到 remote.update()

2025年05月04日6rubylouvre

我正在尝试编写一个脚本(可能是 python),它需要获取一个远程存储库(通过 git 与 Stash 一起 stash ),并检查一个特定的提交(基于哈希)。问题是这需要对用户“盲目”发生,但它会暂停输入密码。我需要找出一种方法将密码(或 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? 

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

请您参考如下方法:

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

生成 ssh key :

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

一旦你创建了你的 key ,将它们添加到中央机器或你的中央 git 软件,并更新你的获取 url 以使用 ssh 而不是 http 进行克隆