Python git模块无效的git存储库错误 [英] Python git module Invalid git repository error

查看:603
本文介绍了Python git模块无效的git存储库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个放置在文件夹结构中的脚本,如下所示:

I have a script that is placed in a folder structure as such:

~/wofc/folder1/folder2/script.py

script.py使用git模块执行一些任务.但是,当我从folder2外部运行脚本时,即当我将cd放入folder1时,我运行python folder2/script.py arg1 arg2我会收到raise InvalidGitRepositoryError(epath)错误.当我从folder2内部(即cdfolder2)运行python script.py arg1 arg2时,脚本运行良好.以下是相关的代码段.您能告诉我是什么问题吗?

script.py uses the git module to go some tasks. However when I run the script from outside of folder2 i.e. when I have cd into folder1 i run python folder2/script.py arg1 arg2 I get the raise InvalidGitRepositoryError(epath) error. The script runs fine when I run it from inside folder2 i.e. cd into folder2 and run python script.py arg1 arg2. Below is the relevant code snippet. Can You please let me know what is the issue?

    git = Repo('{}/..'.format(os.getcwd())).git
    git.checkout('master')
    git.pull()

推荐答案

问题是您使用os.getcwd()返回当前工作目录.如果您站在folder2外面,则此函数将返回~/wofc/folder1.

The problem is that you use os.getcwd() which returns the current working directory. If you stand just outside folder2 this function will return ~/wofc/folder1.

您应该将其替换为类似的内容:

You should swap it for something like:

import os
os.path.dirname(os.path.abspath(__file__))

例如这样的

import os
path = os.path.dirname(os.path.abspath(__file__))
git = Repo('{}/..'.format(path)).git
git.checkout('master')
git.pull()

这篇关于Python git模块无效的git存储库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆