GitPython:如何在不提供完整路径的情况下在目录内进行 git 克隆?

标签 gitpython

当我做git clone时从命令行,它会自动使用该存储库的名称创建一个新目录,并将代码放入该目录中。

但是当我这样做时

 from git import Repo
 Repo.clone_from('https://github.com/jessfraz/dockerfiles.git', '/home/dev')

我明白了

GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git clone -v https://github.com/jessfraz/dockerfiles.git /home/dev
  stderr: 'fatal: destination path '/home/dev' already exists and is not an empty directory.

如何获取存储库名称并构建路径 /home/dev/<repo_name>

最佳答案

我刚刚查看了代码,我认为您需要提供工作副本的完整路径(即/home/dev/dockerfiles)。

如果您不希望这样,您可能需要创建自己的从原始 Repo 类继承的 Repo 类。然后,您可能想要覆盖 clone_from() 类方法。像这样的东西(未经测试,只是一个快速草稿):

from git import Repo


class YourRepo(Repo):

    @classmethod
    def clone_from(cls, url, to_path=None, **kwargs):
        if to_path is None:
            to_path = url.split('/')[-1]
            if to_path.endswith('.git'):
                to_path = to_path[0:-4]
        return super().clone_from(url=url, to_path=to_path, **kwargs)

关于GitPython:如何在不提供完整路径的情况下在目录内进行 git 克隆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371631/

相关文章:

python - gitpython 使用 PYTHON 检查文件是否有任何更改

python - Gitpython 检查克隆的存储库

python - gitpython - 如何检查远程分支是否存在?

python3 + pygit : "No module named ' repository'"

python - 想要检查存储库是否存在以及是否是公共(public)的(gitpython)

使用用户名和密码的 gitpython git 身份验证

python - 无法在AWS lambda中使用gitpython

ssl - 使用 GitPython 库的 git clone

python - gitpython 相当于 git-apply

python - 使用 gitpython 来区分 pull 请求中的更改