提交所有更改到GIT通过Python并捕获输出

2024-09-29 23:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须拿出Python脚本来捕获GIT的所有更改。此脚本将自动运行一天一次。你知道吗

我已经想出了这个办法,但看起来并不令人满意。你知道吗

import os
import subprocess
from subprocess import *

accpath = 'M:/STD1/GIT_TEST'
print (accpath)
subprocess.Popen(["ls"],cwd = accpath)
subprocess.Popen(["git","init"],cwd = accpath)

subprocess.Popen(["git","add","."],cwd = accpath)
subprocess.Popen(["git","commit","-m","'committed'"],cwd = accpath)

p = subprocess.Popen(["git","log","-p","-1"], cwd = accpath, stdout = 
subprocess.PIPE,)
stdout_value = p.communicate()[0]
t1 = repr(stdout_value)

v = subprocess.Popen(["git","status"],cwd = accpath)
std_val = v.communicate()
print(std_val)

输出是这样的

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working         directory)
#
#       deleted:    England/mana2.ttx
#       deleted:    England/test12121212.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

你们能帮我吗? -删除了2个文件,但没有要提交的更改。 -我只是在寻找一个明确的输出,可以作为一个报告(邮件)发送。你知道吗


Tags: toimportgit脚本addusestdoutcommit
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:48

这是因为git add只添加新文件,或修改现有文件。要从树中进行阶段性删除,您需要git rm,正如状态所示(即使文件在物理上不在那里,这也会起作用)。你知道吗

如错误所示,使用git commit -a将绕过此操作—它将提交所有修改和删除。但是仍然需要git add来添加新文件。所以只要改变一下:

subprocess.Popen(["git","commit","-a","-m","'committed'"],cwd = accpath)

相关问题 更多 >

    热门问题