Skip to content

Commit

Permalink
gh clone --search query allows you to do a search to find a repo to c…
Browse files Browse the repository at this point in the history
…lone
  • Loading branch information
drnic committed Nov 4, 2009
1 parent cf563bd commit 3d5d1e7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/commands/commands.rb
Expand Up @@ -126,13 +126,26 @@
desc "Clone a repo. Uses ssh if current user is "
usage "github clone [user] [repo] [dir]"
flags :ssh => "Clone using the git@github.com style url."
flags :search => "Search for [user|repo] and clone selected repository"
command :clone do |user, repo, dir|
die "Specify a user to pull from" if user.nil?
if options[:search]
query = [user, repo, dir].compact.join(" ")
data = JSON.parse(open("http://github.com/api/v1/json/search/#{URI.escape query}").read)
if (repos = data['repositories']) && !repos.nil? && repos.length > 0
repos = repos.map { |r| "#{r['username']}/#{r['name']}"}.sort.uniq
if user_repo = GitHub::UI.display_select_list(repos)
user, repo = user_repo.split('/', 2)
end
end
die "Perhaps try another search" unless user && repo
end

if user.include?('/') && !user.include?('@') && !user.include?(':')
die "Expected user/repo dir, given extra argument" if dir
(user, repo), dir = [user.split('/', 2), repo]
end

if repo
if options[:ssh] || current_user?(user)
git_exec "clone git@github.com:#{user}/#{repo}.git" + (dir ? " #{dir}" : "")
Expand Down
1 change: 1 addition & 0 deletions lib/github.rb
Expand Up @@ -2,6 +2,7 @@
require 'github/extensions'
require 'github/command'
require 'github/helper'
require 'github/ui'
require 'fileutils'
require 'rubygems'
require 'open-uri'
Expand Down
14 changes: 14 additions & 0 deletions lib/github/ui.rb
@@ -0,0 +1,14 @@
require "readline"
require "highline"
module GitHub
module UI
extend self
def display_select_list(list)
HighLine.track_eof = false
HighLine.new.choose do |menu|
list.each { |item| menu.choice item }
menu.header = "Select a repository to clone"
end
end
end
end
15 changes: 15 additions & 0 deletions spec/ui_spec.rb
Expand Up @@ -352,6 +352,21 @@
end
end

specify "clone a selected repo after showing search results" do
running :clone, "--search", "github-gem" do
json = StringIO.new '{"repositories":[' +
'{"name":"github-gem","size":300,"followers":499,"username":"defunkt","language":"Ruby","fork":false,"id":"repo-1653","type":"repo","pushed":"2008-12-04T03:14:00Z","forks":59,"description":"The official `github` command line helper for simplifying your GitHub experience.","score":3.4152448,"created":"2008-02-28T09:35:34Z"},' +
'{"name":"github-gem-builder","size":76,"followers":26,"username":"pjhyett","language":"Ruby","fork":false,"id":"repo-67489","type":"repo","pushed":"2008-11-04T04:54:57Z","forks":3,"description":"The scripts used to build RubyGems on GitHub","score":3.4152448,"created":"2008-10-24T22:29:32Z"}' +
']}'
json.rewind
@command.should_receive(:open).with("http://github.com/api/v1/json/search/github-gem").and_return(json)
GitHub::UI.should_receive(:display_select_list).with(%w[defunkt/github-gem pjhyett/github-gem-builder]).
and_return("defunkt/github-gem")
@command.should_receive(:current_user?).and_return(nil)
@command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git")
end
end

# -- fork --
specify "fork should print out help" do
running :fork do
Expand Down

0 comments on commit 3d5d1e7

Please sign in to comment.