簡體   English   中英

重定向'git clone'的stdout/stderr/stdin

[英]Redirecting stdout/stderr/stdin of 'git clone'

我想編寫一個程序來克隆遠程 git 存儲庫,然后做一些其他的事情。 問題是“git clone”要求輸入密碼。 當我打開到 stdin/out/err 到“git clone”的管道時,它不起作用,因為它在下面運行 git-remote-http,提示輸入 TTY 上的密碼。

我想從我的程序中傳遞密碼。 我正在使用 Python 和子進程中的 Popen。 下面的代碼不起作用。

Popen(['git', 'clone', 'https://my.git.repo/repo.git'], shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE)

我怎樣才能做到這一點?

如果您不想像評論者所說的那樣使用挑戰認證,我會使用pexpect來自動化這種交互

您可以git clone https://foo:bar@my.git.repo/repo.git獲取數據,然后git remote set-url origin https://my.git.repo/repo.git清除密碼。 但是,在克隆開始和URL更改之間存在競爭條件。

如果您對使用圖書館感到滿意,您可以嘗試使用Dulwich

from dulwich import porcelain
porcelain.clone("https://example.com/repo.git", username="youruser", password="yourpassword")

這似乎沒有存儲用戶名或密碼(我在.git目錄中搜索了使用的密碼,但沒有找到任何東西)。

來源

因為要找到一個沒有任何明顯問題的最新 Git 庫非常麻煩,我想列出我的來源以供將來參考:

  1. 2009 年PTBNL 對“Python Git 模塊體驗?[關閉]”的回答,建議(除其他外)使用 Dulwich
  2. 從 2019 年起, Trishayyyy 對“在 Python 代碼中使用 Git 命令”的回答仍然推薦 Dulwich(和 PyGit)
  3. 可悲的是,關於cloneDulwich porcelain文檔根本沒有提到身份驗證是可能的。
  4. harvin 對“dulwich - clone from remote repo authentication”的回答提到了 Dulwich clone確實可以使用username:password@語法進行身份驗證。
  5. 受最后一個鏈接答案的啟發,我進一步檢查了源代碼,偶然發現了usernamepassword參數。 我已經在我對“dulwich - clone from remote repo authentication”問題的回答中記錄了這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM