繁体   English   中英

不能使用 Python(子进程)进行 Git 推送?

[英]Cant Git Push Using Python (Subprocess)?

我有这个脚本,它应该将脚本当前目录中的文件推送到存储库:

def pushToGit():
    currDir = os.path.dirname(os.path.realpath(sys.argv[0]))
    try:
        shutil.rmtree(os.path.join(currDir, '.git'))
    except:
        pass
    try:
        cp = cmd.run("git init", check=True, shell=True, cwd=currDir)
        cp = cmd.run(f"git remote add origin git@github.com:johnsmith/repo_hold.git", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git config user.name 'john smith'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git config user.email 'john@smith.com'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git add .", check=True, shell=True, cwd=currDir + "//")
        message = f"Some generated message here"
        cp = cmd.run(f"git commit -m '{message}'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git push -u origin master", check=True, shell=True, cwd=currDir + "//")
        return True
    except Exception as e:
        return False

过程很简单:只需初始化文件夹,添加配置,添加文件,用消息提交,然后推送。

在这个例子中,我尝试使用 SSH 方法,因为它会自动运行,所以它不能输入密码。 SSH 密钥位于同一个文件夹中(例如),并附加到我的 GitHub 帐户。

但是,我遇到了以下错误:

* Running on http://127.0.0.1:2897/ (Press CTRL+C to quit)
Initialized empty Git repository in C:/Users/John/Desktop/my_repo/.git/
warning: LF will be replaced by CRLF in id_rsa.
The file will have its original line endings in your working directory
error: pathspec 'URL' did not match any file(s) known to git
error: pathspec 'added'' did not match any file(s) known to git
127.0.0.1 - - [08/Sep/2020 22:13:36] "←[37mGET /<url post request here> HTTP/1.1←[0m" 200 -

我不确定是什么导致了问题。 我不明白控制台中的错误。 这是我第一次使用 SSH 方法,但我不知道我会如何错误地使用它。

如果有帮助,这就是目录中文件的样子:

目录视图

我究竟做错了什么?

首先,在您的命令之间添加一些 echo 以隔离触发错误的那个。

其次,添加一个git status来检查:

  • 如果您在正确的工作目录中
  • 文件的状态

pathspec 'added'' did not match any file(s) known to git的错误与pathspec 'added'' did not match any file(s) known to git似乎表明它正在尝试使用上一个命令的输出添加或提交文件。

使用gitpython-developers/GitPython会更安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM