Pre-Commit Code Review with Git and Swarm
Swarm, Perforce's social coding tool, has always been a great tool for reviewing changes. I use it all the time when reviewing changes after someone pushes to our Git Fusion project. New for 2014, Swarm lets me review changes from Git before those changes are committed to the project.
Pushing to a Review Branch
Pushing a change for pre-commit review is simple: push to a review branch.
For example, I work in Git, in task branch matrix2. I just finished a fix I would like reviewed before pushing to main:
$ git diff --stat matrix2 master bin/p4gf_copy_to_p4.py | 33 --------------------------------- bin/p4gf_g2p_matrix2.py | 23 +++++------------------ bin/p4gf_g2p_matrix2_row_decider.py | 23 +++++------------------ 3 files changed, 10 insertions(+), 69 deletions(-)
To start the review, without touching master, push the work to a new review branch: review/destination-branch/new.
$ git push origin matrix2:review/master/new Counting objects: 11, done. Delta compression using up to 24 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 1.76 KiB, done. Total 6 (delta 5), reused 0 (delta 0) remote: Perforce: 100% (1870/1870) Loading commit tree into memory... remote: Perforce: 100% (1870/1870) Finding child commits... remote: Perforce: Running git fast-export... remote: Perforce: 100% (2/2) Checking commits... remote: Processing will continue even if connection is closed. remote: Perforce: 100% (2/2) Copying changelists... remote: Perforce: Swarm review assigned: review/master/774355 remote: remote: Perforce: Submitting new Git commit objects to Perforce: 3 To email@example.com:gfmain * [new branch] matrix2 -> review/master/new
You will notice that Git Fusion assigns a review number 774355 to this new review. It also sends out a review notification email:
Follow the link in that email, or browse the activity stream in Swarm. Swarm has opened a new review for this change. If you look carefully, "shelved into" is a hint that this is a pre-commit review. Reviewers get a chance to find and fix mistakes before this change lands in main.
Committing Approved Changes
When you are ready to submit to main, you have two options:
- Click "Approve and Commit," just like any other Swarm review.
- git push to destination branch.
Swarm users will probably prefer "Approve and Commit", which submits the shelved changes to the destination.
Git users will probably prefer git push. I do. Performing my own push lets me rebase my work into a nice linear history, then push that to master:
$ git checkout master Switched to branch 'master' $ git merge --ff-only matrix2 Updating b0e8082..81d5a4e Fast-forward bin/p4gf_copy_to_p4.py | 33 +++++++++++++++++++++++++++++++++ bin/p4gf_g2p_matrix2.py | 23 ++++++++++++++++++----- bin/p4gf_g2p_matrix2_row_decider.py | 23 ++++++++++++++++++----- 3 files changed, 69 insertions(+), 10 deletions(-) $ git push origin master Total 0 (delta 0), reused 0 (delta 0) remote: Perforce: 100% (1/1) Loading commit tree into memory... remote: Perforce: 100% (2/2) Finding child commits... remote: Perforce: Running git fast-export... remote: Perforce: 100% (1/1) Checking commits... remote: Processing will continue even if connection is closed. remote: Perforce: 100% (1/1) Copying changelists... remote: Perforce: Submitting new Git commit objects to Perforce: 2 To firstname.lastname@example.org:gfmain b0e8082..81d5a4e master -> master
At that point, I could stop and consider my work done, or I could associate my push with the review.
Associating a Push with a Review
Telling Swarm which pushed commit goes with a review helps my coworkers (and myself two weeks from now) track down who reviewed this change.
To associate a push with a review, Swarm has a new "Already Committed..." option:
This brings up a changelist picker where you can find the commit you just pushed:
Amending a Review
Swarm also adds the ability to amend an existing review, from Git. A Git user can repeatedly push to the same review branch, updating the changes shelved for review, until that change is finally ready to be merged into its destination branch. I will save that for a future article.