Search code examples
pythondjangogitpython

Gitpython check repo cloned


I am working with Django Rest Framework, build some functionality interacts with git repository. I am using gitpython. Now, I used clone_from to clone remote repository.

repo = Repo.clone_from("REMOTE_REPOSITORY", "LOCAL_PATH")

But sometimes it fails with some network connection problems. How can I know repo is cloned correctly or not?


Solution

  • You can wrap your command in a try, except block

    try: repo = Repo.clone_from("REMOTE_REPOSITORY", "LOCAL_PATH") except git.exc.InvalidGitRepositoryError: ....

    Catching a sample exception above. A full list of exceptions is available at http://gitpython.readthedocs.io/en/stable/reference.html#module-git.exc