Skip to content

Instantly share code, notes, and snippets.

@andsens
Created July 15, 2017 19:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andsens/053713248e23e6f968ce5735c6b7f0ae to your computer and use it in GitHub Desktop.
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)
#!/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