简体   繁体   中英

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/')

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM