简体   繁体   English

如何使用 python 自动化 git push 进程?

[英]How to automate git push processes using python?

I'm trying to automate the git push processes using python.我正在尝试使用 python 自动化git push进程。

I've succeeded automating all except entering the username and password after the git push command.除了在git push命令后输入用户名和密码外,我已经成功地自动化了所有操作。

This is my code so far:到目前为止,这是我的代码:

import subprocess
import sys

add: str = sys.argv[1]
commit: str = sys.argv[2]
branch: str = sys.argv[3]


def run_command(command: str):
    print(command)
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    print(str(process.args))
    if command.startswith("git push"):
        output, error = process.communicate()
    else:
        output, error = process.communicate()
    try:
        output = bytes(output).decode()
        error = bytes(error).decode()
        if not output:
            print("output: " + output)
        print("error: " + error)
    except TypeError:
        print()


def main():
    global add
    global commit
    global branch
    if add == "" or add == " ":
        add = "."
    if branch == "":
        branch = "master"
    print("add: '" + add + "' commit: '" + commit + "' branch: '" + branch + "'")

    command = "git add " + add
    run_command(command)

    commit = commit.replace(" ", "''")
    command = 'git commit -m "' + commit + '"'
    run_command(command)

    command = "git push origin " + branch
    run_command(command)


if __name__ == '__main__':
    main()

Is there any way to send the information to the command?有没有办法将信息发送到命令?

If possible, use a credential helper in order to cache that information (credentials associated to a remote URL).如果可能,请使用凭据帮助器来缓存该信息(与远程 URL 关联的凭据)。
Check the gitcredential section and " Git Tools - Credential Storage ".检查gitcredential 部分和“ Git 工具 - 凭证存储”。

git config --global credential.helper

That way, you won't have to enter that information at all.这样,您根本不必输入该信息。

This is how I solved it:这就是我解决它的方法:

# make sure to cd into the git repo foler

import subprocess
import sys
import os


msg = input('Type the commit message (+ ENTER):') 
repo_directory = os.getcwd()

subprocess.run(["git", "add", "."], cwd=repo_directory)
# commit file
subprocess.run(["git", "commit", "-m", msg], cwd=repo_directory)
# push
subprocess.run(["git", "push"], cwd=repo_directory)  

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

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