现状
假设我们有一个名为 repo-old 的存储库,其中包含一个子目录 sub,我们希望将其转换为具有自己的 repo @ 的子模块 987654324@.
还打算将原始 repo repo-old 转换为修改后的 repo repo-new,其中所有涉及先前存在的子目录 sub 的提交现在应指向我们提取的子模块 repo repo-sub 的相应提交.
让我们改变
可以在git filter-branch 的帮助下分两步实现:
- 从
repo-old 到repo-sub 的子目录提取(已在接受的answer 中提及)
- 从
repo-old 到repo-new 的子目录替换(使用正确的提交映射)
备注:我知道这个问题已经过时了,并且已经提到 git filter-branch 有点过时并且可能很危险。但另一方面,它可能会帮助其他人使用转换后易于验证的个人存储库。所以要警告!如果有任何其他工具可以做同样的事情而不会被弃用并且可以安全使用,请告诉我!
我将解释我是如何使用 git 版本 2.26.2 在 linux 上实现这两个步骤的。旧版本可能会在一定程度上起作用,但需要进行测试。
为了简单起见,我将自己限制在原始存储库repo-old 中只有一个master 分支和一个origin 远程的情况。另请注意,我依赖带有前缀 temp_ 的临时 git 标签,这些标签将在此过程中被删除。因此,如果已经有类似名称的标签,您可能需要调整下面的前缀。最后请注意,我没有对此进行广泛的测试,并且可能存在配方失败的极端情况。所以请在继续之前备份所有内容!
以下 bash sn-ps 可以连接成一个大脚本,然后应该在 repo repo-org 所在的同一文件夹中执行该脚本。不建议将所有内容直接复制粘贴到命令窗口中(即使我已经成功测试过)!
0。准备
变量
# Root directory where repo-org lives
# and a temporary location for git filter-branch
root="$PWD"
temp='/dev/shm/tmp'
# The old repository and the subdirectory we'd like to extract
repo_old="$root/repo-old"
repo_old_directory='sub'
# The new submodule repository, its url
# and a hash map folder which will be populated
# and later used in the filter script below
repo_sub="$root/repo-sub"
repo_sub_url='https://github.com/somewhere/repo-sub.git'
repo_sub_hashmap="$root/repo-sub.map"
# The new modified repository, its url
# and a filter script which is created as heredoc below
repo_new="$root/repo-new"
repo_new_url='https://github.com/somewhere/repo-new.git'
repo_new_filter="$root/repo-new.sh"
过滤脚本
# The index filter script which converts our subdirectory into a submodule
cat << EOF > "$repo_new_filter"
#!/bin/bash
# Submodule hash map function
sub ()
{
local old_commit=\$(git rev-list -1 \$1 -- '$repo_old_directory')
if [ ! -z "\$old_commit" ]
then
echo \$(cat "$repo_sub_hashmap/\$old_commit")
fi
}
# Submodule config
SUB_COMMIT=\$(sub \$GIT_COMMIT)
SUB_DIR='$repo_old_directory'
SUB_URL='$repo_sub_url'
# Submodule replacement
if [ ! -z "\$SUB_COMMIT" ]
then
touch '.gitmodules'
git config --file='.gitmodules' "submodule.\$SUB_DIR.path" "\$SUB_DIR"
git config --file='.gitmodules' "submodule.\$SUB_DIR.url" "\$SUB_URL"
git config --file='.gitmodules' "submodule.\$SUB_DIR.branch" 'master'
git add '.gitmodules'
git rm --cached -qrf "\$SUB_DIR"
git update-index --add --cacheinfo 160000 \$SUB_COMMIT "\$SUB_DIR"
fi
EOF
chmod +x "$repo_new_filter"
1。子目录提取
cd "$root"
# Create a new clone for our new submodule repo
git clone "$repo_old" "$repo_sub"
# Enter the new submodule repo
cd "$repo_sub"
# Remove the old origin remote
git remote remove origin
# Loop over all commits and create temporary tags
for commit in $(git rev-list --all)
do
git tag "temp_$commit" $commit
done
# Extract the subdirectory and slice commits
mkdir -p "$temp"
git filter-branch --subdirectory-filter "$repo_old_directory" \
--tag-name-filter 'cat' \
--prune-empty --force -d "$temp" -- --all
# Populate hash map folder from our previously created tag names
mkdir -p "$repo_sub_hashmap"
for tag in $(git tag | grep "^temp_")
do
old_commit=${tag#'temp_'}
sub_commit=$(git rev-list -1 $tag)
echo $sub_commit > "$repo_sub_hashmap/$old_commit"
done
git tag | grep "^temp_" | xargs -d '\n' git tag -d 2>&1 > /dev/null
# Add the new url for this repository (and e.g. push)
git remote add origin "$repo_sub_url"
# git push -u origin master
2。子目录替换
cd "$root"
# Create a clone for our modified repo
git clone "$repo_old" "$repo_new"
# Enter the new modified repo
cd "$repo_new"
# Remove the old origin remote
git remote remove origin
# Replace the subdirectory and map all sliced submodule commits using
# the filter script from above
mkdir -p "$temp"
git filter-branch --index-filter "$repo_new_filter" \
--tag-name-filter 'cat' --force -d "$temp" -- --all
# Add the new url for this repository (and e.g. push)
git remote add origin "$repo_new_url"
# git push -u origin master
# Cleanup (commented for safety reasons)
# rm -rf "$repo_sub_hashmap"
# rm -f "$repo_new_filter"
备注:如果新创建的 repo repo-new 在 git submodule update --init 期间挂起,则尝试以递归方式重新克隆存储库一次:
cd "$root"
# Clone the new modified repo recursively
git clone --recursive "$repo_new" "$repo_new-tmp"
# Now use the newly cloned one
mv "$repo_new" "$repo_new-bak"
mv "$repo_new-tmp" "$repo_new"
# Cleanup (commented for safety reasons)
# rm -rf "$repo_new-bak"