Skip to content

Commit

Permalink
Evaluate commands/helpers in the context of GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyball committed May 25, 2008
1 parent c977651 commit cf10b2f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
38 changes: 19 additions & 19 deletions commands/commands.rb
@@ -1,12 +1,12 @@
GitHub.describe :home => "Open this repo's master branch in a web browser."
GitHub.register :home do |user|
describe :home => "Open this repo's master branch in a web browser."
register :home do |user|
if helper.project
helper.open helper.homepage_for(user || helper.owner, 'master')
end
end

GitHub.describe :browse => "Open this repo in a web browser."
GitHub.register :browse do |user, branch|
describe :browse => "Open this repo in a web browser."
register :browse do |user, branch|
if helper.project
# if one arg given, treat it as a branch name
# unless it maches user/branch, then split it
Expand All @@ -20,16 +20,16 @@
end
end

GitHub.describe :network => "Open the network page for this repo in a web browser"
GitHub.register :network do |user|
describe :network => "Open the network page for this repo in a web browser"
register :network do |user|
if helper.project
user ||= helper.owner
helper.open helper.network_page_for(user)
end
end

GitHub.describe :info => "Info about this project."
GitHub.register :info do
describe :info => "Info about this project."
register :info do
puts "== Info for #{helper.project}"
puts "You are #{helper.owner}"
puts "Currently tracking:"
Expand All @@ -38,9 +38,9 @@
end
end

GitHub.describe :track => "Track another user's repository."
GitHub.flags :track, :private => "Use git@github.com: instead of git://github.com/"
GitHub.register :track do |user|
describe :track => "Track another user's repository."
flags :track, :private => "Use git@github.com: instead of git://github.com/"
register :track do |user|
die "Specify a user to track" if user.nil?
die "Already tracking #{user}" if helper.tracking?(user)

Expand All @@ -51,9 +51,9 @@
end
end

GitHub.describe :pull => "Pull from a remote."
GitHub.flags :pull, :merge => "Automatically merge remote's changes into your master."
GitHub.register :pull do |user, branch|
describe :pull => "Pull from a remote."
flags :pull, :merge => "Automatically merge remote's changes into your master."
register :pull do |user, branch|
die "Specify a user to pull from" if user.nil?
user, branch = user.split("/", 2) if branch.nil?
branch ||= 'master'
Expand All @@ -68,9 +68,9 @@
end
end

GitHub.describe :clone => "Clone a repo."
GitHub.flags :clone, :ssh => "Clone using the git@github.com style url"
GitHub.register :clone do |user, repo, dir|
describe :clone => "Clone a repo."
flags :clone, :ssh => "Clone using the git@github.com style url"
register :clone do |user, repo, dir|
die "Specify a user to pull from" if user.nil?
user, repo = user.split('/') unless repo
die "Specify a repo to pull from" if repo.nil?
Expand All @@ -82,8 +82,8 @@
end
end

GitHub.describe :'pull-request' => "Generate the text for a pull request"
GitHub.register :'pull-request' do |user, branch|
describe :'pull-request' => "Generate the text for a pull request"
register :'pull-request' do |user, branch|
if helper.project
die "Specify a user for the pull request" if user.nil?
user, branch = user.split('/', 2) if branch.nil?
Expand Down
36 changes: 18 additions & 18 deletions commands/helpers.rb
@@ -1,23 +1,23 @@
GitHub.helper :user_and_repo_from do |url|
helper :user_and_repo_from do |url|
case url
when %r|^git://github\.com/([^/]+/[^/]+)$|: $1.split('/')
when %r|^(?:ssh://)?(?:git@)?github\.com:([^/]+/[^/]+)$|: $1.split('/')
end
end

GitHub.helper :user_and_repo_for do |remote|
helper :user_and_repo_for do |remote|
user_and_repo_from(url_for(remote))
end

GitHub.helper :user_for do |remote|
helper :user_for do |remote|
user_and_repo_for(remote).try.first
end

GitHub.helper :repo_for do |remote|
helper :repo_for do |remote|
user_and_repo_for(remote).try.last
end

GitHub.helper :project do
helper :project do
repo = repo_for(:origin)
if repo.nil?
if url_for(:origin) == ""
Expand All @@ -30,11 +30,11 @@
repo.chomp('.git')
end

GitHub.helper :url_for do |remote|
helper :url_for do |remote|
`git config --get remote.#{remote}.url`.chomp
end

GitHub.helper :remotes do
helper :remotes do
regexp = '^remote\.(.+)\.url$'
`git config --get-regexp '#{regexp}'`.split(/\n/).inject({}) do |memo, line|
name_string, url = line.split(/ /, 2)
Expand All @@ -44,7 +44,7 @@
end
end

GitHub.helper :tracking do
helper :tracking do
remotes.inject({}) do |memo, (name, url)|
if ur = user_and_repo_from(url)
memo[name] = ur.first
Expand All @@ -55,15 +55,15 @@
end
end

GitHub.helper :tracking? do |user|
helper :tracking? do |user|
tracking.values.include?(user)
end

GitHub.helper :owner do
helper :owner do
user_for(:origin)
end

GitHub.helper :user_and_branch do
helper :user_and_branch do
raw_branch = `git rev-parse --symbolic-full-name HEAD`.chomp.sub(/^refs\/heads\//, '')
user, branch = raw_branch.split(/\//, 2)
if branch
Expand All @@ -73,30 +73,30 @@
end
end

GitHub.helper :branch_user do
helper :branch_user do
user_and_branch.first
end

GitHub.helper :branch_name do
helper :branch_name do
user_and_branch.last
end

GitHub.helper :public_url_for do |user|
helper :public_url_for do |user|
"git://github.com/#{user}/#{project}.git"
end

GitHub.helper :private_url_for do |user|
helper :private_url_for do |user|
"git@github.com:#{user}/#{project}.git"
end

GitHub.helper :homepage_for do |user, branch|
helper :homepage_for do |user, branch|
"https://github.com/#{user}/#{project}/tree/#{branch}"
end

GitHub.helper :network_page_for do |user|
helper :network_page_for do |user|
"https://github.com/#{user}/#{project}/network"
end

GitHub.helper :open do |url|
helper :open do |url|
Launchy::Browser.new.visit url
end
4 changes: 3 additions & 1 deletion lib/github.rb
Expand Up @@ -91,7 +91,9 @@ def parse_options(args)
end

def load(file)
file[0] == ?/ ? super : super(BasePath + "/commands/#{file}")
file[0] == ?/ ? path = file : path = BasePath + "/commands/#{file}"
data = File.read(path)
GitHub.module_eval data, path
end

def debug(*messages)
Expand Down

0 comments on commit cf10b2f

Please sign in to comment.