Created
March 27, 2018 15:06
-
-
Save thsig/76d8f16374a5cfb2f20f8395f2e0c8ab to your computer and use it in GitHub Desktop.
Simple script for viewing a file at a given commit/branch Github.com
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 | |
usage_description = """Usage: | |
github-open <commit> [<path> [<line-number>]] | |
Uses the <origin> remote's URL, and assumes that it's a Github repo. | |
""" | |
args = ARGV | |
if args.count == 0 | |
puts usage_description | |
exit | |
end | |
commit = args[0] | |
repo_rel_path = if args[1] | |
%x[git ls-tree --full-name --name-only HEAD #{args[1]}].gsub("\n", "") | |
else | |
nil | |
end | |
line_number = (args[2] || nil) | |
remote = 'origin' | |
base_url = %x[git ls-remote --get-url|cut -d':' -f 2|cut -d'.' -f 1].gsub("\n", "") | |
url = "https://github.com/#{base_url}/tree/#{commit}" | |
if repo_rel_path | |
url << "/#{repo_rel_path}" | |
end | |
if line_number | |
url << "\#L#{line_number}" | |
end | |
%x[open #{url}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment