grafting-monorepos icon indicating copy to clipboard operation
grafting-monorepos copied to clipboard

Activity 3: Graft a repository

Open selkins13 opened this issue 4 years ago • 5 comments

Duration: 20 minutes

Grafting commands

  • Step 1: Clean your repository
    • LFS
    • Unneeded or unwanted files
    • Add dependencies to dependency management
    • Remove independent components
    • Remove any binaries
    • Review automation in the repository
  • Step 2: Create a new repository
  • Step 3: Prepare for grafting
    • Communicate properly to the affected teams
    • Merge all in progress work
    • If something is not merged, it will not be moved
    • Call GitHub Professional Services if things go south
  • Step 4: Delete your history
# Delete the git folder that contains git objects
rm -rf .git

# Initialize a new history
git init

# Set the new repository
git remote add origin [email protected]:githubuniverseworkshops/grafting-repo.git
  • Step 5: Write a commit referencing to the previous repository as your first commit in the new repository
# Add all files to the stage
git add --all

# Add changes to history
git commit -m "Previous repo can be found on https://github.com/torvalds/linux"

# Submit your changes to upstream
git push --set-upstream origin main

Working with the new repository

To preserve the history while working with the new repository, follow the grafting command:

# Fetch the old history
git fetch [email protected]:torvalds/linux.git

# See you only have one commit in it
git log --oneline

# See the commits we are replacing
git rev-parse --short HEAD
git rev-parse --short FETCH_HEAD

# Perform the grafting operation replacing HEAD with FETCH_HEAD
git replace HEAD FETCH_HEAD

Check that all the changes that have happened to the repository are local and don't get pushed when new code goes to the repository:

# Check that the new commit goes to the right repo
# Modify a file
echo "Test" > test.txt
git add --all
git commit -m "Adding a test commit"

# Check that you can navigate the history
git log --oneline | head -n 10

# Push the change and see the number of commits is still 2
git push

Analysis after grafting

Re-run the command from Activity 2 to analyze the grafted repository.

Stats of repo size: git-sizer

  1. Download the corresponding compiled version of git-sizer.

Optionally you can install git-sizer using Homebrew if you are on Mac.

  1. Run the tool from the root of the repository to analyze:
/path/to/git-sizer --verbose

Find files that should be in LFS: git-find-lfs-extensions

  1. Checkout the grafting-monorepos repository
  2. Run the tool from the root of the repository to analyze:
/path/to/grafting-monorepos/scripts/git-find-lfs-extensions

Print directories with the number of files contained: git-find-dirs-many-files

  1. Checkout the grafting-monorepos repository
  2. Run the tool from the root of the repository to analyze:
/path/to/grafting-monorepos/scripts/git-find-dirs-many-files

Find dirs that should not be committed: git-find-dirs-unwanted

  1. Checkout the grafting-monorepos repository
  2. Run the tool from the root of the repository to analyze:
/path/to/grafting-monorepos/scripts/git-find-dirs-unwanted | head -n 15            

Analyze the repository: git-filter-repo --analyze

  1. Clone the git-filter-repo tool
  2. Execute the tool from the linux repository
/path/to/git-filter-repo/git-filter-repo --analyze

For examples and more information, please see README.md -> Activity 3.

selkins13 avatar Dec 04 '20 17:12 selkins13

It sounds like that there is the assumption that everything is merged into the main branch but what about existing bug-fix/release branches which still need to be maintained? 🚀

TestStefan avatar Dec 11 '20 19:12 TestStefan

@TestStefan Very good question! This approach only covers merged changes. Unmerged branches need a special treatment that is outside the scope of this workshop. But we can help with that in a Professional Services session.

larsxschneider avatar Dec 11 '20 19:12 larsxschneider

@TestStefan Very good question! This approach only covers merged changes. Unmerged branches need a special treatment that is outside the scope of this workshop. But we can help with that in a Professional Services session.

I guess you need to cleanup all active branches and create in the new grafted repository these branches as well and link them back to the archive repository.

TestStefan avatar Dec 11 '20 19:12 TestStefan

After run the command "git push --set-upstream origin main" I got the fellowing error(I am using windows 10) [email protected]: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Any suggestion to fix it? Thanks

neilwang0913 avatar Dec 12 '20 21:12 neilwang0913

Any clear instruction for step 1: Clean your repository: how to make it ? LFS Unneeded or unwanted files Add dependencies to dependency management Remove independent components Remove any binaries Review automation in the repository

If you could provide a video demo using a public example?

Many thanks.

neilwang0913 avatar Dec 12 '20 22:12 neilwang0913