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!

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.

Quickly Prototyping a Ktor HTTP API
Development Web

Quickly Prototyping a Ktor HTTP API

August 18, 2022

Whether it’s needing a quick REST API for a personal project, or quickly prototyping a mockup for a client, I like to look for web server frameworks that help me get up and running with minimal configuration and are easy to use. I recently converted a personal project’s API from an Express web server to a Ktor web server and it felt like a breeze. I’ll share below an example of what I found and how easy it is to get a Ktor server up and running.

Read more
Development Practice Lead, Josh Friend on What's Possible in Technology
Business

Development Practice Lead, Josh Friend on What's Possible in Technology

December 12, 2019

As the Development Practice Lead at Michigan Software Labs, I’ve had the unique opportunity to have a small role in most of the digital products that we develop for our clients. This has given me the perspective that nearly anything is possible when it comes to what a client wants to develop - technology is rarely the barrier stopping progress.

Read more
“Learning To Code” Actually Means Different Things To Different People...And That’s Okay
Development

“Learning To Code” Actually Means Different Things To Different People...And That’s Okay

October 15, 2020

Depending on whom it is coming from, the phrase “I’d like to learn how to code” can mean wildly different things. To help shed light on the subject, I will attempt to put never-before coders into two distinct categories.

Read more
View more articles