現状
独自の repo を持つサブモジュールに変換したいサブディレクトリrepo-old
を含むというリポジトリがあると仮定しましょう。 sub
repo-sub
repo-old
さらに、元のリポジトリを変更されたリポジトリに変換しrepo-new
、以前に存在したサブディレクトリに触れるすべてのコミットsub
が、抽出されたサブモジュール repo の対応するコミットを指すようにすることを意図していrepo-sub
ます。
さあ変えよう
これはgit filter-branch
、次の 2 段階のプロセスを使用して実現できます。
repo-old
からへのサブディレクトリの抽出repo-sub
(受け入れられた回答で既に言及されています)
repo-old
からへのサブディレクトリの置換repo-new
(適切なコミット マッピングを使用)
備考git filter-branch
: この質問は古いものであり、非推奨であり、危険である可能性があることは既に言及されています。しかし一方で、変換後に簡単に検証できる個人用リポジトリを使用すると、他の人に役立つかもしれません. ですから、注意してください!また、廃止されずに安全に使用できる他のツールがあれば教えてください。
Linux で git バージョン 2.26.2 を使用して両方のステップをどのように実現したかを以下で説明します。古いバージョンはある程度機能する可能性がありますが、テストする必要があります。
master
簡単にするために、元のリポジトリにブランチとorigin
リモートだけがある場合に限定しrepo-old
ます。temp_
また、プロセスで削除されるプレフィックス付きの一時的な git タグに依存していることにも注意してください。したがって、同様の名前のタグが既にある場合は、以下のプレフィックスを調整することをお勧めします。そして最後に、私はこれを広範囲にテストしておらず、レシピが失敗するまれなケースがあるかもしれないことに注意してください. したがって、続行する前にすべてをバックアップしてください。
次の bash スニペットは、1 つの大きなスクリプトに連結できます。このスクリプトは、リポジトリが存在する同じフォルダーで実行する必要があり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-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"