Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jawwad/112addd8f295e30cd78a to your computer and use it in GitHub Desktop.
Save jawwad/112addd8f295e30cd78a to your computer and use it in GitHub Desktop.
SourceTree Custom Actions to open the selected FILE, SHA, or REPO on github.com
#!/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/this_script.rb
# Parameters: $SHA
# Menu Caption: Open File on github.com
# Script to run: ~/path/to/this_script.rb
# Parameters: $FILE
# Menu Caption: Open REPO on github.com
# Script to run: ~/path/to/this_script.rb
# Parameters: $REPO
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
# 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$/, '')
# Determine if a $REPO, $SHA, or $FILE was passed in and open the appropriate page
if script_input_param.start_with? '/Users/'
# $REPO
system("open #{github_repo_url}")
elsif script_input_param =~ /\b[0-9a-f]{5,40}\b/
# $SHA
github_commit_url = "#{github_repo_url}/commit/#{script_input_param}"
system("open #{github_commit_url}")
else
# $FILE
github_file_url = "#{github_repo_url}/blob/master/#{script_input_param}"
system("open #{github_file_url}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment