Skip to content

Instantly share code, notes, and snippets.

@kiko
Forked from manveru/sync
Created August 21, 2010 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiko/542566 to your computer and use it in GitHub Desktop.
Save kiko/542566 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open-uri'
require 'cgi'
require 'pp'
require 'yaml'
require 'rake'
module GitHub
module Connectivity
API = 'http://github.com/api/v2/yaml/' # repos/show/schacon/grit
def get(uri, *args)
uri = API + (uri % args.map{|a|
CGI.escape(a).sub(/\./, '%2e') # escape . for silly github
})
# p uri
response = YAML.load(open(uri).read)
return response unless error = response['error']
raise(error.inspect)
end
end
module Creatability
def create_one(hash)
return unless hash
if hash.keys.first.respond_to?(:to_str)
new(*members.map{|m| hash[m.to_s] })
else
new(*members.map{|m| hash[m.to_sym] })
end
end
def create_many(key, hash)
array = hash[key]
array.map{|hash| create_one(hash) }
end
end
class Issue < Struct.new(:position, :number, :votes, :created_at, :body,
:title, :updated_at, :user, :state)
extend Connectivity, Creatability
LIST = 'issues/list/%s/%s/%s' # user, repo, state
def self.list(user, repo, state = 'open')
create_many 'issues', get(LIST, user, repo, state)
end
end
class Repo < Struct.new(:description, :fork, :forks, :homepage, :name, :owner,
:pledgie, :private, :url, :watchers, :actions, :size,
:followers, :username, :language, :id, :type, :pushed,
:score, :created)
extend Connectivity, Creatability
SEARCH = 'repos/search/%s' # query
def self.search(query)
create_many 'repositories', get(SEARCH, query)
end
SHOW_USER = 'repos/show/%s' # user
SHOW_USER_REPO = 'repos/show/%s/%s' # user, repo
def self.show(user, repo = nil)
if repo
create_one 'repository', get(SHOW_USER_REPO, user, repo)
else
create_many 'repositories', get(SHOW_USER, user)
end
end
def issue_list
Issue.list(owner, name)
rescue => ex
pp ex
end
end
end
gh_dir = File.expand_path('~/github')
Dir.glob File.join(gh_dir, (ARGV[0] || '*/')) do |user_dir|
next unless File.directory?(user_dir)
Dir.chdir user_dir do
GitHub::Repo.show(File.basename(user_dir)).each do |repo|
repo_dir = File.join(user_dir, repo.name)
if File.directory?(File.join(repo_dir, '.git'))
puts "Sync #{repo_dir}"
Dir.chdir(repo_dir){ fork{ system('git', 'pull', '-q') } }
else
user_project = "#{repo.owner}/#{repo.name}.git"
unless system('git', 'clone', "git@github.com:#{user_project}")
system('git', 'clone', "git://github.com/#{user_project}")
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment