-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoGit.py
executable file
·44 lines (37 loc) · 1.31 KB
/
autoGit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import git
import time
def pushRepo(retry): #将提交推送至github
repo = git.Repo(".")
for i in range(retry):
print(f"开始第{i + 1}次推送:", end="", flush=True)
try:
repo.remotes.origin.pull(rebase=True) #推送前先拉取最新代码
repo.remotes.origin.push().raise_if_error()
print("推送成功。")
break
except Exception as e:
print(e)
time.sleep(2)
if (i == (retry - 1)):
print("达到最大重试次数,退出推送。")
def pushFile(file, retry): #检查文件是否有修改,如果有修改,则将修改推送至github
repo = git.Repo(".")
listStatus = repo.git.status(file)
if ("modified" in listStatus):
print(f"{file}已更新,开始推送至github。")
repo.git.add(file)
repo.git.commit("-m", f"更新{file}")
pushRepo(retry)
else:
print(f"{file}未更新,无需推送至github。")
def checkoutFile(file):
repo = git.Repo(".")
listStatus = repo.git.status(file)
if ("modified" in listStatus):
print(f"{file}已修改,回退文件")
repo.git.checkout(file)
else:
print(f"{file}未修改,无需回退")
if __name__ == "__main__":
#pushRepo(1)
checkoutFile("list.yaml")