MCUdude / MCUdude_corefiles

Subtree for Arduino core files used with MightyCore, MegaCore, MiniCore and MajorCore
19 stars 4 forks source link

How on earth do I update this subtree? #5

Closed MCUdude closed 6 years ago

MCUdude commented 6 years ago

I've committed a change to Tone.cpp, and I'm trying to update MCUdude_corefiles (which is a subtree within MegaCore) without luck. I wrote a simple guide for myself when I worked with this half a year ago, but it somewhat fails. Do you remember how this was done @per1234?

Guide:

# MegaCore must be up to date/identical to the one upstream 
(cd into MegaCore folder)

# Add subtree (not sure if this should be done every time?)
$ git remote add -f avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git

# Fetch
$ git fetch https://github.com/MCUdude/MCUdude_corefiles.git master # --squash
$ git subtree add --prefix avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git master --squash

# Update subtree
$ git subtree pull --prefix avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git master --squash

Actual output:

$ git remote add -f avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git
fatal: remote avr/cores/MCUdude_corefiles already exists.

$ git fetch https://github.com/MCUdude/MCUdude_corefiles.git master # --squash
From https://github.com/MCUdude/MCUdude_corefiles
 * branch            master     -> FETCH_HEAD

$ git subtree add --prefix avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git master --squash
prefix 'avr/cores/MCUdude_corefiles' already exists.

$ git subtree pull --prefix avr/cores/MCUdude_corefiles https://github.com/MCUdude/MCUdude_corefiles.git master --squash
From https://github.com/MCUdude/MCUdude_corefiles
 * branch            master     -> FETCH_HEAD
Can't squash-merge: 'avr/cores/MCUdude_corefiles' was never added.
per1234 commented 6 years ago

It's strange because I have specific recipes (based on the official documentation) saved that I use to create subtrees and update them that have always worked in the past but mine also don't work.

I was able to do it like this:

# create a working branch just to avoid messing up master if something goes wrong
git checkout -b update-core-subtree master
# create a remote for the core repository and fetch from it
git remote add -f MCUdude_corefiles-remote https://github.com/MCUdude/MCUdude_corefiles.git
# create a branch with the contents of the core repository's master branch
git branch MCUdude_corefiles-branch MCUdude_corefiles-remote/master
# update the core subtree
git merge --squash -s subtree -Xsubtree=avr/cores/MCUdude_corefiles --allow-unrelated-histories -m 'Update core subtree to 3aa8d81' -m 'Fix tone() for ATmega64/128' -m 'https://github.com/MCUdude/MCUdude_corefiles/tree/3aa8d810b24952efc870483dfa2af609a50aa720' MCUdude_corefiles-branch
# the above fails with "Automatic merge failed; fix conflicts and then commit the result."
# I had to fix the merge conflicts in README.md and Tone.cpp
# add the edits that fix the merge conflicts
git add .
# finally commit the updates to the subtree
git commit -m 'Update core subtree to 3aa8d81' -m 'Fix tone() for ATmega64/128' -m 'https://github.com/MCUdude/MCUdude_corefiles/tree/3aa8d810b24952efc870483dfa2af609a50aa720'
git checkout master
# rebase the commit from the working branch to master
git rebase update-core-subtree

The difference from my standard procedure was the -Xsubtree=avr/cores/MCUdude_corefiles was required and also the merge conflict never has happened before. Even though that gets the job done it really doesn't make sense that there was a merge conflict.

I'm going to work on it some more and will let you know as soon as I find a better solution.

per1234 commented 6 years ago

OK, I finally got it:

# create a working branch just to avoid messing up master if something goes wrong
git checkout -b update-core-subtree master
# create a remote for the core repository and fetch from it
git remote add -f MCUdude_corefiles-remote https://github.com/MCUdude/MCUdude_corefiles.git
# create a branch with the contents of the core repository's master branch
git branch MCUdude_corefiles-branch MCUdude_corefiles-remote/master
# update the core subtree
git merge --squash -s recursive -Xsubtree=avr/cores/MCUdude_corefiles -Xtheirs --allow-unrelated-histories --no-commit MCUdude_corefiles-branch
# commit the updates to the subtree
git commit -m 'Update core subtree to 3aa8d81' -m 'Fix tone() for ATmega64/128' -m 'https://github.com/MCUdude/MCUdude_corefiles/tree/3aa8d810b24952efc870483dfa2af609a50aa720'
git checkout master
# rebase the commit from the working branch to master
git rebase update-core-subtree
# delete the working branch
git branch -d update-core-subtree
# push the commit to GitHub
git push

That automatically solves the merge conflict.

I'm happy to submit a PR if you prefer.

I also verified that, after the above commands, my standard subtree update recipe works correctly without needing to specify the prefix or dealing with any merge conflicts (not even automatically resolved ones). It seems like something got glitched with the subtree information in the MegaCore repository and after successful update it was repaired.

Here's my subtree update recipe for future reference (not needed for this update as that's handled by the above commands):

# create a working branch just to avoid messing up master if something goes wrong
git checkout -b update-core-subtree master
# create a remote for the core repository and fetch from it
git remote add -f MCUdude_corefiles-remote https://github.com/MCUdude/MCUdude_corefiles.git
# create a branch with the contents of the core repository's master branch
git branch MCUdude_corefiles-branch MCUdude_corefiles-remote/master
# update the core subtree
git merge --squash -s subtree --allow-unrelated-histories --no-commit MCUdude_corefiles-branch
# commit the updates to the subtree
git commit -m 'Update core subtree to {latest commit short hash}' -m '{summary of changes since last update}' -m 'https://github.com/MCUdude/MCUdude_corefiles/tree/{latest commit hash}'
git checkout master
# rebase the commit from the working branch to master
git rebase update-core-subtree
# delete the working branch
git branch -d update-core-subtree
# push the commit to GitHub
git push

The only difference between the two is that instead of using the recursive strategy with the -Xsubtree=<path> option, it uses the standard subtree strategy.

MCUdude commented 6 years ago

Thanks for the cookbook instructions! Updating MegaCore went smoothly 👍

per1234 commented 6 years ago

Not a big deal, but I noticed there might have been some confusion about my subtree update "recipe" above from this commit: https://github.com/MCUdude/MegaCore/commit/46fc7eac6a8d12cf6a9a6a89b7f29eaf36249253 The things I put in braces like {latest commit short hash} were meant to be manually replaced. They were just notes to myself to give a standardized commit format for subtree updates. I should have been clear about that.

I'm sure it would be easy enough to create a script that automatically fills those values. There's probably one of them out there on the internet.

MCUdude commented 6 years ago

Yep, I understood that just after pushing the commit.. I'll try to make sure it don't happened again 😉

MCUdude commented 3 years ago

@per1234 sorry for bringing this thread back up, but I'm having a real hard time updating the MCUdude_corefiles subtree in MegaCore. I'm following your procedure, but strangely, there is nothing to be committed. Do you have any idea why?

$ git checkout -b update-core-subtree master
Switched to a new branch 'update-core-subtree'

$ git remote add -f MCUdude_corefiles-remote https://github.com/MCUdude/MCUdude_corefiles.git
fatal: remote MCUdude_corefiles-remote already exists.

$ git branch MCUdude_corefiles-branch MCUdude_corefiles-remote/master
Branch 'MCUdude_corefiles-branch' set up to track remote branch 'master' from 'MCUdude_corefiles-remote'.

$ git merge --squash -s subtree --allow-unrelated-histories --no-commit MCUdude_corefiles-branch
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

$ git commit -m "Update corefiles"
On branch update-core-subtree
nothing to commit, working tree clean

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git rebase update-core-subtree
Current branch master is up to date.

$ git branch -d update-core-subtree
Deleted branch update-core-subtree (was 972faf75).

$ git push
Everything up-to-date