Created
July 15, 2017 19:28
-
-
Save andsens/053713248e23e6f968ce5735c6b7f0ae to your computer and use it in GitHub Desktop.
Merges a repo into a subdirectory of another repo (useful when making a submodule part of a parent repo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
function merge_repo_to_subdir { | |
local url=$1 | |
local commit=$2 | |
local module_path=$3 | |
if [[ -z $url || -z $commit || -z $module_path ]]; then | |
echo "Usage: merge-repo-to-subdir.sh URL BRANCH PATH" >&2 | |
exit 1 | |
fi | |
local remote_name | |
local branch_name | |
remote_name=$(basename "$url") | |
branch_name=$(basename "$url") | |
git remote add -f "$remote_name" "$url" | |
git branch "$branch_name" "$commit" | |
git filter-branch -f --index-filter \ | |
'git ls-files -s | gsed "s-\t\"*-&'${module_path//-/\\-}'/-" | | |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \ | |
git update-index --index-info && \ | |
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" \ | |
|| test ! -e "$GIT_INDEX_FILE.new"' "$branch_name" | |
git merge --allow-unrelated-histories "$branch_name" | |
git branch -D "$branch_name" | |
git remote rm "$remote_name" | |
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d | |
git reflog expire --verbose --expire=0 --all | |
git gc --prune=0 | |
} | |
merge_repo_to_subdir "$1" "$2" "$3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment