2

我正在尝试编写一个脚本(可能是 python),该脚本需要获取远程仓库(通过 git 与 Stash 一起存放),并签出特定的提交(基于哈希)。问题是这需要对用户“盲目地”发生,但它会暂停输入密码。我需要想办法将(或proc.communicate()(或)某些东西)密码传送到repo.fetch()or origin.update()proc。

目前,我有这样的代码:

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)

但如果我尝试了回声/管道,它似乎不起作用,但跟着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 可能有一个很好的方法来发送密码到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?

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

4

1 回答 1

1

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

生成 ssh 密钥:

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

创建密钥后,将它们添加到中央机器或中央 git 软件并更新获取 url 以使用 ssh 而不是 http 进行克隆

于 2015-05-26T21:06:11.227 回答