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
45
46
47
48
49
50
51

# @Author: [email protected]
# @CreateData: 2023-10-25 15:50:40
# @MaintenanceDate: 2023-10-25 15:50:40
# @Purpose: python 提交本地文件到 github


import subprocess
import datetime
import time
# 定义要切换到的目录
repo_directory = r"M:\blog_hexo"

# 切换到指定目录
try:
# 使用subprocess运行cd命令切换目录
subprocess.run(f"cd /d {repo_directory} && git status", shell=True, check=True, stdout=subprocess.PIPE)

except subprocess.CalledProcessError as e:
print("切换目录时出现错误:", e)
exit(1)

# 获取当前时间字符串
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d_%H%M%S")

# 写 log文件
with open(f"{repo_directory}/log.txt", "w") as fw:
fw.write(formatted_datetime + '\n')

# 执行Git命令(例如:git add, git commit, git push)
try:
# 拉取语雀文章
subprocess.run(f"cd /d {repo_directory} && yuque-hexo sync", shell=True, check=True)

# 添加文件
subprocess.run(f"cd /d {repo_directory} && git add .", shell=True, check=True)

# 提交更改
subprocess.run(f"cd /d {repo_directory} && git commit -m '{formatted_datetime}'", shell=True, check=True)

# 推送到GitHub
subprocess.run(f"cd /d {repo_directory} && git push", shell=True, check=True)

print("成功提交到GitHub")

except subprocess.CalledProcessError as e:
print("=" * 80)
print("执行Git命令时出现错误:", e)
exit(1)