3

Using Python and GitPython, with a list of git repositories from different users, I need to check if the repository exists and if it is public.

With that in mind and thinking of GitHub, if it asks for username and password, I know that the repository is out of my criterias. Therefore, I can ignore it.

import git

class User():
    def __init__(self, a, b, c):
        self.name = a
        self.git_user = b
        self.git_repos = c

user = User('John', 'john1234', 'fake_one')

try:
    git.Repo.clone_from(f'https://github.com/{user.git_user}/' + \
        f'{user.git_repos}.git', f'temp/{user.name}/')
except git.exc.GitError:
    print(f'ERROR! {user.name}: {user.git_user}/{user.git_repos} does not exist')

This code works, so far I enter with any username and respective password (including empty ones) when the repository is not found as a public one. Is there a way of either ignoring (by sending null strings) or catching (some exception?) the username/password stuff?

Note that if I could check for the existence of the (public) repository, I could use this information to prevent an inappropriate 'clone'...

iperetta
  • 607
  • 10
  • 19

1 Answers1

2

I've figure it out, thanks to Lacho Tomov answer in this question: Git push requires username and password

Code with desired behavior is:

import git

class User():
    def __init__(self, a, b, c):
        self.name = a
        self.git_user = b
        self.git_repos = c

user = User('John', 'john1234', 'fake_one')

try:
    git.Repo.clone_from(f'https://null:null@github.com/{user.git_user}/' + \
        f'{user.git_repos}.git', f'temp/{user.name}/')
except git.exc.GitError:
    print(f'ERROR! {user.name}: {user.git_user}/{user.git_repos} does not exist')

Note that null in https://null:null@github.com/{user.git_user}/ could be some other string (not empty, though).

If anybody has a more pythonic/proper way of doing this, please fell free to update this answer.

iperetta
  • 607
  • 10
  • 19