tomono是一个代码库迁移工具,可以很方便的将multiplerepo的代码迁移至monorepo,并且保留所有分支和历史提交记录,有很多特性:
下面是根据实际开发过程中进行的一次迁移工作,参考该过程写的一个demo,便于记录。
1. 构建monorepo git仓库
初始化构建一个git仓库即可
git init
或直接在GitHub/GitLeb/gitee构建项目,再将项目git clone下来,此处演示为GitHub构建项目:
- multiple-repo1:multiple仓库1
- multiple-repo2:multiple仓库2
- multiple-to-mono:monorepo仓库
2. 使用tomono将multiplerepo仓库迁移至构建的monorepo应用
参考tomono的应用示例即可:
2.1. 应用实践过程
- 在monorepo仓库的父级目录创建
repos.txt
文件:
内容如下:
txt
git@github.com:7neves/multiple-repo1.git repo1
git@github.com:7neves/multiple-repo2.git repo2
- 下载
tomono
shell命令(复制github.com/hraban/tomo...仓库下的tomono文件内容即可),在用户根目录构建~/tomono/tomono.sh
脚本文件:
sh
#!/usr/bin/env bash
set -euo pipefail ${DEBUGSH+-x}
# 这里执行后报错,先不执行,不影响
# shopt -s inherit_errexit
# Poor man's arg parse :/
arg="${1-}"
: "${MONOREPO_NAME:=scyx-frontend}"
case "$arg" in
"")
if [[ -d "$MONOREPO_NAME" ]]; then
>&2 echo "monorepo directory $MONOREPO_NAME already exists"
exit 1
fi
mkdir "$MONOREPO_NAME"
cd "$MONOREPO_NAME"
git init
;;
"--continue")
if [[ ! -d "$MONOREPO_NAME" ]]; then
>&2 echo "Asked to --continue, but monorepo directory $MONOREPO_NAME doesn't exist"
exit 1
fi
cd "$MONOREPO_NAME"
if git status --porcelain | grep . ; then
>&2 echo "Git status shows pending changes in the repo. Cannot --continue."
exit 1
fi
# There isn't anything special about --continue, really.
;;
"--help" | "-h" | "help")
cat <<EOF
Usage: tomono [--continue]
For more information, see the documentation at "https://tomono.0brg.net".
EOF
exit 0
;;
*)
>&2 echo "Unexpected argument: $arg"
>&2 echo
>&2 echo "Usage: tomono [--continue]"
exit 1
;;
esac
# 创建一个空树
empty_tree="$(git hash-object -t tree /dev/null)"
# Note this is top-level in the script so it's reading from the script's stdin
# 读取stdin中的内容
while IFS=$'\r'"$IFS" read -r repourl reponame repopath; do
if [[ -z "$repopath" ]]; then
repopath="$reponame"
fi
# 添加远程仓库
git remote add "$reponame" "$repourl"
# 设置远程仓库的fetch规则
git config --add "remote.$reponame.fetch" "+refs/tags/*:refs/tags/$reponame/*"
# 设置远程仓库的tagOpt
git config "remote.$reponame.tagOpt" --no-tags
# 获取远程仓库的提交
git fetch --atomic "$reponame"
# 获取远程仓库的分支
git branch -r --no-color --list "$reponame/*" --format "%(refname:lstrip=3)" | while read -r branch ; do
# 如果分支不存在,则创建一个根提交
if ! git show-ref --verify --quiet "refs/heads/$branch"; then
tree="$(git commit-tree \
"$empty_tree" \
-m "Root commit for monorepo branch $branch")"
git branch -- "$branch" "$tree"
fi
# 将当前分支设置为远程分支
git symbolic-ref HEAD "refs/heads/$branch"
# 重置当前分支
git reset -q
# 将远程分支合并到本地分支
git read-tree --prefix "$repopath" "refs/remotes/$reponame/$branch"
tree="$(git write-tree)"
commit="$(git commit-tree \
"$tree" \
-p "$branch" \
-p "refs/remotes/$reponame/$branch" \
-m "Merge $reponame/$branch")"
git reset -q "$commit"
done
done
# 将当前分支重置到根提交
git checkout .
# Copyright © 2020, 2022, 2023 Hraban Luyat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
- 执行脚本:
cat my-repos.txt | ~/tomono/tomono.sh --continue
-
- 这里有几个注意点:
报错信息如上,所以注释了shell脚本第二行的代码
-
-
- 默认构建的仓库名为
core
,可以修改,命令行输入进行修改:
- 默认构建的仓库名为
-
export MONOREPO_NAME=multiple-to-mono
-
-
- 因为该文件夹已经存在,根据脚本提示,添加了
--continue
指令
- 因为该文件夹已经存在,根据脚本提示,添加了
-
2.2 踩坑点
这里有个比较棘手的问题,如上本来需要迁移的仓库有两个,但是脚本执行完之后,只迁移了一个项目,对shell脚本掌握的知识比较少,查询多处资料无果,最后将my-repos.txt
文件进行了修改,多复制了一份仓库地址出来,没想到解决了🤣:
scss
git@github.com:7neves/multiple-repo1.git repo1
git@github.com:7neves/multiple-repo2.git repo2
git@github.com:7neves/multiple-repo2.git repo2
再次执行脚本:cat my-repos.txt | ~/tomono/tomono.sh --continue
,正常迁移仓库:
3. 总结
至此,迁移代码的工作结束,可以看到在monorepo仓库的分支中包含了所有提交信息: