Open Repository in SourceTree from Cygwin
SourceTree is my go-to Windows git client. It has pretty good UI and exposes
most of the more advanced features that I need (git add -p
being chief among
them).
But I still spend a lot of time in the command line via Cygwin and want the ability to open SourceTree to whatever repo I happen to be in at the time.
I don’t like the manual process of loading up SourceTree and adding a new repository bookmark. A lot of times, I don’t even care to book mark the repo: I just want to pull it up and look at some diffs or quickly add parts of a file.
Luckily, SourceTree added a few command line arguments a few versions back that allow you to open it right to a repository.
It’s a little clunky in cygwin, like most things, but with a small script it becomes very accessible.
# Open SourceTree to the file status window for the repo
# in the provided directory.
#
# stree [path]
#
# if no path is provided, the current working directory is used
# Cygwin path to the source tree executable
SOURCE_TREE="/cygdrive/c/Program Files (x86)/Atlassian/SourceTree/SourceTree.exe"
# Determine the path. If a path is provided, convert it to
# (w)indows (a)bsolute path with cygpath. Otherwise, convert
# the current directory
if [ "$1" != "" ]; then
cwd=$(cygpath -wa $1)
else
cwd=$(cygpath -wa $PWD)
fi
# Override the HOME environment variable to be the windows
# HOME instead of cygwin's $HOME. Then execute source tree
# See https://blog.sourcetreeapp.com/2014/01/29/sourcetree-for-windows-1-4-released/
# for options
HOME="$HOMEPATH" cygstart -v "$SOURCE_TREE" -f $cwd status
There’s a couple of tricks involved
- Use
cygpath -wa
to convert the Unix file paths for the directory to the absolute windows path. So~/repos/test/
will becomeC:\cygwin64\home\<username>\repos\test\
. - Override the
$HOME
environment variable. Cygwin will be default pass the full windows path to~
as the newHOME
environment variable to Source Tree. This caused problems for me when SourceTree in turn shelled out to git and git complained about the.gitconfig
. I fixed it by making sure SourceTree usedC:\users\<username>
as its$HOME
instead ofC:\cygwin64\home\<username>
.