Skip to content

Use git subtree for importing external packages #615

Closed

Description

@asharma93

I've had issues using $ lerna import to pull in external packages and with no luck, I've had to find an alternative solution. Most of the problems I've encountered are some variation of the issues already raised in this repo, along the lines of:

Error: Command failed: /bin/sh -c git am -3
fatal: git apply: bad git-diff - inconsistent old filename on line 218
error: could not build fake ancestor

Error: Command failed: /bin/sh -c git am -3
.git/rebase-apply/patch:122: trailing whitespace.

.git/rebase-apply/patch:124: trailing whitespace.

Known issues:
#432
#439

@gigabo briefly mentioned on slack that "The advantage of the method lerna import uses currently is the history is all rewritten to the target directory, so git log and git annotate work how you’d expect them to.". This is certainly a welcomed benefit, though given the buggy nature of this method, a simpler (and hopefully bug free) approach I used to get round this issue could be employed.

I had written the following bash script that utilises git's subtree cmd. This retains all of external package X's history and imports as expected into my lerna repo, with the difference being that the history isn't rewritten to the target directory as per lerna's method.

Workaround script:

if [[ $# -ne 2 ]]; then
  echo "Please provide a project name and a project path to import an existing package. Example :"
  echo "   $0 my_project path/to/my_project"
  exit 1
fi
  
project_name=$1
project_path=$2

git subtree add -P packages/$project_name $project_path master

Using the script above I was able to pull in several external packages with ease and so I wanted to share this with you guys to spark a discussion on whether we would like to see lerna employing git subtree as an alternative approach.

Activity

  1. doug-wade commented on Mar 20, 2017

    @doug-wade
    Contributor

    @asharma93 I just tried to use lerna import on a fresh lerna repo (e.g. only ran a lerna init with the latest master), and was greeted with

    dwade frontend/monorepo ‹import-build› » lerna import ../build
    Lerna v2.0.0-beta.38
    4d892f1
    About to import 81 commits from ../build into packages/build
    ? Are you sure you want to import these commits onto the current branch? Yes
    Errored while running ImportCommand.execute
    Failed to apply commit 716f365.
    Error: Command failed: git am -3
    error: sha1 information is lacking or useless (packages/build/package.json).
    error: could not build fake ancestor
    
    Rolling back to previous HEAD (commit 4d892f1).
    

    I can confirm that git subtree add -P packages/build ../build master works as intended, so I'm in favor of this change.

  2. asharma93 commented on Apr 5, 2017

    @asharma93
    Author

    If there aren't any objections I'll submit a PR.

  3. vzaidman commented on Dec 5, 2017

    @vzaidman

    any updates on this?
    i think it solves the #1058 ("issue with importing packages with merge commits") much better then the "--flatten" flag that removes the merges history whatsoever.

  4. vzaidman commented on Dec 5, 2017

    @vzaidman

    here is how adding multiple packages using git subtree looks like

    git subtree add -P packages/some-package ../src-to-some-package master
    

    image

  5. chrisdothtml commented on Sep 4, 2018

    @chrisdothtml
    Contributor

    In my experience, the subtree import doesn't update the paths in the history of the files (like lerna currently does manually), so it ends up with broken file history. Happy to be proven wrong though

  6. vzaidman commented on Sep 5, 2018

    @vzaidman

    actually it works quite fine. you can see the history with older paths which was good enough for our usage.

  7. stale commented on Dec 27, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  8. lock commented on Apr 3, 2019

    @lock

    This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  9. locked as resolved and limited conversation to collaborators on Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions