Skip to content

Instantly share code, notes, and snippets.

@olejorgenb
Forked from andsens/merge-repo-to-subdir.sh
Created July 16, 2017 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olejorgenb/428e1d245c345f73d9f2bc5295b6835f to your computer and use it in GitHub Desktop.
Save olejorgenb/428e1d245c345f73d9f2bc5295b6835f 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