2

我正在尝试为我的用户制作一个小程序,用于 git 和其他的基本操作。而且我在克隆私有远程存储库时遇到了很多问题。

我有以下配置:在远程服务器上建立了 Python 3.4 Windows GitPython Ssh 连接。

这是我的代码:

print(blue + "Where to clone repos ?")
path_repo = input(cyan + "> " + green)
try:
    assert os.path.exists(path_repo)
except AssertionError:
    print(red + "Path does not exist")
continue
print(blue + "Name of Repos :")
name_repo = input(cyan + "> " + green)
remote_path = "git@dev01:/home/git/repos/{0}.git".format(name_repo)
local_path = "{0}/{1}".format(path_repo, name_repo)
# Repo.clone_from(remote_path, local_path)
repo = Repo.clone_from(remote_path, local_path)
#print(repo.git.fetch())
#print(repo.git.pull())
#print(repo.git.status())

这不会引发错误,但脚本会在最后停止并阻塞终端(给我无限的空行,没有>>>

运行之后,如果我进入 Git Bash 并输入git status他似乎没有创建分支,只需初始化。因此,我添加了代码的最后 3 行以查看发生了什么变化,但什么也没有。

如果我在 Git Bash 中输入git pull,他会很好地拉出 master 分支......

如果有人可以解决我的问题吗?

我在哪里犯了错误?

谢谢

4

1 回答 1

1

您描述的代码和方法存在许多问题......

首先,你有

continue

写在你的代码中间......所以我假设你提供的代码在某个循环中。循环中无条件之后continue的所有代码都是死代码 - 它永远不会被执行。

因此,您似乎从未真正克隆过任何东西......

要查看所有分支都在 repo 中,请使用类似git branch -va. 恐怕这git status不是为此而生的。

由于您似乎已经以某种方式初始化了存储库,因此您将无法克隆到同一位置 - 因为克隆会创建存储库。

此外,检查目录是否存在是没有意义的——因为如果git clone目录不存在则创建目录。


总结一下,试试下面这个简单的代码。我已经注释掉了由于 repo 已经存在而可能导致错误的行。

print(blue + "Where to clone repo?")
path_repo = input(cyan + "> " + green)
print(blue + "Name of repo:")
name_repo = input(cyan + "> " + green)
remote_path = "git@dev01:/home/git/repos/{0}.git".format(name_repo)
local_path = "{0}/{1}".format(path_repo, name_repo)
#repo = Repo.clone_from(remote_path, local_path)
repo = Repo(local_path)
info = repo.remote('origin').fetch()
if not info:
    print('no fetch information')
for i in info:
    if i.note:
        print('fetched, note is: ' + i.note.strip())
    else:
        print('fetched, no note')
于 2015-06-25T07:21:01.050 回答