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

migrate: switch to git-filter-repo, make temp dir creation more robust, enable changing default branch name #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions bin/meta-project-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ When the monorepo no longer needs to be maintained you can simply add it to your
const run = ({ process }) => {
const { argv } = process;
/^(-h||--help)$/.test(argv[2]) && usage();

// Load the ".meta" module.
const meta = getMetaFile();
if (!meta) process.exit(1);

const destArg = argv[2] === 'blank' ? argv[3] : argv[2];
const repoUrl = argv[3] === 'blank' ? argv[4] : argv[3];
const branch = argv[4] === 'blank' ? null : argv[4];
const branch = (argv[4] === 'blank' ? null : argv[4]) || meta.defaultBranch || "master";
(destArg && repoUrl) || usage();

// Load the ".meta" module.
const meta = getMetaFile();
if (!meta) process.exit(1);

const metaPath = getMetaFile.getFileLocation();
const dest = path.relative(path.dirname(metaPath), path.resolve(destArg));

Expand Down Expand Up @@ -93,42 +93,42 @@ const run = ({ process }) => {
console.log(`Root remote found: ${cyan(tildify(rootRepo))}`);

const monorepoDir = process.cwd();
const tempDir = path.resolve(monorepoDir, './.tmp');
console.log({ tempDir });
mkdirp.sync(tempDir);
const tempDir = fs.mkdtempSync("meta-project-migrate-");
console.log(tempDir);

function afterGitClone(err) {
if (err) throw err;

console.log(`Splitting subdirectory "${dest}" history into temporary folder "${tempDir}"`);
splitSubtree({
subdirectory: dest,
source: monorepoDir,
target: tempDir,
branch: branch,
});

process.chdir(tempDir);

console.log(`Now working in ${tempDir}`);

console.log(`Removing original remote "origin"`);

removeRemote({
remoteName: 'origin',
});

console.log(`Splitting subdirectory "${dest}" history`);

splitSubtree({
subdirectory: dest,
});


console.log(`Setting remote origin to ${repoUrl}`);

addRemote({
remoteName: 'origin',
gitUrl: repoUrl,
});

let pushBranch = branch || 'master';
console.log(`Pushing split history to new remote origin ${repoUrl} on branch ${pushBranch}`);
console.log(`Pushing split history to new remote origin ${repoUrl} on branch ${branch}`);

gitPush({
remote: 'origin',
branch: pushBranch,
branch: branch,
});

process.chdir(monorepoDir);
Expand Down
4 changes: 2 additions & 2 deletions lib/splitSubtree.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const childProcess = require('child_process');

function splitSubtree({ subdirectory, branch = 'master' }) {
function splitSubtree({ subdirectory, source, target, branch }) {
return childProcess
.execSync(`git filter-branch --prune-empty --subdirectory-filter ${subdirectory} ${branch}`)
.execSync(`git filter-repo --subdirectory-filter ${subdirectory} --refs ${branch} --source ${source} --target ${target}`)
.toString()
.trim();
}
Expand Down