Skip to content

Instantly share code, notes, and snippets.

@thsig
Created March 27, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thsig/76d8f16374a5cfb2f20f8395f2e0c8ab to your computer and use it in GitHub Desktop.
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
#!/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