Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Dec 5, 2009
0 parents commit de9bfa2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task :default => :test
1 change: 1 addition & 0 deletions bin/.#hub
61 changes: 61 additions & 0 deletions bin/hub
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env ruby

class Hub
PRIVATE = 'git@github.com:%s/%s.git'
PUBLIC = 'git://github.com/%s/%s.git'
USER = `git config --global github.user`.chomp
REPO = `basename $(pwd)`.chomp

attr_reader :args, :after
def initialize(*args)
@args = args
end

def clone
ssh = @args.delete('-p')
@args.each_with_index do |arg, i|
if arg.scan('/').size == 1 && !arg.include?(':')
url = ssh ? PRIVATE : PUBLIC
@args[i] = url % arg.split('/')
end
end
end

def remote
if @args[1] == 'add'
if @args.delete('-g')
ssh = @args.delete('-p')
user = @args.last
url = ssh ? PRIVATE : PUBLIC
@args << url % [ user, REPO ]
end
end
end

def init
if @args.delete('-g')
url = PRIVATE % [ USER, REPO ]
@after = "git remote add origin #{url}"
end
end

def execute
if respond_to?(@args[0])
send(@args[0])
end

if @after
if system("git", *@args)
exec @after
else
exit 1
end
else
exec "git", *@args
end
end
end

if $0 == __FILE__
Hub.new(*ARGV).execute
end
36 changes: 36 additions & 0 deletions test/hub_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'test/unit'
load File.dirname(__FILE__) + '/../bin/hub'

class HubTest < Test::Unit::TestCase
def hub(args)
args = args.split(' ')
hub = Hub.new(*args)
hub.send(args[0])
hub
end

def test_private_clone
h = hub("clone -p rtomayko/ron")
assert_equal 'git@github.com:rtomayko/ron.git', h.args.last
end

def test_public_clone
h = hub("clone rtomayko/ron")
assert_equal 'git://github.com/rtomayko/ron.git', h.args.last
end

def test_private_remote
h = hub("remote add -g -p rtomayko")
assert_equal 'git@github.com:rtomayko/hub.git', h.args.last
end

def test_public_remote
h = hub("remote add -g rtomayko")
assert_equal 'git://github.com/rtomayko/hub.git', h.args.last
end

def test_init
h = hub("init -g")
assert_equal 'git remote add origin git@github.com:defunkt/hub.git', h.after
end
end

0 comments on commit de9bfa2

Please sign in to comment.