Search code examples
pythongitpython

Preventing GitPython from asking for credentials when trying to clone a non-existent remote repo


In the program fragment below, I clone an existing repo from a remote location and it works correctly.

Then I try to clone a non-existent repo, and this time the call to git.Repo.clone_from() requests a name and a password from the keyboard.

Blocking while waiting for keyboard input is highly undesirable in my application, and so if the repo doesn't exist, I want the call to git.Repo.clone_from() to throw an exception instead.

Is there any way to cause that to happen, or to somehow detect whether or not there exists a git repo at an existing URL before I even try to clone it?

import git, shutil

DIRECTORY = '/tmp/clone'


def clone(url):
    print(url)
    shutil.rmtree(DIRECTORY, ignore_errors=True)
    git.Repo.clone_from(url=url, to_path=DIRECTORY, b='master')


clone('https://github.com/ManiacalLabs/BiblioPixelAnimations.git/')
clone('https://github.com/ManiacalLabs/NONEXISTENT.git/')

Solution

  • Putting an empty username and password should do the trick

    clone('https://:@github.com/ManiacalLabs/BiblioPixelAnimations.git/')
    clone('https://:@github.com/ManiacalLabs/NONEXISTENT.git/')
    

    Note there is :@ before github.com.