Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle gitpython clone exceptions?

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
like image 694
tomriddle_1234 Avatar asked Oct 18 '22 13:10

tomriddle_1234


1 Answers

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:

  • items 1 and 2 will probably make an underlying git fail, all of those cases yield GitCommandError
  • NoSuchPathError is only ever raised on Repo initialization if the path to the local repo doesn't exist
like image 147
ivan_pozdeev Avatar answered Nov 01 '22 11:11

ivan_pozdeev