Skip to content

Instantly share code, notes, and snippets.

@jdforsythe
Created October 28, 2016 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdforsythe/dbd7de722b99d1279fb323607523ee56 to your computer and use it in GitHub Desktop.
Save jdforsythe/dbd7de722b99d1279fb323607523ee56 to your computer and use it in GitHub Desktop.
SourceTree Open on GitHub Custom Actions
ruby d:\dev\sh\sourcetree\sourcetree-open-on-github.rb %1
#!/usr/bin/env ruby
## Setup the following Custom Actions in SourceTree preferences in order
## to easily open a FILE or commit SHA in a browser on github.com
# No need to checkmark "Open in Separate Window" or "Show in Full Output"
# Menu Caption: Open SHA on github.com
# Script to run: ~/path/to/sourcetree-open-on-github.bat
# Parameters: $SHA
# Menu Caption: Open File on github.com
# Script to run: ~/path/to/sourcetree-open-on-github.bat
# Parameters: $FILE
# Menu Caption: Open REPO on github.com
# Script to run: ~/path/to/sourcetree-open-on-github.bat
# Parameters: $REPO
# set this to the base path for your repos - it will do a beginning-of-string match to see
# if a directory was passed and assume you want to open a file
# e.g.
# Windows: 'C:\Users'
# Mac: '/Users'
# Linux: '/home'
dev_path_base = 'D:\dev'
if ARGV.empty?
puts "Expecting a $SHA, $FILE, or $REPO. See the comments in this script for details"
exit 1
end
# Get the input param
script_input_param = ARGV[0]
# Found out what the github.com remote url is
remote_url = `git config --get remote.origin.url`.chomp
# Error Checking
if remote_url == ''
puts 'Error: The remote url for this repository has not been set'
exit 1
elsif !remote_url.include? 'github.com'
puts 'Error: This script is only setup for github.com repos'
exit 1
end
puts "#{script_input_param}"
# Transform git@github.com:user/repo-name.git -> https://github.com/user/repo-name
github_repo_url = remote_url.sub(/git@github.com:/, 'https://github.com/').sub(/.git$/, '')
open_url = ""
# Determine if a $REPO, $SHA, or $FILE was passed in and open the appropriate page
if script_input_param.start_with? dev_path_base
puts 'repo'
# $REPO
open_url = "#{github_repo_url}"
elsif script_input_param =~ /\b[0-9a-f]{5,40}\b/
puts 'sha'
# $SHA
open_url = "#{github_repo_url}/commit/#{script_input_param}"
else
# $FILE
puts 'file'
open_url = "#{github_repo_url}/blob/master/#{script_input_param}"
end
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{open_url}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{open_url}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{open_url}"
end
#!/bin/bash
ruby /dev/sh/sourcetree/sourcetree-open-on-github.rb "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment