简体   繁体   English

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

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

I have this script that is supposed to push the files in the script's current directory to a repo:我有这个脚本,它应该将脚本当前目录中的文件推送到存储库:

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

The process is simple: just initialize the folder, add the configuration, add the files, commit with a message, and push.过程很简单:只需初始化文件夹,添加配置,添加文件,用消息提交,然后推送。

In this example, I am trying to use the SSH method since this will be ran automatically, so it cannot type a password.在这个例子中,我尝试使用 SSH 方法,因为它会自动运行,所以它不能输入密码。 The SSH keys are in the same folder (for the sake of example) and are attached to my GitHub account. SSH 密钥位于同一个文件夹中(例如),并附加到我的 GitHub 帐户。

However, I run into the following error:但是,我遇到了以下错误:

* 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 -

I am not sure what is causing the problem.我不确定是什么导致了问题。 I don't understand the error in the console.我不明白控制台中的错误。 This is the first time I'm using the SSH method but I don't see how I would be using it wrong.这是我第一次使用 SSH 方法,但我不知道我会如何错误地使用它。

If it helps, this is what the files look like in the directory:如果有帮助,这就是目录中文件的样子:

目录视图

What am I doing wrong?我究竟做错了什么?

First, add some echo between your command to isolate the one which trigger the error.首先,在您的命令之间添加一些 echo 以隔离触发错误的那个。

Second, add a git status to check:其次,添加一个git status来检查:

  • if you are in the right working directory如果您在正确的工作目录中
  • the state of the files文件的状态

An error like pathspec 'added'' did not match any file(s) known to git seems to indicate it is trying to add or commit files using the output of a previous command.pathspec 'added'' did not match any file(s) known to git的错误与pathspec 'added'' did not match any file(s) known to git似乎表明它正在尝试使用上一个命令的输出添加或提交文件。

Using gitpython-developers/GitPython would be safer.使用gitpython-developers/GitPython会更安全。

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

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