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.

# 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 become
C:\cygwin64\home\<username>\repos\test\.

Override the $HOME environment variable. Cygwin will be default pass the
full windows path to ~ as the new HOME 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 used C:\users\<username> as its $HOME instead of
C:\cygwin64\home\<username>.