-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout_history.sh
executable file
·96 lines (81 loc) · 2.49 KB
/
checkout_history.sh
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Config
COMMITS_PER_PAGE=20
page=0
# Store commits with date
mapfile -t commits < <(git log --pretty=format:"%h|%cr|%s")
total_commits=${#commits[@]}
total_pages=$(( (total_commits + COMMITS_PER_PAGE - 1) / COMMITS_PER_PAGE ))
current=$(git rev-parse HEAD)
while true; do
clear
echo "提交历史 (第 $((page + 1))/$total_pages 页)"
echo "------------------------"
# Calculate page bounds
start=$((page * COMMITS_PER_PAGE))
end=$((start + COMMITS_PER_PAGE))
[[ $end -gt $total_commits ]] && end=$total_commits
# Display commits
for ((i = start; i < end; i++)); do
commit=${commits[$i]}
hash=${commit%%|*}
rest=${commit#*|}
date=${rest%%|*}
msg=${rest#*|}
if [[ $hash = $(echo $current | cut -c1-7) ]]; then
echo "$i $hash ($date) * $msg"
else
echo "$i $hash ($date) $msg"
fi
done
echo -e "\n操作选项:"
echo "-2: 上一页"
echo "-1: 下一页"
echo " 0: 退出"
echo -e "请输入版本编号或操作选项:"
read -r choice
# Handle navigation
if [[ $choice == "-1" ]]; then
if ((page < total_pages - 1)); then
((page++))
fi
continue
elif [[ $choice == "-2" ]]; then
if ((page > 0)); then
((page--))
fi
continue
elif [[ $choice == "0" ]]; then
exit 0
fi
# Handle checkout
if [[ $choice =~ ^[0-9]+$ ]] && ((choice < total_commits)); then
# Extract only hash part from stored commit
commit=${commits[$choice]}
commit_hash=${commit%%|*}
# Store current shebang modifications
git diff > temp_changes.patch
# Restore all tracked files to their original state
git restore .
if git checkout "$commit_hash"; then
echo "已切换到版本 $commit_hash"
# Show current commit
echo "当前版本:"
git log --oneline -1
# if not in venv, source it
if [[ -d ".venv" && -z "$VIRTUAL_ENV" ]]; then
source .venv/bin/activate
fi
pip install -r requirements.txt
exit 0
else
echo "切换版本失败"
read -n 1 -s -r -p "按任意键继续..."
fi
# Reapply Termux shebang
termux-fix-shebang *.sh
else
echo "无效的选择"
read -n 1 -s -r -p "按任意键继续..."
fi
done