Permalink
Browse files

Proper fetch vs clone syncing

  • Loading branch information...
rsanheim committed Aug 13, 2010
1 parent 714d5d2 commit 50eb893640739529b76df025aaa23a2751a1867a
Showing with 63 additions and 28 deletions.
  1. +1 −4 lib/nachos.rb
  2. +1 −17 lib/nachos/cli.rb
  3. +35 −5 lib/nachos/main.rb
  4. +2 −2 spec/nachos/cli_spec.rb
  5. +15 −0 spec/nachos/main_spec.rb
  6. +9 −0 spec/nachos_spec.rb
View
@@ -11,13 +11,10 @@
class Nachos
- attr_reader :args, :command
- attr_accessor :out, :err, :exit_code
+ attr_reader :args
def initialize(*args)
@args = args
- @command = args.first || "help"
- @exit_code = 0
end
def self.execute(*args)
View
@@ -22,30 +22,14 @@ def watched
desc "sync", "Sync repositories"
def sync
- shell.say main.sync_summary
+ shell.say main.github_summary
main.sync
end
private
- def Hub(args)
- Hub::Runner.new(*args.split(' '))
- end
-
def dry_run?
options[:dry_run]
end
- def run(cmd)
- if dry_run?
- say cmd
- else
- system cmd
- end
- end
-
- def github_summary
- "You have #{github.watched.size} watched repos, and #{github.client.list_repos.size} owned repos."
- end
-
end
View
@@ -3,6 +3,7 @@ class Main
extend Forwardable
include Nachos::Github
def_delegators :cli, :shell, :dry_run?
+ def_delegators :config, :config
attr_reader :cli, :config
def initialize(cli)
@@ -17,11 +18,8 @@ def info
end
def sync
- chdir config.config.repo_root do
- watched.each do |repo|
- git_url = repo.url.gsub("http", "git")
- run Hub("clone #{git_url} #{repo.owner}-#{repo.name}").command
- end
+ chdir config.repo_root do
+ watched.each { |repo| sync_repo(repo) }
end
end
@@ -35,5 +33,37 @@ def chdir(dir)
end
end
+ def sync_repo(repo)
+ git_url = repo.url.gsub("http", "git")
+ path = repo_path(repo)
+ if repo_exists?(repo)
+ chdir(path) do
+ run Hub("fetch").command
+ end
+ else
+ run Hub("clone #{git_url} #{repo.owner}-#{repo.name}").command
+ end
+ end
+
+ def repo_path(repo)
+ Pathname(config.repo_root.join("#{repo.owner}-#{repo.name}"))
+ end
+
+ def repo_exists?(repo)
+ repo_path(repo).directory?
+ end
+
+ def Hub(args)
+ Hub::Runner.new(*args.split(' '))
+ end
+
+ def run(cmd)
+ if dry_run?
+ say cmd
+ else
+ system cmd
+ end
+ end
+
end
end
View
@@ -55,15 +55,15 @@
it "calls sync on main" do
cli = Nachos::CLI.new
- main = stub_everything(:sync_summary => "")
+ main = stub_everything(:github_summary => "")
main.expects(:sync)
cli.stubs(:main).returns(main)
cli.invoke(:sync)
end
it "displays summary sync info" do
cli = Nachos::CLI.new
- cli.stubs(:main).returns(stub_everything(:sync_summary => "sync summary"))
+ cli.stubs(:main).returns(stub_everything(:github_summary => "sync summary"))
cli.invoke(:sync)
cli.shell.output.should include("sync summary")
end
View
@@ -21,4 +21,19 @@
end
end
+ describe "repo_exists?" do
+ it "returns true if dir exists" do
+ Pathname.any_instance.expects(:directory?).returns(true)
+ repo = Hashie::Mash.new(:owner => "jdoe", :name => "project")
+ main = Nachos::Main.new(Nachos::CLI.new)
+ main.repo_exists?(repo).should be_true
+ end
+
+ it "returns false if dir does not exist" do
+ Pathname.any_instance.expects(:directory?).returns(false)
+ repo = Hashie::Mash.new(:owner => "jdoe", :name => "project")
+ main = Nachos::Main.new(Nachos::CLI.new)
+ main.repo_exists?(repo).should be_false
+ end
+ end
end
View
@@ -16,5 +16,14 @@
nachos.expects(:exit).with(0)
nachos.execute
end
+
+ it "can do help" do
+ begin
+ Nachos::CLI.any_instance.stubs(:shell).returns(shell = stub_everything())
+ Nachos.execute("help")
+ rescue SystemExit => e
+ end
+ end
end
+
end

0 comments on commit 50eb893

Please sign in to comment.