gogs迁移至gitlab | 我的日常分享

gogs迁移至gitlab

gogs迁移至gitlab

原理:不管是gogs迁移至gitlab,还是gitlab迁移至gogs,或者gitee迁移至gitlab等等不同平台的迁移。手动迁移的步骤都是如下

1.进入项目的仓库,删除原来的远程地址

2.新指定新的远程地址

3.上传所有本地分支至新的远程地址

4.上传所有tag至新的远程地址

1
2
3
4
5
cd existing_repo
git remote rm origin
git remote add origin git@gitlab.yuencode.cn:jiaxiaoyu/existing_repo.git
git push -u origin --all
git push -u origin --tags

对于Gogs的git仓库存储在gogs/gogs-repositories

此目录的是对应的gogs每个用户的仓库。

image-20240108180254253

我们需要根据目录和目录里面的仓库名称,拼接出所有的url

image-20240108220732845

写个脚本:

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
import os
from git import Repo
import git
import logging
# pip3 install gitpython
import re
import time

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# gogs仓库目录
directory = "/www/opt/module/gogs/gogs-repositories/jiaxiaoyu"

# 排除的目录
exclude = ["pdc_server.wiki.git"]

# origin_url = "https://jiaxiaoyu:password@git.yuencode.cn/jiaxiaoyu"
origin_url = "http://jiaxiaoyu:password@127.0.0.1:3033/jiaxiaoyu"

# 缓存目录,所有的仓库数据会下载到这里
temp_path = "/www/temp"


def main():
# 获取所有仓库名称
file_list = []
for f in os.listdir(directory):
if f in exclude:
continue
file_list.append(f)

logging.info(f"找到仓库共计:{len(file_list)}个")
logging.info(f"排除仓库:{len(exclude)}{exclude}")
logging.info(file_list)


if not os.path.exists(temp_path):
os.makedirs(temp_path)
logging.info(f"文件夹 {temp_path} 创建成功")

c = os.listdir(temp_path)
if c:
logging.error(f"error:目录{temp_path}非空")
return
count = 0
for name in file_list:
count+=1
p = temp_path+"/"+name
u = origin_url+'/'+name
logging.info(f'scan repo({count}/{len(file_list)}): {u}')
if not os.path.exists(p):
os.makedirs(p)
else:
logging.error(f"error:目录{p}已存在")
return
Repo.clone_from( url=u, to_path=p )
#获取远程分支的分支名称
repo = git.Repo(p)
remote_branches = []
for ref in repo.git.branch('-r').split('\n'):
remote_branches.append(ref)

#格式化分支名称
del remote_branches[0]
bran_name = []
pattern = r"origin/(.*)"
for bran in remote_branches:
match = re.search(pattern, bran)
if match:
bran_name.append( match.group(1))
else:
logging.error("No match found.")
return

logging.info(f"远程分支:{bran_name}")
# 在本地切换一遍分支,因为在上传至新的gitlab库时,只会把已存在的本地分支上传,没有的不会上传,所以必须把所有分支都切换一遍,把远程分支下载到本地。
for bran in bran_name:
logging.info(f"check 分支:{bran}")
repo.git.checkout(bran)

if __name__ == "__main__":
start_time = time.perf_counter()
main()
end_time = time.perf_counter()
elapsed_time = end_time - start_time
logging.info(f"Time elapsed: {elapsed_time} seconds")

如图,共86个仓库,等待下载完成。

image-20240108222533874

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
#!/bin/bash
# 缓存目录,与上面保持一致
path="/www/temp"
# 要推送的仓库地址
origin_url_new="https://jiaxiaoyu:password@gitlab.yuencode.cn/jiaxiaoyu"

counter=0

if [ -d "$path" ]; then
total_dirs=$(find "$path" -mindepth 1 -maxdepth 1 -type d | wc -l)

for dir in "$path"/*; do
if [ -d "$dir" ]; then
((counter++))
tmp="$(basename "$dir")"
cd "$path/$tmp"
echo "################ start [$counter/$total_dirs] start ################"
echo "resp_url: $origin_url_new/$tmp"
# 检查是否存在名为 "origin" 的远程仓库
if git remote | grep -q "^origin$"; then
echo "exec: git remote rm origin"
git remote rm origin
fi
echo "exec: git remote add origin $origin_url_new/$tmp"
git remote add origin "$origin_url_new/$tmp"

echo "exec: git push -u origin --all"
git push -u origin --all

echo "exec: git push -u origin --tags"
git push -u origin --tags
echo "@@@@@@@@@@@@@@@@@@@@@@ end @@@@@@@@@@@@@@@@@@@@@@@"
echo "----------------------------------------------------------"
echo "----------------------------------------------------------"
echo "----------------------------------------------------------"
fi
done
fi

共86个仓库等待推送完成。

截图里显示87,是因为脚本有个bug,已经改了。就不重新截图了。

image-20240108222310480

共86个仓库,同步完成:

image-20240108222745968

其中能够使用内网的地方尽量使用内网,速度快一些。