我正在尝试使用 python 自动执行 git push 过程。
除了在 git push 命令后输入用户名和密码外,我已经成功地自动执行了所有操作。
到目前为止,这是我的代码:
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()
有什么办法可以把信息发给命令吗?
请您参考如下方法:
如果可能,使用凭证助手来缓存该信息(与远程 URL 关联的凭证)。
检查gitcredential section和“Git Tools - Credential Storage”。
git config --global credential.helper
这样一来,您根本就不必输入该信息。
