-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenclaw-git.sh
More file actions
executable file
·306 lines (261 loc) · 7.25 KB
/
openclaw-git.sh
File metadata and controls
executable file
·306 lines (261 loc) · 7.25 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
# OpenClaw Workspace Git Manager
# 用于管理OpenClaw工作区的版本控制
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 工作区路径
WORKSPACE_DIR="$HOME/.openclaw/workspace"
cd "$WORKSPACE_DIR"
# 显示帮助信息
show_help() {
echo "OpenClaw Workspace Git Manager"
echo ""
echo "用法: openclaw-git <命令> [参数]"
echo ""
echo "命令:"
echo " init 初始化git仓库"
echo " status 查看当前状态"
echo " commit [message] 提交更改(默认消息:'Auto backup')"
echo " log [n] 查看最近n次提交(默认10次)"
echo " rollback <commit> 回退到指定提交"
echo " diff [commit] 查看与指定提交的差异(默认:最新提交)"
echo " backup 创建备份(带时间戳的提交)"
echo " restore <file> 恢复指定文件到最新提交版本"
echo " branch <name> 创建并切换到新分支"
echo " branches 列出所有分支"
echo " merge <branch> 合并指定分支到当前分支"
echo " tag <name> 创建标签"
echo " tags 列出所有标签"
echo ""
echo "示例:"
echo " openclaw-git init # 初始化仓库"
echo " openclaw-git commit '修复记忆错误' # 提交更改"
echo " openclaw-git log 20 # 查看最近20次提交"
echo " openclaw-git rollback abc123 # 回退到提交abc123"
echo " openclaw-git backup # 创建带时间戳的备份"
echo ""
}
# 检查是否已初始化
check_init() {
if [ ! -d ".git" ]; then
echo -e "${RED}错误: Git仓库未初始化${NC}"
echo "请先运行: openclaw-git init"
exit 1
fi
}
# 初始化仓库
init_repo() {
if [ -d ".git" ]; then
echo -e "${YELLOW}Git仓库已存在${NC}"
read -p "是否重新初始化?这将丢失所有历史记录 (y/N): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "操作已取消"
exit 0
fi
rm -rf .git
fi
echo -e "${BLUE}初始化Git仓库...${NC}"
git init
echo -e "${BLUE}配置Git用户信息...${NC}"
git config user.name "OpenClaw Agent"
git config user.email "openclaw@local"
echo -e "${BLUE}创建初始提交...${NC}"
git add .
git commit -m "Initial commit: OpenClaw workspace initialized"
echo -e "${GREEN}✓ Git仓库初始化完成${NC}"
echo ""
echo "现在你可以使用以下命令:"
echo " openclaw-git commit - 提交更改"
echo " openclaw-git status - 查看状态"
echo " openclaw-git log - 查看历史"
}
# 查看状态
show_status() {
check_init
echo -e "${BLUE}=== Git 状态 ===${NC}"
git status
echo ""
echo -e "${BLUE}=== 未跟踪的文件 ===${NC}"
git ls-files --others --exclude-standard
echo ""
echo -e "${BLUE}=== 已修改的文件 ===${NC}"
git diff --name-only
}
# 提交更改
commit_changes() {
check_init
local message="${1:-Auto backup at $(date '+%Y-%m-%d %H:%M:%S')}"
echo -e "${BLUE}检查待提交的更改...${NC}"
# 检查是否有更改
if [ -z "$(git status --porcelain)" ]; then
echo -e "${YELLOW}没有需要提交的更改${NC}"
exit 0
fi
echo -e "${BLUE}待提交的文件:${NC}"
git status --short
echo ""
# 添加所有更改(除了.gitignore中排除的)
git add .
# 提交
git commit -m "$message"
echo -e "${GREEN}✓ 提交成功${NC}"
echo -e "${BLUE}提交ID: $(git rev-parse HEAD)${NC}"
}
# 查看历史
show_log() {
check_init
local n="${1:-10}"
echo -e "${BLUE}=== 最近 $n 次提交 ===${NC}"
git log --oneline --graph --decorate -n "$n"
}
# 回退到指定提交
rollback_to() {
check_init
local commit="$1"
if [ -z "$commit" ]; then
echo -e "${RED}错误: 请指定提交ID${NC}"
echo "用法: openclaw-git rollback <commit>"
exit 1
fi
echo -e "${YELLOW}警告: 这将丢失所有未提交的更改!${NC}"
echo -e "${BLUE}回退到提交: $commit${NC}"
read -p "确认回退? (y/N): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "操作已取消"
exit 0
fi
git reset --hard "$commit"
echo -e "${GREEN}✓ 已回退到提交 $commit${NC}"
}
# 查看差异
show_diff() {
check_init
local commit="${1:-HEAD}"
echo -e "${BLUE}=== 与 $commit 的差异 ===${NC}"
git diff "$commit"
}
# 创建备份
create_backup() {
check_init
local timestamp=$(date '+%Y%m%d_%H%M%S')
local message="Backup at $timestamp"
commit_changes "$message"
git tag "backup_$timestamp"
echo -e "${GREEN}✓ 备份已创建: backup_$timestamp${NC}"
}
# 恢复文件
restore_file() {
check_init
local file="$1"
if [ -z "$file" ]; then
echo -e "${RED}错误: 请指定文件路径${NC}"
echo "用法: openclaw-git restore <file>"
exit 1
fi
git checkout HEAD -- "$file"
echo -e "${GREEN}✓ 文件已恢复: $file${NC}"
}
# 创建分支
create_branch() {
check_init
local name="$1"
if [ -z "$name" ]; then
echo -e "${RED}错误: 请指定分支名称${NC}"
echo "用法: openclaw-git branch <name>"
exit 1
fi
git checkout -b "$name"
echo -e "${GREEN}✓ 已创建并切换到分支: $name${NC}"
}
# 列出分支
list_branches() {
check_init
echo -e "${BLUE}=== 所有分支 ===${NC}"
git branch -a
}
# 合并分支
merge_branch() {
check_init
local branch="$1"
if [ -z "$branch" ]; then
echo -e "${RED}错误: 请指定要合并的分支${NC}"
echo "用法: openclaw-git merge <branch>"
exit 1
fi
git merge "$branch"
echo -e "${GREEN}✓ 已合并分支: $branch${NC}"
}
# 创建标签
create_tag() {
check_init
local name="$1"
if [ -z "$name" ]; then
echo -e "${RED}错误: 请指定标签名称${NC}"
echo "用法: openclaw-git tag <name>"
exit 1
fi
git tag "$name"
echo -e "${GREEN}✓ 已创建标签: $name${NC}"
}
# 列出标签
list_tags() {
check_init
echo -e "${BLUE}=== 所有标签 ===${NC}"
git tag -l
}
# 主命令
case "${1:-help}" in
init)
init_repo
;;
status)
show_status
;;
commit)
commit_changes "$2"
;;
log)
show_log "$2"
;;
rollback)
rollback_to "$2"
;;
diff)
show_diff "$2"
;;
backup)
create_backup
;;
restore)
restore_file "$2"
;;
branch)
create_branch "$2"
;;
branches)
list_branches
;;
merge)
merge_branch "$2"
;;
tag)
create_tag "$2"
;;
tags)
list_tags
;;
help|--help|-h)
show_help
;;
*)
echo -e "${RED}错误: 未知命令 '$1'${NC}"
echo ""
show_help
exit 1
;;
esac