Created
October 28, 2016 13:48
-
-
Save jdforsythe/dbd7de722b99d1279fb323607523ee56 to your computer and use it in GitHub Desktop.
SourceTree Open on GitHub Custom Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby d:\dev\sh\sourcetree\sourcetree-open-on-github.rb %1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 [email protected]:user/repo-name.git -> https://github.com/user/repo-name | |
github_repo_url = remote_url.sub(/[email protected]:/, '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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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