Permalink
Browse files

add 'create' task for instant creation and pushing to a GitHub repo

  • Loading branch information...
1 parent 5a4cb0b commit 022dd8e2ef7f49fbcb884f71414867b7da666da6 @mislav committed Aug 24, 2008
Showing with 44 additions and 3 deletions.
  1. +44 −3 github.thor
View
@@ -1,19 +1,58 @@
#!/usr/bin/env ruby
+require 'rubygems'
+require 'thor'
class Github < Thor
- desc "track USER [BRANCH_NAME]", "track a USERs fork"
+ desc "track <user> [<branch-name>]", "track a fork belonging to <user>"
def track(user, branch_name = nil)
branch_name ||= user
git %(remote add #{user} git://github.com/#{user}/#{project_name}.git)
git %(fetch --no-tags #{user} master:refs/remotes/#{user}/master)
git %(branch #{branch_name} --track #{user}/master)
end
+ desc "update", "checkout each branch that has a remote and git pull"
def update
- branches.reverse.each do |branch|
+ all = branches
+ current = all.first
+ last = nil
+ branches_with_remotes = all.reject { |branch| remote_for_branch(branch) == '' }
+
+ branches_with_remotes.reverse.each do |branch|
git %(checkout #{branch})
git 'pull -v --no-tags'
+ last = branch
end
+
+ git %(checkout #{current}) unless last == current
+ end
+
+ desc "create <repo-name>", "create a new GitHub repository and push to it"
+ method_options :private => :boolean, :remote => :optional, :branch => :optional
+ def create(repo_name, opts)
+ # prompt for username/password
+ print 'GitHub login ("user:pass"): '
+ STDIN.gets
+ auth = $_.chomp
+ owner = auth.split(':').first
+
+ # make a POST request to create a new repo
+ # WARNING: your GitHub password is being sent unencrypted over HTTP Basic auth
+ require 'net/http'
+ uri = URI.parse "http://#{auth}@github.com/repositories"
+ data = { 'repository[name]' => repo_name, 'repository[public]' => (!opts['private']).to_s }
+ response = Net::HTTP.post_form(uri, data)
+ response.error! if response.code >= 400
+
+ # set up a new remote and push commits to it
+ remote = opts['remote'] || 'origin'
+ branch = opts['branch'] || 'master'
+ git %(remote add #{remote} git@github.com:#{owner}/#{repo_name}.git), true
+ puts "added GitHub as remote origin; now pushing commits from #{branch} ..."
+ git %(push origin #{branch})
+ # set up tracking
+ git %(config branch.#{branch}.remote origin)
+ git %(config branch.#{branch}.merge refs/heads/master)
end
private
@@ -63,4 +102,6 @@ class Github < Thor
end
end
end
-end
+end
+
+Github.start if __FILE__ == $0

0 comments on commit 022dd8e

Please sign in to comment.