如何处理 gitpython 克隆异常?
How to handle gitpython clone exceptions?
我正在尝试使用 GitPython 编写批量克隆脚本,但是我找不到有效的处理示例,例如 git url not exsits、download interupt 等
我怎么会这样呢?
我现有的代码:
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
exception git.exc.GitError
Base class for all package exceptions
那么,谁说你有处理所有的,或者任何异常?你只能合理地处理那些你可以做一些聪明的事情。底层 git
和 TCP 堆栈已经足够聪明,可以处理诸如不可靠连接之类的瞬态问题,因此如果它失败了,您通常不能再试一次,希望这次能正常工作。
为了批处理作业的目的,只需向上游传播错误,以便您的脚本正常失败。例如。在 .bat
文件中,您需要编写类似 <command> || exit 1
的内容,以便脚本在出错时终止,而不是盲目地继续。
现在,在您的 3 个具体案例中:
- 项目 1 和 2 可能会使基础
git
失败,all of those cases yield GitCommandError
NoSuchPathError
is only ever raised 在 Repo
初始化时如果本地存储库的路径不存在
我正在尝试使用 GitPython 编写批量克隆脚本,但是我找不到有效的处理示例,例如 git url not exsits、download interupt 等
我怎么会这样呢?
我现有的代码:
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
exception git.exc.GitError
Base class for all package exceptions
那么,谁说你有处理所有的,或者任何异常?你只能合理地处理那些你可以做一些聪明的事情。底层 git
和 TCP 堆栈已经足够聪明,可以处理诸如不可靠连接之类的瞬态问题,因此如果它失败了,您通常不能再试一次,希望这次能正常工作。
为了批处理作业的目的,只需向上游传播错误,以便您的脚本正常失败。例如。在 .bat
文件中,您需要编写类似 <command> || exit 1
的内容,以便脚本在出错时终止,而不是盲目地继续。
现在,在您的 3 个具体案例中:
- 项目 1 和 2 可能会使基础
git
失败,all of those cases yieldGitCommandError
NoSuchPathError
is only ever raised 在Repo
初始化时如果本地存储库的路径不存在