Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monorepo_add to allow incremental build #345

Merged
merged 4 commits into from Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -85,6 +85,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### [shopsys/monorepo-tools]
#### Added
- [#345 - monorepo-tools: allow incremental build of monorepo](https://github.com/shopsys/shopsys/pull/345) [@lukaso]
- [# 311 - monorepo split allows adding new package when monorepo is already tagged](https://github.com/shopsys/shopsys/pull/311)
#### Fixed
- [#281 - monorepo-tools: Fix scripts to work on OS X](https://github.com/shopsys/shopsys/pull/282) [@lukaso]
Expand Down
6 changes: 6 additions & 0 deletions packages/monorepo-tools/README.md
Expand Up @@ -108,6 +108,12 @@ Split monorepo built by `monorepo_build.sh` and push all `master` branches along

Usage: `monorepo_split.sh <remote-name>[:<subdirectory>] <remote-name>[:<subdirectory>] ...`

### [monorepo_add.sh](./monorepo_add.sh)

Add repositories to an existing monorepo from specified remotes. The remotes must be already added to your repository and fetched. Only master branch will be added from each repo.

Usage: `monorepo_add.sh <remote-name>[:<subdirectory>] <remote-name>[:<subdirectory>] ...`

### [rewrite_history_into.sh](./rewrite_history_into.sh)

Rewrite git history (even tags) so that all filepaths are in a specific subdirectory.
Expand Down
51 changes: 51 additions & 0 deletions packages/monorepo-tools/monorepo_add.sh
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# Add repositories to a monorepo from specified remotes
# You must first add the remotes by "git remote add <remote-name> <repository-url>" and fetch from them by "git fetch --all"
# It will merge master branches of the monorepo and all remotes together while keeping all current branches in monorepo intact
# If subdirectory is not specified remote name will be used instead
#
# Usage: monorepo_add.sh <remote-name>[:<subdirectory>] <remote-name>[:<subdirectory>] ...
#
# Example: monorepo_add.sh additional-repository package-gamma:packages/gamma package-delta:packages/delta

# Check provided arguments
if [ "$#" -lt "1" ]; then
echo 'Please provide at least 1 remote to be added into an existing monorepo'
echo 'Usage: monorepo_add.sh <remote-name>[:<subdirectory>] <remote-name>[:<subdirectory>] ...'
echo 'Example: monorepo_add.sh additional-repository package-gamma:packages/gamma package-delta:packages/delta'
exit
fi
# Get directory of the other scripts
MONOREPO_SCRIPT_DIR=$(dirname "$0")
# Wipe original refs (possible left-over back-up after rewriting git history)
$MONOREPO_SCRIPT_DIR/original_refs_wipe.sh
for PARAM in $@; do
# Parse parameters in format <remote-name>[:<subdirectory>]
PARAM_ARR=(${PARAM//:/ })
REMOTE=${PARAM_ARR[0]}
SUBDIRECTORY=${PARAM_ARR[1]}
if [ "$SUBDIRECTORY" == "" ]; then
SUBDIRECTORY=$REMOTE
fi
echo "Building branch 'master' of the remote '$REMOTE'"
git checkout --detach $REMOTE/master
$MONOREPO_SCRIPT_DIR/rewrite_history_into.sh $SUBDIRECTORY
MERGE_REFS="$MERGE_REFS $(git rev-parse HEAD)"
# Wipe the back-up of original history
$MONOREPO_SCRIPT_DIR/original_refs_wipe.sh
done
# Merge all master branches
COMMIT_MSG="merge multiple repositories into an existing monorepo"$'\n'$'\n'"- merged using: 'monorepo_add.sh $@'"$'\n'"- see https://github.com/shopsys/monorepo-tools"
git checkout master
echo "Merging refs: $MERGE_REFS"
git merge --no-commit -q $MERGE_REFS --allow-unrelated-histories
echo 'Resolving conflicts using trees of all parents'
for REF in $MERGE_REFS; do
# Add all files from all master branches into index
# "git read-tree" with multiple refs cannot be used as it is limited to 8 refs
git ls-tree -r $REF | git update-index --index-info
done
git commit -m "$COMMIT_MSG"
git reset --hard