Eclipse CDT has just had all its source code reformatted. That means I just pushed a massive set of commits with a huge git shortstat:
$ git diff --shortstat 3cf0297769..HEAD 10806 files changed, 697885 insertions(+), 593225 deletions(-)
That also means that any commit/patch you have on CDT before the reformat will almost certainly not apply cleanly.
Therefore I have created this guide for you to reformat your commit and make it easier for you to submit your updated patch to gerrit.
To be able to rebase your commit onto the current master you need to create a version of it with the new formatting. At a high-level the steps we are going to do are:
- Reformat your commit with CDT’s new coding style
- Create a commit against the pre-formatted CDT that has only the files your commit changed formatted – but without your other changes
The diff between 1 and 2 above is an updated version of the diff for your real change.
Step-by-step this breaks down as:
Pre-requisites
- Modern bash shell with sed, awk, grep, git, python, etc
- I am using Ubuntu 18.04 LTS and python 2.7
- Eclipse EASE with Py4J Python language support
- p2 site is http://download.eclipse.org/ease/update/release
- Install the following features
- EASE Language Support -> EASE Py4J Support (Incubation)
- EASE Modules -> Ease Modules (Incubation)
- I am using EASE 0.6.0
- Eclipse workspace setup for CDT development, follow the standard setup on the wiki and perform these extra steps:
- Import releng/scripts/formattersettings.xml Java formatter settings and set active profile to CDT
- Import releng/scripts/cleanupsettings.xml Java Clean Up settings and set active profile to CDT
- In Clean Up preferences uncheck the “Show profile selection dialog for the ‘Source > Clean Up’ Action”
- Copy releng/scripts/cleanup.py somewhere outside of your gitroot so you can access it as the branches change later
- Set ECLIPSE environment variable to path to eclipse on your machine to make some of the commands below easier to copy/paste
Step 1: Checkout the commit to rebase
First step is to checkout in CDT repo the commit you want to rebase onto current master. For example if you are updating a commit in gerrit, you could press Download in the top right and copy/paste the Checkout command:
Step 2: Reformat commit to new style
Start by creating a new branch to work on:
git checkout -b commit_to_rebase
git rebase 35996a5c5ca5c254959ba48241eaada6dbf8628d
Reformat the code to current standard using the following steps:
Step 2a: Close open editors in Eclipse
The EASE script will run on all open editors, so make sure to start with no open editors.
Step 2b: Open all the Java files modified in your commit
git diff-tree --no-commit-id --name-only -r commit_to_rebase -- *.java | xargs $ECLIPSE
Step 2c: Run cleanup.py
With Eclipse EASE run cleanup.py:
- Create a new Run Launch Configuration of type EASE Script
- In Script Source browse the filesystem to where you saved cleanup.py earlier
- Ensure Execution Engine is set to Python (Py4J)
- Press Run
Each open editor will have the clean up actions run, and then be closed.
Step 2d: Remove trailing whitespace in files
CDT’s code standards require trailing whitespace to be removed from all files marked with remove trailing whitespace in .gitattributes. To do this, run this bit of shell:
git show master:.gitattributes | awk '/# remove trailing whitespace/{getline; print $1}' | while read i ; do echo "Removing trailing whitespace on $i files" git diff-tree --no-commit-id --name-only -r commit_to_rebase -- "$i" | xargs --no-run-if-empty sed -i 's/[ \t]*$//' done
Step 2e: Save/commit cleanup changes
git add -u git commit --amend --reuse-message=HEAD
Step 3: Create a base commit to compare against
Step 3a: Create a new branch
Start by checking out to a new branch the same commit as above with the formatter settings
git checkout 35996a5c5ca5c254959ba48241eaada6dbf8628d -b commit_to_format
Step 3b: Cleanup the modified files
Repeat Step 2a to Step 2d above to cleanup the files.
Step 3c: Save/commit cleanup changes
git add -u git commit -m"formatted files"
Step 4: Create a new commit that can be cherry-picked to master
Step 4a: Diff and apply the change
git diff commit_to_format..commit_to_rebase | git apply
Step 4b: Commit the change
git add -u git commit --reuse-message=commit_to_rebase
Step 5: Cherry-pick the new commit onto master
git checkout master git cherry-pick commit_to_format
Step 6: Push commit to gerrit
Push your commit now that it is ready, if you are using gerrit you can do:
git push origin master:refs/for/master
Step 7: Clean up your branches
Once you are done make sure to remove the branches you don’t need anymore.
git branch -D commit_to_rebase git branch -D commit_to_format
All done!
And that is it. Hopefully you have now recreated your patch with just a few automated steps, instead of a painful manual copy+paste job. These instructions are also in the CDT repo in releng/scripts/rebase_helper.sh.
This set of steps shows that there are still some more automatable things that could be done. In particular being able to run Code Clean Ups from the command line in Eclipse would be really nice. Eclipse’s Java code formatter can be run from the command line.
And finally, join in on the cdt-dev mailing list!