将Git子模块转换为子树

21

如何将一个 git 子模块(其远程是本地文件系统中的一个文件夹)转换为 git 子树,最好能保留子模块的提交历史记录?

2个回答

14
以下bash脚本基于Alexander Mikhailian的文章(http://mikhailian.mova.org/node/233)。我稍微修改了它,以调用subtree add而不是read-tree。它将从.gitmodule中获取子模块列表,并提取模块的前缀、名称和url。然后删除每个子模块,并将它们重新添加为子树到相同的位置。它还将每个子模块的远程添加为远程,因此您可以稍后通过提供其名称而不是URL来更新子树(例如:git subtree pull -P Foo Foo master --squash,而不是git subtree pull -P Foo https://example.com/foo.git master --squash)。
如果您想要将子树的完整历史记录导入到存储库中,则可以删除--squash参数。使用--squash,将只将子树的头导入到存储库中。这可能是大多数人想要的。
有关更多信息,请阅读atlassian的这篇文章:http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/
#!/bin/bash -x
# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i

    # extract the module's prefix
    mpath=$(echo $i | cut -d\" -f2)

    # skip two lines
    read i; read i;

    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)

    # extract the module name
    mname=$(basename $mpath)

    # deinit the module
    git submodule deinit $mpath

    # remove the module from git
    git rm -r --cached $mpath

    # remove the module from the filesystem
    rm -rf $mpath

    # commit the change
    git commit -m "Removed $mpath submodule"

    # add the remote
    git remote add -f $mname $murl

    # add the subtree
    git subtree add --prefix $mpath $mname master --squash

    # fetch the files
    git fetch $murl master
fi
done
git rm .gitmodules

这里有一个示例,展示了如何在所有分支上进行这些转换。转换代码已经稍作调整。还有一些额外的代码用于我的镜像目的——你可以忽略它或者随意使用。请访问此链接获取更多信息:https://github.com/eallik/curry-kics2-sync - Erik Kaplun
2
你可以考虑使用例如#!/bin/bash -x作为shebang,而不是回显所有命令。 - Bjoern Dahlgren
2
如果子模块的URL中缺少空格,提取子模块行的URL将无法工作。使用murl = $(echo $ i | cut -d \ = -f2 | xargs)会更安全。 - Wuvist
我理解得对吗?当将子模块转换为子树时,这会拉取最新的提交记录。因此,如果我的子模块引用了旧的提交记录以实现“兼容性原因”,那么这个脚本会破坏它吗? - Nikita240

9

亚历山大·米哈伊利安的脚本由@GaspardP修改后并不能满足我的需求。

我进行了修改并作出了改进。现在,新的子模块将指向与旧的子模块相同的提交。之前,该脚本只是从目标仓库中下载最新的提交,可能会导致兼容性问题。

https://gist.github.com/Nikita240/0c98cea8f53a15e69699cd8bc40657c4

#!/bin/bash -x
# This script will convert all your git submodules into git subtrees.
# This script ensures that your new subtrees point to the same commits as the
# old submodules did, unlike most other scripts that do this.
# THIS SCRIPT MUST BE PLACED OUTSIDE OF YOUR REPOSITORY!!!!!!!!!!
# Otherwise, the script will interfere with the git commits.
# Save the script in your home directory as `~/subtrees.sh`
# `cd` into your repository
# Run `~/subtrees.sh`
# Enjoy!

# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i

    read i

    # extract the module's prefix
    mpath=$(echo $i | grep -E "(\S+)$" -o)

    echo path: $mpath

    read i

    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)

    echo url: $murl

    # extract the module name
    mname=$(basename $mpath)

    echo name: $mname

    # extract the referenced commit
    mcommit=$(git submodule status $mpath | grep -E "\S+" -o | head -1)

    echo commit: $mcommit

    # deinit the module
    git submodule deinit $mpath

    # remove the module from git
    git rm -r --cached $mpath

    # remove the module from the filesystem
    rm -rf $mpath

    # commit the change
    git commit -m "Removed $mpath submodule at commit $mcommit"

    # add the remote
    git remote add -f $mname $murl

    # add the subtree
    git subtree add --prefix $mpath $mcommit --squash

    # commit any left over uncommited changes
    git commit -a -m "$mname cleaned up"

    # fetch the files
    git fetch $murl master

    echo
fi
done
git rm .gitmodules
git commit -a -m "Removed .gitmodules"

2
+1. 我不得不稍微调整一下这个脚本;由于某种原因,git submodule status 返回的提交名称前面有一个连字符(-)。我不得不在该行末尾添加 cut -d"-" -f2 来将其切掉。 - Jason R
这对我有用,但只有在将 keybase:// 远程更改为本地远程后才有效。不太可能其他人会遇到这个问题。 - Nathan Musoke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接