forked from Roboparty/roboto_origin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync_subtrees.sh
More file actions
executable file
·221 lines (184 loc) · 6.71 KB
/
sync_subtrees.sh
File metadata and controls
executable file
·221 lines (184 loc) · 6.71 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
#!/bin/bash
# Subtree 同步脚本(包括动态处理 submodule 转为 subtree)
# 流程:
# 1. 同步主模块的 subtree
# 2. 从远程获取主模块的 .gitmodules
# 3. 解析 .gitmodules,将 submodule 也作为 subtree 拉取
echo "==================================="
echo "开始同步所有 subtree(含 submodule 转 subtree)..."
echo "==================================="
# 将 HTTPS URL 转换为 SSH URL
convert_to_ssh_url() {
local url="$1"
# 转换 https://github.com/user/repo.git -> git@github.com:user/repo.git
if echo "$url" | grep -q "^https://github.com/"; then
echo "$url" | sed 's|https://github.com/|git@github.com:|'
else
echo "$url"
fi
}
# 确保 remote 存在的辅助函数
ensure_remote() {
local remote_name="$1"
local remote_url="$2"
# 转换为 SSH URL
remote_url=$(convert_to_ssh_url "$remote_url")
if ! git remote | grep -q "^${remote_name}$"; then
echo " 添加 remote: $remote_name ($remote_url)"
git remote add "$remote_name" "$remote_url"
else
# 更新已存在的 remote URL
local current_url=$(git remote get-url "$remote_name")
if [ "$current_url" != "$remote_url" ]; then
echo " 更新 remote URL: $remote_name -> $remote_url"
git remote set-url "$remote_name" "$remote_url"
fi
fi
}
# 自动检测远程仓库的默认分支
detect_default_branch() {
local remote_name="$1"
# 优先尝试 main,然后 master
if git ls-remote --heads "$remote_name" | grep -q "refs/heads/main"; then
echo "main"
elif git ls-remote --heads "$remote_name" | grep -q "refs/heads/master"; then
echo "master"
else
echo ""
fi
}
# Subtree 同步辅助函数
sync_subtree() {
local prefix="$1"
local remote_name="$2"
local branch="${3:-auto}"
echo ""
echo "同步 $prefix..."
# 确保 remote 存在
if ! git remote | grep -q "^${remote_name}$"; then
echo " ⚠ remote $remote_name 不存在,跳过"
return 1
fi
# 自动检测分支
if [ "$branch" = "auto" ]; then
branch=$(detect_default_branch "$remote_name")
if [ -z "$branch" ]; then
echo " ⚠ 无法检测远程仓库的默认分支"
return 1
fi
echo " 检测到默认分支: $branch"
fi
# 检查目录是否为空或不存在
local need_add=false
if [ ! -d "$prefix" ] || ! ls -A "$prefix" >/dev/null 2>&1; then
need_add=true
fi
# 拉取更新
if git fetch "$remote_name" "$branch" >/dev/null 2>&1; then
if [ "$need_add" = true ]; then
# 首次添加
if git subtree add --prefix="$prefix" "$remote_name" "$branch" >/dev/null 2>&1; then
echo " ✓ $prefix 添加成功"
return 0
else
echo " ⚠ $prefix 添加失败"
return 1
fi
else
# 更新已有的 subtree
if git subtree pull --prefix="$prefix" "$remote_name" "$branch" -m "Merge subtree $prefix" >/dev/null 2>&1; then
echo " ✓ $prefix 同步成功"
return 0
else
echo " ⚠ $prefix 同步失败或已是最新"
return 1
fi
fi
else
echo " ⚠ 无法从 $remote_name 获取 $branch 分支"
return 1
fi
}
# 从远程获取 .gitmodules 并解析,将 submodule 作为 subtree 同步
sync_submodules_as_subtrees() {
local remote_name="$1"
local base_path="$2"
local remote_branch="${3:-main}"
echo ""
echo "处理 $base_path 的 submodule(转为 subtree)..."
# 从远程获取 .gitmodules 文件
local tmp_gitmodules=$(mktemp)
if git show "$remote_name/$remote_branch:.gitmodules" > "$tmp_gitmodules" 2>/dev/null; then
echo " ✓ 从远程获取 .gitmodules"
# 解析 .gitmodules 文件
local current_path=""
local current_url=""
while IFS='=' read -r key value; do
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
if [ "$key" = "path" ]; then
current_path="$value"
elif [ "$key" = "url" ]; then
current_url="$value"
if [ -n "$current_path" ] && [ -n "$current_url" ]; then
local full_path="$base_path/$current_path"
local submodule_name="submodule_$(echo "$current_path" | tr '/' '_')_$remote_name"
echo " 发现 submodule: $current_path"
echo " URL: $current_url"
# 添加 remote
ensure_remote "$submodule_name" "$current_url"
# 同步 subtree(自动检测分支)
sync_subtree "$full_path" "$submodule_name" "auto"
current_path=""
current_url=""
fi
fi
done < "$tmp_gitmodules"
rm -f "$tmp_gitmodules"
else
echo " ⚠ 远程仓库没有 .gitmodules 文件或获取失败"
rm -f "$tmp_gitmodules"
fi
}
# ========== 主流程 ==========
echo ""
echo "==================================="
echo "步骤 1: 同步主模块 subtree..."
echo "==================================="
# Atom01_hardware
sync_subtree "modules/Atom01_hardware" "Atom01_hardware" "main"
# atom01_description
sync_subtree "modules/atom01_description" "atom01_description" "main"
# atom01_train
sync_subtree "modules/atom01_train" "atom01_train" "main"
# atom01_deploy
sync_subtree "modules/atom01_deploy" "atom01_deploy" "main"
echo ""
echo "==================================="
echo "步骤 2: 将 submodule 转为 subtree(动态获取 .gitmodules)..."
echo "==================================="
# 处理 atom01_deploy 的 submodule
sync_submodules_as_subtrees "atom01_deploy" "modules/atom01_deploy" "main"
# 处理 atom01_train 的 submodule
sync_submodules_as_subtrees "atom01_train" "modules/atom01_train" "main"
echo ""
echo "==================================="
echo "步骤 3: 清理旧的 .gitmodules 文件..."
echo "==================================="
# 删除 .gitmodules 文件(因为不再使用 submodule)
for gitmodules in modules/atom01_deploy/.gitmodules modules/atom01_train/.gitmodules; do
if [ -f "$gitmodules" ]; then
echo " 删除 $gitmodules"
rm -f "$gitmodules"
fi
done
echo ""
echo "==================================="
echo "✓ 所有 subtree 同步完成!"
echo "==================================="
echo ""
echo "现在可以提交并推送更新:"
echo " git add ."
echo " git commit -m 'chore: update all subtrees (submodules converted to subtree)'"
echo " git push"
echo ""