Skip to content

Instantly share code, notes, and snippets.

@qfox
Created January 24, 2018 00:51
Show Gist options
  • Save qfox/c32b604e56159386306a05bc16850944 to your computer and use it in GitHub Desktop.
Save qfox/c32b604e56159386306a05bc16850944 to your computer and use it in GitHub Desktop.
Import one git repo as a subdirectory to another (monorepo maker)

monorepish

How to use

cd git-monorepo
env PACKAGE_NAME=babel/babel-bridge node import | bash
const assert = require('assert');
const packageName = process.env.PACKAGE_NAME;
assert(packageName, 'Provide PACKAGE_NAME env variable with name in format: `github-user/github-name`.');
const localName = process.env.LOCAL_NAME || packageName.split('/')[1].replace(/^bem-/, '');
const gitsshUrl = `git@github.com:${packageName}.git`;
const packageSlug = packageName.toLowerCase().replace(/[^a-z0-9]+/g, '-');
const originName = `${packageSlug}--origin`;
const branches = (process.env.BRANCHES || 'master').split(',');
/*
git remote add project-a path/to/project-a
git fetch project-a
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
git remote remove project-a
*/
[
'\n# Add remote',
`git remote add ${originName} ${gitsshUrl}`,
`git fetch -qa ${originName}`,
// For each branch:
branches.map(branch => [].concat(
branch === 'master' // or HEAD?
? ([
'\n# Master branch, clean up the working dir',
`git checkout -q "${branch}"`,
`git checkout -q -- .`,
`git clean -f -d`,
])
// branch !== 'master'
: (branch = `${localName}/${branch}`,
[
`\n# Create a fresh branch "${branch}" with an empty root commit"`,
`git checkout -q --orphan "${branch}"`,
`\n# The ignore unmatch is necessary when this was a fresh repo`,
`git rm -rfq --ignore-unmatch .`,
`git commit -q --allow-empty -m "Root commit for ${branch} branch"`,
]),
`\n# For branch ${branch}`,
`git merge -s ours --allow-unrelated-histories --no-commit ${originName}/${branch}`,
`git read-tree --prefix=packages/${localName}/ -u ${originName}/${branch}`,
`git commit -q --no-verify --allow-empty -m 'Merging ${localName} to ${branch}'`
)),
'\n# Clean up',
`#git remote remove ${originName}`,
'\n\n'
].reduce((res, el) => [].concat.apply([], res.concat(el)), [])
.map(s => console.log(s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment