Code Review Helper Function for Git in PowerShell
I wrote the following article for work after finding a particularly useful function for code review as first talked about in this article.
You can use PowerShell and Git to build a helper function for showing the different between develop and a branch under review; outputting a diff file like this which can help at least establish what needs to be reviewed:
The “code-review” function we’ll add does the following:
- Get a list of log entries in the branch not in develop
- Get a full diff between the branch and develop
- Reverses the log so it’s in ascending chronological order
- Output them to diff files
- Open them in Notepad++
I find this immensely useful for quickly checking how far a branch is away from develop and summarising the changes to merge. See below for a guide on setting this up:
Pre-requisites
- Git must be installed and available on the command line
- Notepad++ (x86) must be installed
Setting up a PowerShell Profile
Powershell profiles are scripts loaded whenever you open Powershell under a particular user. They can be used to create helper functions, alias programs etc.
If you use PoshGit you’ll already have a local Powershell Profile and can skip this step as it creates a profile for you.
- Go to your “Documents” folder in Windows Explorer (e.g: C:\Users\luke.merrett\Documents)
- If it doesn’t exist already;create a folder called “WindowsPowerShell”
- Inside that folder create a new file called “Microsoft.PowerShell_profile.ps1”
Anything added to that PS1 script will now be run when you open Powershell as your current user.
Adding the Helper Function to the Profile
Open the “Microsoft.PowerShell_profile.ps1” and append the following to it:
<#
.SYNOPSIS
Runs a Git command on the current branch that displays all commits
in ascending order that haven't been merged into develop
.EXAMPLE
code-review
# Displays differences between the current branch and origin/develop
.EXAMPLE
code-review feature/differentbasebranch
# Displays differences between the current branch and feature/differentbasebranch
.EXAMPLE
code-review 7bcedc50adfbddffd7e5918a457b5f0d807a68d1
# Displays the changes after the specified commit up to the head of the current branch
.EXAMPLE
code-review -OriginalBranch feature/differentbasebranch
# Displays differences between the current branch and feature/differentbasebranch
#>
function code-review {
param (
$OriginalBranch = "origin/develop"
)
# Need to use Invoke-Expression otherwise the parameter stops git log working correctly
Invoke-Expression "git log $OriginalBranch..head --name-status --reverse > Review_Summary.diff"
Invoke-Expression "git log $OriginalBranch..head -p --reverse > Review_Log.diff"
Invoke-Expression "git diff $OriginalBranch..head -p --reverse > Review_Diff.diff"
if (Get-Command "notepad++" -ErrorAction SilentlyContinue)
{
notepad++ Review_Summary.diff
notepad++ Review_Log.diff
notepad++ Review_Diff.diff
}
}
Prepare PowerShell
As with pretty much anything in Powershell you need to Set-ExecutionPolicy.
- Open Powershell as an Administrator
- Run “Set-ExecutionPolicy Unrestricted”
Use the Function
Now the function is ready to use and will work inside any git repository. To use it:
- Open up Powershell as your standard user
- CD into a git repository that is ahead of develop
- Type “code-review” and press enter
If you want to see more advanced examples of usage; run “Get-Help code-review” and “Get-Help code-review -examples”.