Skip to content

Commit 6e292fd

Browse files
committed
add absorb-repo.sh script
1 parent 0c7699e commit 6e292fd

File tree

6 files changed

+5108
-0
lines changed

6 files changed

+5108
-0
lines changed

tools/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory contains tools for development/maintenance of the codebase itself.
2+
It should not contain code that needs to be deployed or run in production.

tools/absorb-repo/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# `absorb-repo.sh`
2+
3+
This script absorbs a repository into the monorepo in a way that (mostly) preserves its history. History will be rewritten so that the repository's contents will be, or "will have always been", in a subdirectory, but otherwise the authors and dates of each commit will be preserved. Operations like `git blame` can continue past the point where the repository was absorbed.
4+
5+
Example usage:
6+
```
7+
$ ./absorb-repo.sh worker [email protected]:codecov/worker.git apps/worker
8+
```

tools/absorb-repo/absorb-repo.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+

tools/git-filter-repo/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009, 2018-2019
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

tools/git-filter-repo/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# `git-filter-repo`
2+
3+
Upstream project: https://github.com/newren/git-filter-repo
4+
5+
`git-filter-repo` is a replacement for the stock `git filter-branch` command
6+
recommended by [the Git project itself](https://git-scm.com/docs/git-filter-branch#_warning).
7+
8+
The copy of the core `git-filter-repo` script included here is not modified.
9+
10+
Usage example:
11+
```
12+
# Rewrite the history of `local-worker-main` so that all repository content is placed in `apps/worker`
13+
$ PATH="$PATH:." git filter-repo --force --refs local-worker-main --to-subdirectory-filter apps/worker
14+
```

0 commit comments

Comments
 (0)