How to handle gitpython clone exceptions?
Asked Answered
S

1

7

I'm trying to write a batch clone script with GitPython, however I can't find a valid example of hanlding such as git url not exsits, download interupt etc.

How could I actually do this?

my exsiting code:

giturl = 'https://github.com/'+username+'/'+hwName+'.git'
targeturl = os.path.join(hwfolder,username+'-'+hwName)
try:
    repo = Repo.clone_from(giturl, targeturl, branch='master')
except:
    #git url not reachable
    #download interupt
    #target local path problem
Seleneselenious answered 14/3, 2016 at 2:4 Comment(3)
Gitpython has a list of custom exceptions hereHiram
But clearly the exceptions there cannot cover all the situations.Seleneselenious
GitPython appears to run the git binary for many operations, including clone. If so, clone failure will be represented as a generic command failure. (Interrupt is simply KeyboardInterrupt of course.)Helvetic
D
4

For starters,

exception git.exc.GitError

Base class for all package exceptions

Then, who said you have to handle all, or any exceptions? You can only reasonably handle those that you can do something intelligent about. The underlying git and the TCP stack are already smart enough to handle transient problems like an unreliable connection, so if it failed, you can't, as a rule, just try again and hope it happens to work this time.

For the purpose of a batch job, just propagate the error upstream so that your script fails gracefully. E.g. in a .bat file, you need to write something like <command> || exit 1 for the script to terminate upon error rather than continue blindly.


Now, of your 3 specific cases:

Distillery answered 17/7, 2017 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.