Created
October 25, 2010 16:06
-
-
Save mislav/645208 to your computer and use it in GitHub Desktop.
TextMate command to copy a GitHub URL for selected code to clipboard
This file contains hidden or 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 | |
filename = ENV['TM_FILEPATH'] | |
root_dir = ENV['TM_DIRECTORY'] | |
root_dir = File.dirname(root_dir) until File.directory?("#{root_dir}/.git") or root_dir == "/" | |
Dir.chdir root_dir | |
file_path = filename.sub("#{root_dir}/", '') | |
if ENV['TM_INPUT_START_LINE'] | |
start_line = ENV['TM_INPUT_START_LINE'].to_i | |
end_line = start_line + ENV['TM_SELECTED_TEXT'].rstrip.scan("\n").size | |
range = start_line..end_line | |
else | |
line = ENV['TM_LINE_NUMBER'].to_i | |
range = line..line | |
end | |
branch = `git symbolic-ref HEAD`.chomp.sub('refs/heads/', '') | |
remote_name = `git config branch.#{branch}.remote`.chomp | |
if remote_name.empty? | |
remote_name = 'origin' | |
remote_branch = branch | |
else | |
remote_branch = `git config branch.#{branch}.merge`.chomp.sub('refs/heads/', '') | |
end | |
remote_url = `git config remote.#{remote_name}.url`.chomp.sub(/\.git$/, '') | |
rev = `git rev-parse #{remote_name}/#{remote_branch}`[0, 7] | |
range_out = if range.begin == range.end | |
range.begin | |
else | |
"#{range.begin}-#{range.end}" | |
end | |
remote_url =~ %r{github\.com[/:]([^/]+)/([^/]+)} | |
url = "http://github.com/#{$1}/#{$2}/blob/#{rev}/#{file_path}#L#{range_out}" | |
IO.popen('pbcopy', 'w') { |cp| cp << url } | |
puts url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment