28

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!

Joe Attardi
  • 4,381
  • 3
  • 39
  • 41

2 Answers2

3

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.

etf213
  • 86
  • 5
1

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.

Patrick Fowler
  • 176
  • 4
  • 13
Doğancan Arabacı
  • 3,934
  • 2
  • 17
  • 25
  • I don't understand how these commands work. You claim "We did similar thing in past for 8-10 repositories." but when I try it does not work. 3 problems I see: 1. It doesn't replay the commits in the same way lerna import does. 2. With default merge, you cannot merge unrelated histories. The history from lerna import would be different than the feature branch from the target repo. 3. It doesn't deal with different folder structures from mono repo and target repo. The mono has project at ./packages/target and target repo hat at it's root ./ so the merge would likely not be sensible – Matt Mazzola Nov 22 '19 at 22:26
  • Another reason this technique might be useful is if you want to import multiple branches. My understanding is the lerna import currently only replaces the currently checked out branch from the path to git repo you provide, however, if there are multiple branches you're stuck and would likely to need to manually add this after wards. After more experimentation I found correcting the folder paths of target before constructing the merge and using the flag `--allow-unrelated-histories` does allow the merge to work; however, since the history is unrelated git sees all changes as conflicts – Matt Mazzola Nov 22 '19 at 22:58
  • 1
    this answer needs more clarification in order to earn more upvotes. – mesqueeb Sep 05 '20 at 22:32