|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Command line arguments |
| 4 | +repo_name="$1" |
| 5 | +remote_url="$2" |
| 6 | +subdirectory="$3" |
| 7 | + |
| 8 | +# Variables used throughout the script |
| 9 | +local_main_checkout="$repo_name-main" |
| 10 | +absorb_branch="absorb-$repo_name" |
| 11 | +current_branch="$(git rev-parse --abbrev-ref HEAD)" |
| 12 | + |
| 13 | +# Assumes this script's directory has a sibling directory called `git-filter-repo` |
| 14 | +# which contains a copy of the `git-filter-repo` script. |
| 15 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 16 | +GIT_FILTER_REPO_DIR="$(realpath "$SCRIPT_DIR/../git-filter-repo")" |
| 17 | + |
| 18 | +function usage() { |
| 19 | + echo "Usage:" |
| 20 | + echo " $0 <name> <remote-url> <subdirectory>" |
| 21 | + echo " Example: $0 worker [email protected]:codecov/worker.git apps/worker" |
| 22 | + echo "" |
| 23 | + echo "This script absorbs a repository into the monorepo in a way that (mostly)" |
| 24 | + echo "preserves its history. History will be rewritten so that the repository's" |
| 25 | + echo "contents will be, or \"will have always been\", in a subdirectory, but" |
| 26 | + echo "otherwise the authors and dates of each commit will be preserved. Operations" |
| 27 | + echo "like \`git blame\` can continue past the point where the repository was" |
| 28 | + echo "absorbed." |
| 29 | + echo "" |
| 30 | + echo "Arguments:" |
| 31 | + echo "- <name> is the name of the repository you are absorbing. Used to name the git" |
| 32 | + echo " remote and the branch used to open a PR." |
| 33 | + echo "" |
| 34 | + echo "- <remote-url> must be a valid git remote URL that you have access to. Example:" |
| 35 | + echo " \`[email protected]:codecov/worker.git\`" |
| 36 | + echo "" |
| 37 | + echo "- <subdirectory> is a path relative to the repository root where <repository>" |
| 38 | + echo " should be placed." |
| 39 | + echo "" |
| 40 | + echo "This script must be run from inside a git repository." |
| 41 | + exit 1 |
| 42 | +} |
| 43 | + |
| 44 | +if [ $# -ne 3 ] || [ "$(git rev-parse --is-inside-work-tree)" != "true" ]; then |
| 45 | + usage |
| 46 | +fi |
| 47 | + |
| 48 | +echo "Adding \`$remote_url\` as a remote named \`$repo_name\`..." |
| 49 | +git remote add $repo_name $remote_url |
| 50 | + |
| 51 | +git ls-remote $repo_name | grep main > /dev/null && branch="main" || branch="master" |
| 52 | +git fetch $repo_name $branch |
| 53 | +echo "Found repository with default branch \`$branch\`" |
| 54 | +echo "" |
| 55 | + |
| 56 | +echo "Checking out \`$repo_name/$branch\` locally as \`$local_main_checkout\`..." |
| 57 | +git checkout -b "$local_main_checkout" $repo_name/$branch |
| 58 | +echo "Done" |
| 59 | +echo "" |
| 60 | + |
| 61 | +echo "Rewriting history to put all of $repo_name's contents inside \`$subdirectory\`..." |
| 62 | +# We have to go back to the branch we started on in order to access the `git-filter-repo` |
| 63 | +# custom command. |
| 64 | +git checkout "$current_branch" |
| 65 | +python "$GIT_FILTER_REPO_DIR/git-filter-repo" --force --refs "$local_main_checkout" --to-subdirectory-filter "$subdirectory" |
| 66 | +echo "Done" |
| 67 | +echo "" |
| 68 | + |
| 69 | +echo "Merging the rewritten \`$local_main_checkout\` into our \`$absorb_branch\`" |
| 70 | +git checkout -b $absorb_branch $current_branch |
| 71 | +git merge "$local_main_checkout" --allow-unrelated-histories --no-edit |
| 72 | +echo "Done" |
| 73 | +echo "" |
| 74 | + |
| 75 | +echo "Cleaning up after ourselves..." |
| 76 | +git branch -D $local_main_checkout |
| 77 | +git remote remove $repo_name |
| 78 | +echo "Done" |
| 79 | + |
| 80 | +echo "Pushing to GitHub..." |
| 81 | +git push origin $absorb_branch |
| 82 | +echo "Done. Create a PR!" |
| 83 | + |
0 commit comments