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
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
Save the following as
#!/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
$ 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
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 />
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!