From cf10b2f0e4f57c2e8a549663a9ab179119291f44 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Sat, 24 May 2008 23:26:27 -0700 Subject: [PATCH] Evaluate commands/helpers in the context of GitHub --- commands/commands.rb | 38 +++++++++++++++++++------------------- commands/helpers.rb | 36 ++++++++++++++++++------------------ lib/github.rb | 4 +++- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/commands/commands.rb b/commands/commands.rb index c943419d..0af2787b 100644 --- a/commands/commands.rb +++ b/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 @@ -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:" @@ -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) @@ -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' @@ -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? @@ -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? diff --git a/commands/helpers.rb b/commands/helpers.rb index 666e73cc..58e1c565 100644 --- a/commands/helpers.rb +++ b/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) == "" @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/lib/github.rb b/lib/github.rb index 884b641c..c48a3909 100644 --- a/lib/github.rb +++ b/lib/github.rb @@ -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)