Integrating Stash Pull Requests into Your Git Workflow

April 28, 2015

Here at MichiganLabs, we use Git for version control and Atlassian Stash as an interface to our Git repos for code review and discussion. As someone who prefers the CLI interface to Git, I keep looking for ways to ways to integrate Stash (which is primarily a web based tool) with the Git command line.

The primary feature I wanted was a quick and easy way to be able to create pull requests in Stash with a Git command.

First, install the Stash CLI tool:

$ gem install atlassian-stash<br />
$ stash configure<br />

When asked for a password, you can either enter your Stash password or leave it blank to be asked every time. I recommend not giving your password as it is stored as plaintext in ~/.stashconfig.yml.

Once you have that installed and configured, you can begin making pull requests in Stash from the command line:

$ stash pull-request target-branch @reviewer1 @reviewer2 @reviewerN<br />

That’s great, but it’s a lot of stuff to type for each pull request. We usually involve our whole team in the pull requests for our projects, so the list of reviewers doesn’t change that often. It would be nice to be able to save a default list of reviewers and target branch somewhere. To do this, we can make a custom Git command that uses Git’s built-in settings storage system.

When you run a Git command, such as “git foobar“, if the command is not built in to Git, the $PATH will be searched for an executable called git-foobar. Git then runs that executable and passes it any remaining arguments. Knowing this, we can create custom git commands simply by putting a new script in the $PATH.

Save the following as git-pr:

#!/bin/bash</p>
<p>DEFAULT_BRANCH=$(git config stash.default-branch)<br />
DEFAULT_REVIEWERS=$(git config stash.default-reviewers)</p>
<p>if [ "$#" -ne 0 ]; then<br />
 stash pull-request $@<br />
else<br />
 echo "stash pull-request $DEFAULT_BRANCH $DEFAULT_REVIEWERS"<br />
 stash pull-request $DEFAULT_BRANCH $DEFAULT_REVIEWERS<br />
fi<br />

Make it executable and move it to somewhere in your $PATH (such as /usr/bin/ for example):

$ chmod +x git-pr<br />
$ sudo mv git-pr /usr/bin/<br />

You can set up a global default configuration (stored in $HOME/.gitconfig):

$ git config --global stash.default-branch develop<br />
$ git config --global stash.default-reviewers "@reviewer1 @reviewer2 @reviewerN"<br />

This can, of course, be overridden per repo by re-running the commands without the --global flag (and the settings will be stored in repo-dir/.git/config).

To create a pull request in Stash:

$ git pr<br />

If you don’t give any additional arguments, the defaults you set in your Git config will be used. You can see these defaults at any time with:

$ git config -l<br />

The new git-pr will accept the same arguments as the original stash command line tool so you can do one-time overrides if you wish:

$ git pr master @reviewerM<br />

Thats it! Happy pull requesting!

Josh Friend
Josh Friend
Development Practice Co-Lead

Stay in the loop with our latest content!

Select the topics you’re interested to receive our new relevant content in your inbox. Don’t worry, we won’t spam you.

Growing with Glue Work
Team

Growing with Glue Work

July 21, 2021

Co-Written By Kylie Martinez and Amanda Clouser

Read more
Application Architecture with SwiftUI
Development iOS

Application Architecture with SwiftUI

June 15, 2022

An overview of mobile application system architecture using SwiftUI

Read more
MichiganLabs among INC. MAGAZINE’S BEST WORKPLACES 2020
Business Team Press Release

MichiganLabs among INC. MAGAZINE’S BEST WORKPLACES 2020

April 30, 2020

Michigan Software Labs is one of the highest-scoring businesses, with standout employee engagement.

Read more
View more articles