forked from hq450/fancyss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear.py
More file actions
67 lines (57 loc) · 2.2 KB
/
clear.py
File metadata and controls
67 lines (57 loc) · 2.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import subprocess
from datetime import datetime, timedelta
def run_command(cmd, check=True):
"""运行 shell 命令并处理错误"""
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if check and result.returncode != 0:
print(f"命令执行失败: {cmd}")
print(f"标准输出: {result.stdout}")
print(f"错误输出: {result.stderr}")
raise subprocess.CalledProcessError(result.returncode, cmd)
return result
def get_old_commits(days=2):
"""获取早于指定天数的提交"""
threshold_date = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
cmd = f'git log --before="{threshold_date}" --format=%H'
commits = run_command(cmd, check=False).stdout.strip().split('\n')
return [c for c in commits if c]
def delete_old_commits():
"""删除2天前的提交并重写 3.0 分支"""
old_commits = get_old_commits()
if not old_commits:
print("没有找到2天前的提交。")
return
# 确保在 3.0 分支并拉取最新文件
run_command('git checkout 3.0')
run_command('git pull origin 3.0')
# 获取初始提交的哈希(只取第一个初始提交)
initial_commit = run_command(
'git rev-list --max-parents=0 HEAD | head -n 1',
check=True
).stdout.strip()
# 重置提交历史到初始提交,保留所有文件
run_command(f'git reset --soft {initial_commit}')
# 重新添加所有文件
run_command('git rm -r --cached .')
run_command('git add .')
# 创建新提交
try:
run_command('git commit --allow-empty -m "清理后的初始提交"')
except subprocess.CalledProcessError as e:
print(f"提交失败: {e}")
return
# 强制推送重写的 3.0 分支
try:
run_command('git push origin 3.0 --force')
except subprocess.CalledProcessError as e:
print(f"强制推送 3.0 分支失败: {e}")
print("请确保已启用分支保护中的 '允许强制推送' 或有足够权限。")
return
# ✅ 只改这一行
if __name__ == "__main__":
try:
delete_old_commits()
except Exception as e:
print(f"发生错误: {e}")
exit(1)