Permalink
Browse files

Initial addition to git.

  • Loading branch information...
0 parents commit 0a8dbe11f3413bf746dc276b2a531db7128158bc @Narnach committed Jul 10, 2008
Showing with 159 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +17 −0 README.rdoc
  3. +122 −0 github-clone
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Wes Oldenbeuving
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
+= Github-clone
+
+== Description
+
+Github-clone makes it easy to clone all github projects of a user, using either the public or private clone urls for the projects.
+
+== Examples
+
+Clone all repositories of user Narnach, using the public clone URLs:
+ github-clone Narnach
+
+Clone all repositories of user Narnach, using the private clone URLs and suppressing console output:
+ github-clone Narnach -q --private
+
+== Author
+
+Copyright (c) 2008 - Wes Oldenbeuving, released under the MIT license.
@@ -0,0 +1,122 @@
+#!/usr/bin/env ruby
+require 'open-uri'
+require 'yaml'
+
+# Clone all of a user's GitHub repositories.
+class GithubClone
+ attr_accessor :quiet, :use_private_clone_url
+ attr_reader :username
+
+ def initialize(username)
+ @username = username
+ @quiet = false
+ @use_private_clone_url = false
+ end
+
+ # Clone all repositories which do not exist in the local directory.
+ def clone_new_repositories
+ clone_repositories.each do |name, clone_url|
+ if File.exist?(name)
+ puts("Skipping existing project '%s'" % name) unless quiet
+ next
+ end
+ execute_cmd "git clone %s" % clone_url
+ end
+ end
+
+ private
+
+ # Execute something on the command line
+ # Will suppress output if #quiet is true.
+ def execute_cmd(cmd)
+ if quiet
+ cmd << ' > /dev/null 2>&1'
+ else
+ puts cmd
+ end
+ system(cmd)
+ end
+
+ # The github API URL for this user
+ def url
+ @url ||= "http://github.com/api/v1/yaml/%s" % username
+ end
+
+ # Get the raw yaml from the server
+ def raw_yaml
+ @raw_yaml ||= open(url).read
+ end
+
+ # Parse the raw yaml data
+ def data
+ @data ||= YAML.load(raw_yaml)
+ end
+
+ # Returns all repositories of a user
+ def repositories
+ data['user']['repositories']
+ end
+
+ # Returns the name and clone url for all of a user's repositories.
+ def clone_repositories
+ @clone_repositories ||= (
+ repositories.map do |repo|
+ repo_name = repo[:name]
+ clone_url = private_clone_url(repo_name)
+ next repo_name, clone_url(repo_name)
+ end
+ )
+ end
+
+ # Return the clone url for a repository.
+ # Decides between the private and public url based on #use_private_clone_url.
+ def clone_url(repo_name)
+ if use_private_clone_url
+ private_clone_url(repo_name)
+ else
+ public_clone_url(repo_name)
+ end
+ end
+
+ # The private clone url for a repository.
+ def private_clone_url(repo_name)
+ "git@github.com:%s/%s.git" % [username, repo_name]
+ end
+
+ # The private clone url for a repository.
+ def public_clone_url(repo_name)
+ "git://github.com/%s/%s.git" % [username, repo_name]
+ end
+end
+
+# Parse command line arguments
+username = ARGV.shift
+
+if %w[-h --help help].include? username or username.to_s == ''
+ puts <<-EOS
+Syntax:
+ github-clone <Username> [ -q ] [ --public | --private ]
+
+ Username is your github username.
+ -q Run github-clone in quiet mode, suppressing all output.
+ --public, --private Use public or private clone URL. Defaults to use public.
+ EOS
+ exit
+end
+
+# Clone the repositories
+gh = GithubClone.new username
+while option = ARGV.shift
+ case option
+ when '-q'
+ gh.quiet = true
+ when '--public'
+ gh.use_private_clone_url = false
+ when '--private'
+ gh.use_private_clone_url = true
+ else
+ puts "Unknown option '%s'" % option
+ exit
+ end
+end
+gh.clone_new_repositories

0 comments on commit 0a8dbe1

Please sign in to comment.