將git倉庫從submodule轉換為subtree

東風微鳴發表於2022-12-28

三個指令碼

Alexander Mikhailian

cat .gitmodules |while read i
do
  if [[ $i == \[submodule* ]]; then
    mpath=$(echo $i | cut -d\" -f2)
    read i; read i;
    murl=$(echo $i|cut -d\  -f3)
    mcommit=`eval "git submodule status ${mpath} |cut -d\  -f2"`
    mname=$(basename $mpath)
    echo -e "$name\t$mpath\t$murl\t$mcommit"
    git submodule deinit $mpath
    git rm -r --cached $mpath
    rm -rf $mpath
    git remote add $mname $murl
    git fetch $mname
    git branch _$mname $mcommit
    git read-tree --prefix=$mpath/ -u _$mname
fi
done
git rm .gitmodules

?Warning:

下文的兩個指令碼, 寫死了 branch 是 master, 如果主分支不是 master, 需要做相應修改.

Nikita240 - Stack Overflow

?️Reference:
我對它進行了修改和改進。現在,新的subtree將指向與舊 submodule 相同的提交。以前,指令碼只是從目標儲存庫下載最新的提交,這可能會導致相容性問題。

#!/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"

GaspardP - Stack Overflow

?️Reference:

我稍微修改了一下,呼叫subtree add而不是read-tree。它將從.gitmodule中獲取submodule的列表,並提取模組的字首、名稱和網址。然後它刪除每個 submodule,並在同一位置新增它們作為subtree。它還將每個submodule的remote新增為remote,這樣你就可以透過提供它的名字而不是它的網址來更新subtree了(即git subtree pull -P Foo Foo master --squash而不是git subtree pull -P Foo https://example.com/foo.git master --squash)。

如果你想把subtree的全部歷史匯入你的版本庫,你可以去掉 --squash 引數。使用 --squash,將只匯入subtree的 HEAD 到你的版本庫。這可能是大多數人想要的。

#!/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

?️參考文件

三人行, 必有我師; 知識共享, 天下為公. 本文由東風微鳴技術部落格 EWhisper.cn 編寫.

相關文章