Gerrit/split a submitted change
Sometime people submit huge patches that would have been better submitted as smaller patches. This page will help you discover how to split a submitted change and resubmit it properly in Gerrit. This page use a real world case which happened when we migrated from subversion to git back in March 2012, it is based on Gerrit change 3497 which introduce three different changes: a caching helper, a way to format time duration and some double quotes to single quote transformation.
The protocol is really easy, fetch the change, create a new branch and cherry pick the change on top of it. Then add only the chunks you are interested of. Repeat the same process for all parts of the change.
Get the code
Make sure you are up to date with the main repo:
git checkout master git pull --rebase
The next thing we want to do is actually fetch the code from Gerrit. The easiest to do so is to use git-review:
git:master)$ git-review -d 3497 Downloading refs/changes/97/3497/5 from gerrit into review/catrope/cachedpage Switched to branch 'review/catrope/cachedpage' git:review/catrope/cachedpage)$
Then find out the commit sha1 introduced by that change using the git log command. We simply have to output commits which makes HEAD different from master:
review/catrope/cachedpage)$ git log --oneline master..HEAD 367d8f5 Resubmit the entire SpecialCachedPage/formatDuration saga as one big com review/catrope/cachedpage)$
Now create the topic branch and apply the patch locally
git:review/catrope/cachedpage)$ git checkout master -b cachedpage Switched to a new branch 'cachedpage' git:cachedpage)$ git show --no-color 367d8f5 | git apply - $ git status # On branch cachedpage # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: includes/AutoLoader.php # modified: languages/Language.php # modified: languages/messages/MessagesEn.php # modified: languages/messages/MessagesQqq.php # modified: maintenance/language/messages.inc # modified: tests/phpunit/languages/LanguageTest.php # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # includes/CacheHelper.php # includes/actions/CachedAction.php # includes/specials/SpecialCachedPage.php no changes added to commit (use "git add" and/or "git commit -a") git:cachedpage)$
You now have to pick the chunk we are interesting in to create a new commit which be used for the "cachedpage" topic. To add a file, one would use git add as usual, git add --patch to select chunks inside a file. When even you want to skip a file, just reset it with git checkout -- FILENAME, this way they will not clutter the output of git status while you are looking for additional chunks to stage.
Once you have finished staging files and change for the topic. Review the staged difference git diff --cached and commit it. Eventually fill in the original author name by using git commit --amend --author "FirstName LastName <firstname.lastname@example.org>".
For the second topic, it is very similar. Create a new branch and apply the patch that need to be split:
git:cachedpage)$ git checkout master -b formatduration Switched to a new branch 'format duration' git:formatduration)$ git show --no-color 367d8f5 | git apply -
Now we can get rid of any code which has already been committed under the previous topic "cachedpage", to do so we will apply it as a reverse patch:
$ git show cachedpage | git apply --reverse - error: patch failed: maintenance/language/messages.inc:3935 error: maintenance/language/messages.inc: patch does not apply $
You will have to carefully look at the patch to fix that. In this case, that is because we had to manually edit a chunk to split the code between the cachedpage and the formatduration topics. git reject a patch by default, we can forces it with --reject:
git:formatduration)$ git show cachedpage | git apply --reverse --reject - Checking patch includes/AutoLoader.php... Checking patch includes/CacheHelper.php... Checking patch includes/actions/CachedAction.php... Checking patch includes/specials/SpecialCachedPage.php... Checking patch languages/messages/MessagesEn.php... Checking patch languages/messages/MessagesQqq.php... Checking patch maintenance/language/messages.inc... error: while searching for: 'logging-irc' => 'For IRC, see bug 34508. Do not change', 'feedback' => 'Feedback', 'apierrors' => 'API errors', 'cachedspecial' => 'SpecialCachedPage', ); error: patch failed: maintenance/language/messages.inc:3935 Applied patch includes/AutoLoader.php cleanly. Applied patch includes/CacheHelper.php cleanly. Applied patch includes/actions/CachedAction.php cleanly. Applied patch includes/specials/SpecialCachedPage.php cleanly. Applied patch languages/messages/MessagesEn.php cleanly. Applied patch languages/messages/MessagesQqq.php cleanly. Applying patch maintenance/language/messages.inc with 1 rejects... Hunk #1 applied cleanly. Rejected hunk #2. git:formatduration)$
Looking at the working copy status already show that the cache related files have been removed, already saving sometime. --reject generated a file named .rej
git:formatduration)$ git status # On branch formatduration # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: languages/Language.php # modified: languages/messages/MessagesEn.php # modified: maintenance/language/messages.inc # modified: tests/phpunit/languages/LanguageTest.php # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # maintenance/language/messages.inc.rej It contains a line related to cachedpage.
Then start adding the files and chunk you are interested in with git add and git add --patch. Then commit and set the original author (same as above).
This specific case only had three mixed topic. What is remaining in your working copy is the third change. So crafting a commit for it is even easier:
git checkout -b quotes master git add tests/phpunit/languages/LanguageTest.php git commit --author <SOMEONE>
The three topics can be seen with:
git:quotes)$ git log --graph --decorate --oneline --all * 5605bf3 (HEAD, quotes) change double quotes to single quotes | * 5200440 (formatduration) duration formatter, makes time in sec easy to read |/ | * e2ef042 (cachedpage) CacheHelper: facilitate caching handling on a page |/ * d859541 (gerrit/master, master) Use local context to get messages | * 367d8f5 (review/catrope/cachedpage) Resubmit the entire SpecialCachedPage/fo |/ * 458f162 Merge "[LanguageConverter] Added some cache code based on the proble <snip>
So we have the three topic branches based on master and we can now submit them all. Checkout each of them and submit them using git-review:
git:master)$ git checkout cachedpage Switched to branch 'cachedpage' git:cachedpage)$ git-review --no-rebase -f remote: Resolving deltas: 14% (2/14) remote: remote: New Changes: remote: https://gerrit.wikimedia.org/r/4628 remote: To ssh://gerrit.wikimedia.org:29418/mediawiki/core.git * [new branch] HEAD -> refs/for/master/cachedpage Switched to branch 'master' Deleted branch 'cachedpage' git:master)$ git checkout formatduration Switched to branch 'formatduration' amusso@aeriale:/srv/trunk(git:formatduration)$ git-review --no-rebase -f remote: Resolving deltas: 0% (0/12) remote: remote: New Changes: remote: https://gerrit.wikimedia.org/r/4629 remote: To ssh://gerrit.wikimedia.org:29418/mediawiki/core.git * [new branch] HEAD -> refs/for/master/formatduration Switched to branch 'master' Deleted branch 'format duration' git:master)$ git checkout quotes Switched to branch 'quotes' git:quotes)$ git-review --no-rebase -f remote: Resolving deltas: 0% (0/5) remote: remote: New Changes: remote: https://gerrit.wikimedia.org/r/4630 remote: To ssh://gerrit.wikimedia.org:29418/mediawiki/core.git * [new branch] HEAD -> refs/for/master/quotes Switched to branch 'master' Deleted branch 'quotes' git:master)$