Skip to content

Instantly share code, notes, and snippets.

@andrewplummer
Last active October 10, 2015 19:38
Show Gist options
  • Save andrewplummer/3741118 to your computer and use it in GitHub Desktop.
Save andrewplummer/3741118 to your computer and use it in GitHub Desktop.
Open a link to a file in Github from the command line.
#!/usr/bin/env ruby
require 'open-uri'
if ARGV.length == 0
puts <<-MESSAGE
GitHub Show Script
------------------
Generates github URLs
Usage:
Show a file: github_show <FILE>[#LINE_NUMBER]
Show a branch: github_show branch <BRANCH_NAME>
Show a diff: github_show diff [TARGET_BRANCH_NAME] BRANCH_NAME
Create a pull request: github_show pr [TARGET_BRANCH_NAME] BRANCH_NAME
MESSAGE
exit
end
GITHUB_BASE_URL = "https://github.com"
tmp = `git remote -v`.match(/(\w+)\/(\w+)\.git/)
@owner = tmp[1]
@repo = tmp[2]
url = "#{GITHUB_BASE_URL}/#{@owner}/#{@repo}/"
def default_branch
@owner == 'cerego' ? 'stable' : 'master'
end
def current_branch
`git branch`.scan(/\* (.+)$/)[0]
end
if ARGV[0] == 'show'
# Commit view:
# https://github.com/cerego/iknow/commit/a1eaffa69594c7c792a0ab5d366468b2bebb020c
url << "commit/#{ARGV[1]}"
elsif ARGV[0] == 'branch'
# Branch view:
# https://github.com/cerego/iknow/tree/require_authentication_for_teacher_lesson_page_%237183
branch = ARGV[1]
url << "tree/#{branch}"
elsif ARGV[0] == 'pr'
# Pull request view:
# https://github.com/cerego/iknow/pull/new/require_authentication_for_teacher_lesson_page_%237183
branch1 = ARGV[1]
branch2 = ARGV[2]
if branch1.nil?
branch1 = current_branch
end
if branch2.nil?
branch2 = branch1
branch1 = default_branch
end
url << "pull/new/#{branch1}...#{branch2}"
elsif ARGV[0] == 'diff'
# Compare view:
# https://github.com/cerego/iknow/compare/stable...gge_vote_on_lessons_%237002
branch1 = ARGV[1]
branch2 = ARGV[2]
if branch2.nil?
branch2 = branch1
branch1 = default_branch
end
url << "compare/#{branch1}...#{branch2}"
else
# Code view:
# https://githube.com/cerego/iknow/blob/stable/app/controllers/application_controller.rb
if ARGV.length == 1
file = ARGV[0]
branch = default_branch
else
file = ARGV[1]
branch = ARGV[0]
end
if file =~ /#\d+$/
tmp = file.split('#')
file = tmp[0]
line = tmp[1]
end
url << "blob/#{branch}/#{file}"
end
url = URI::encode(url)
if line
url += "#L" + line
end
puts "Opening: #{url}"
#`xdg-open #{url}`
c = <<-COMMAND
if which xdg-open &> /dev/null; then
xdg-open #{url}
else
open #{url}
fi
COMMAND
`#{c}`
@zev
Copy link

zev commented Sep 18, 2012

Try this

if (which xdg-ope2n > /dev/null)
then 
  xdg-open $url &
else
  open $url
fi

@zev
Copy link

zev commented Sep 18, 2012

make sure that is xdg-open not the ope2n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment