Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to "refresh" an imported repository with Lerna?

I am involved in a project with two separate repositories that we will soon be combining into a monorepo. Lerna's import command will be very helpful in this regard, so we will keep the projects' histories.

However, there are currently some in-progress feature branches in the original repositories that likely won't be ready when we move to the monorepo. It's my understanding that lerna import will only pull in the currently checked out branch from the source repo - is that correct?

So I was wondering if there was a way to do the import again, but only pull in commits that have been made since the last import?

That way, the teams working on the feature branches can merge to the develop branch once they're ready and we can bring that over into the monorepo.

Alternatively, are there strategies out there for dealing with this scenario?

Or am I going to have to wait until everything is merged to develop before doing the lerna import?

Thanks!

like image 848
Joe Attardi Avatar asked Apr 19 '18 21:04

Joe Attardi


People also ask

What is monorepo application?

In a single, monolithic repository, also known as a monorepo, you keep all your application and microservice code in the same source code repository (usually Git). Typically, teams split the code of various app components into subfolders and use Git workflow for new features or bug fixes.

What does lerna Import do?

Existing packages You can use lerna import <package> to transfer an existing package into your Lerna repository; this command will preserve the commit history. lerna import <package> takes a local path rather than a URL. In this case you will need to have the repo you wish to link to on your file system.

Who uses Monorepos?

Google, Facebook, Microsoft, Uber, Airbnb, and Twitter all employ very large monorepos with varying strategies to scale build systems and version control software with a large volume of code and daily changes.

What is monorepo react?

A monorepo is simply the practice of placing multiple different projects that are related in some way into the same repository. The biggest benefit is that you do not need to worry about version mismatch issues between the different pieces of your project.


2 Answers

Using the answer by @Doğancan Arabacı and comment by @Matt Mazzola. I was able to do this for myself, but I've added my own answer with a few more details to try and give a clearer explanation.

I also faced this problem as lerna import will only allow you to import once if the directory exists you can’t import. See code here.

The lerna import command takes all the commits from your original repository, reverses and replays them. However there’s no way to replay these from when branches diverge (as you might with the git rebase --onto command). See here I get the feeling that you could possibly achieve it using git rebase or using a similar technique to pick out where branches diverge to extend the lerna import command. I also get the feeling that might get messy or take while so the simple way that currently exists is:

For each branch you wish to import:

From the original repo (referred to as original:

  • Checkout and pull the branch you wish to import
  • Cut a new branch from the branch you wish to import: git checkout -b lerna-export
  • Move everything into a directory where it will be imported to e.g. packages/original something like: mkdir packages && mkdir packages/original
  • Move all you files inside the new directory: git mv -k * ./packages/original - you may have to copy over any files that aren’t picked

Then from the Lerna repo:

  • Add the original repo as a remote git remote add original ###url of repo###
  • Switch to the branch you want to import to git checkout -b orignal-import
  • Merge the branch from original to lerna: git merge original/lerna-export --allow-unrelated-histories
  • Fix any conflicts if you have any
  • Push to the Lerna branch git push

After all branches imported, you may want to remove the second remote once all the branches are imported: git remove rm original

I had some issues with the security on our BitBucket instance as I was pushing commits by other authors so I had to rewrite the git history with git filter-branch, but that doesn’t seem totally related to the question to provide details of it.

like image 167
etf213 Avatar answered Sep 28 '22 16:09

etf213


I'm not sure what lerna is doing underhood but there is manual way of doing it with git. We did similar thing in past for 8-10 repositories.

Let's assume we have MonoRepo and TargetRepo

  1. Go to MonoRepo
  2. git remote add target
  3. git checkout -b feature1
  4. git merge target/feature1-branch-on-target
  5. repeat steps 3 and 4 for all desired branches.
  6. profit

You can repeat steps 3-4 whenever you want, after a few commits, do everything at one day and move to mono repo etc.

like image 37
Doğancan Arabacı Avatar answered Sep 28 '22 14:09

Doğancan Arabacı