8

I want to write a program which will clone remote git repository and then do bunch of other stuff. The problem is that 'git clone' asks for password. It does not work when I open pipes to stdin/out/err to 'git clone' because it runs git-remote-http underneath which prompts for password on TTY.

I would like to pass the password from my program. I am using Python and Popen from subprocess. Code below does not wotk.

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

How can I achieve this?

Kylo
  • 2,300
  • 1
  • 19
  • 24

3 Answers3

2

If you do not want to use challenge authentication as the commenter said, I would use pexpect to automate this kind of interaction

TuringTux
  • 559
  • 1
  • 12
  • 26
ziu
  • 2,634
  • 2
  • 24
  • 39
  • This is exactly what I need. But there is one problem. How can I access return code of child process? Because if I do child.wait() it blocks forever because of unread output and if I do child.expect(pexpect.EOF) and then child.wait() I get exception that child process already exited. – Kylo Dec 01 '11 at 08:59
  • 1
    OK. I found it. I call child.close() after child.expect(pexpect.EOF). – Kylo Dec 01 '11 at 09:22
2

You can git clone https://foo:bar@my.git.repo/repo.git to get the data, and git remote set-url origin https://my.git.repo/repo.git afterwards to clear the password. But you have an race-condition between the start of the clone, and the URL-change.

Rudi
  • 19,366
  • 3
  • 55
  • 77
1

If you are fine with using a library, you could try using Dulwich.

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

This does not seem to store username or password (I've searched for the used password within the .git directory and did not find anything).

Sources

Because it was quite a hassle to find an up-to-date Git library without any obvious problems, I want to list my sources for future reference:

  1. Answer to "Python Git Module experiences? [closed]" by PTBNL from 2009 that recommends (among others) to use Dulwich
  2. Answer to "Use Git commands within Python code" by Trishayyyy from 2019 that still recommends Dulwich (and PyGit)
  3. Dulwich porcelain documentation about clone that sadly does not mention that authentication is possible at all.
  4. Answer to "dulwich - clone from remote repo authentication" by harvin that mentions authentication is indeed possible with Dulwich clone, using the username:password@ syntax.
  5. Inspired by the last linked answer, I inspected the source code a bit further and stumbled upon the username and password parameters. I've documented this in my answer to the "dulwich - clone from remote repo authentication" question.
TuringTux
  • 559
  • 1
  • 12
  • 26