[英]Commit All changes and email the files that were changed/added/deleted
我是python和GIT新手。
我的目標是將所有更改提交到GIT並通過電子郵件發送已更改/添加/刪除的文件列表。 該腳本每天運行一次。
我想出了這個。
import os
import subprocess
from subprocess import *
accpath = '/home/Project'
subprocess.Popen(["git","init"],cwd = accpath)
subprocess.Popen(["git","add","."],cwd = accpath)
subprocess.Popen(["git","commit","-a","-m","'committed'"],cwd = accpath)
v = subprocess.Popen(["git","status","-s"],cwd = accpath)
輸出:
Reinitialized existing Git repository in /home/Project/.git/
M Italy/ACMilan/team.cfg
D Italy/ACMilan/team.tet
A Italy/ACMilan/team.ttwq
[master 2381802] 'committed'
2 files changed, 1 insertions(+), 2 deletions(-)
delete mode 100644 Italy/ACMilan/team.tet
create mode 100644 Italy/ACMilan/team.ttwq
如何獲得可理解的輸出並通過電子郵件發送? 我只需要M,D和A部分。
針對您的問題的兩個簡單選擇:
在提交時使用Git掛鈎推送到某些API,然后運行某種cron腳本來發送電子郵件。 http://githooks.com/
Git Hooks在git事件上運行腳本。 這些腳本可以由您定義。 快速運行:
可以在任何初始化的Git存儲庫中添加Git鈎子腳本。
如果再執行ls -l .git/hooks/
,則會看到每個可用的鈎子。 如果打開pre-commit
掛鈎,則可以覆蓋默認行為。 (記住,請在出錯時備份原始腳本。)
要么
使用github REST API獲取當天的提交。 那應該為您提供有關當天提交的一些信息https://developer.github.com/v3/ 。 一個示例如下所示:
獲取https://api.github.com/repos/:owner/:repo/commits
調用時,您將獲得分頁的提交列表。 (請記住,您將需要添加身份驗證頭)。
如果您只想接收當天的提交,請添加since參數:
GET https://api.github.com/repos/:owner/:repo/commits?since=2018-03-20T00:00:00+02:00
那只會給您當天的承諾。 查看github REST api文檔以獲取全部詳細信息。
對於您的用例,我建議您使用REST API。 Git鈎子更適合在預先提交時運行單元測試或短絨,以防止不良的拉拔請求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.