Skip to content

Commit

Permalink
Don't try to checkout with -b if the branch is already tracked locally [
Browse files Browse the repository at this point in the history
integrity#9 state:resolved]
  • Loading branch information
foca committed Jul 21, 2008
1 parent e39f644 commit 5437644
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
22 changes: 14 additions & 8 deletions lib/integrity/scm/git.rb
Expand Up @@ -13,8 +13,6 @@ def with_revision(revision, &block)
fetch_code
checkout(revision)
chdir(&block)
ensure
checkout('origin/HEAD')
end

def head
Expand All @@ -34,19 +32,27 @@ def chdir(&in_working_copy)
end

def clone
system "git clone #{uri} #{working_directory}"
`git clone #{uri} #{working_directory}`
end

def checkout(treeish=nil)
if treeish
chdir { system "git checkout #{treeish}" }
else
chdir { system "git checkout -b #{branch} origin/#{branch}" }
strategy = case
when treeish then treeish
when local_branches.include?(branch) then branch
else "-b #{branch} origin/#{branch}"
end

chdir { `git checkout #{strategy}` }
end

def pull
chdir { system "git pull" }
chdir { `git pull` }
end

def local_branches
chdir do
`git branch`.split("\n").map {|b| b.delete("*").strip }
end
end

def commit_info(treeish)
Expand Down
68 changes: 41 additions & 27 deletions spec/scm/git_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe Integrity::SCM::Git do
before do
Integrity::SCM::Git.class_eval { public :fetch_code, :chdir, :clone, :checkout, :pull, :commit_info, :cloned?, :on_branch? }
Integrity::SCM::Git.class_eval { public :fetch_code, :chdir, :clone, :checkout, :pull, :commit_info, :local_branches, :cloned?, :on_branch? }
@git = Integrity::SCM::Git.new("git://github.com/foca/integrity.git", "master", "/var/integrity/exports/foca-integrity")
end

Expand Down Expand Up @@ -104,11 +104,6 @@
@git.should_receive(:chdir).with(&@block).and_yield
@git.with_revision('4d0cfafd569ef60d0c578bf8a9d51f9582612f03', &@block)
end

it 'should ensure it checkout origin/HEAD' do
@git.should_receive(:checkout).with('origin/HEAD')
@git.with_revision('4d0cfafd569ef60d0c578bf8a9d51f9582612f03', &@block)
end
end

describe "Getting information about a commit" do
Expand Down Expand Up @@ -168,38 +163,57 @@
end
end

describe "Doing all the low-level operations on the repo" do
it "should pass the uri and expected working directory to git-clone when cloning" do
@git.should_receive(:system).with("git clone git://github.com/foca/integrity.git /var/integrity/exports/foca-integrity")
@git.clone
describe "Listing the local branches" do
def branches
["* master",
" other",
" yet_another"] * "\n"
end

it "should change dirs to the repo's and checkout the appropiate branch via git-checkout" do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with("git checkout -b master origin/master")
@git.checkout
it "should return an array of branch names" do
@git.stub!(:chdir).and_yield
@git.stub!(:`).with("git branch").and_return(branches)
@git.local_branches.should == ["master", "other", "yet_another"]
end

it "should check out a branch that has already been initialized locally without failing" do
pending "it only works the first time you change the branch, need to fix"
end

describe "Doing all the low-level operations on the repo" do
it "should pass the uri and expected working directory to git-clone when cloning" do
@git.should_receive(:`).with("git clone git://github.com/foca/integrity.git /var/integrity/exports/foca-integrity")
@git.clone
end

it "should switch dirs to the repo's and call git-pull when pulling" do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with("git pull")
@git.should_receive(:`).with("git pull")
@git.pull
end

describe "(checking out code)" do
before { @git.stub!(:chdir).and_yield }

it "should check out the branch locally if it's already available" do
@git.stub!(:local_branches).and_return(["master"])
@git.should_receive(:`).with("git checkout master")
@git.checkout
end

it 'should checkout the given commit' do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with('git checkout 7e4f36231776ea4401b6e385df5f43c11633d59f')
@git.checkout('7e4f36231776ea4401b6e385df5f43c11633d59f')
end
it "should create a new branch that tracks an external branch if the branch isn't local" do
@git.stub!(:local_branches).and_return(["master"])
@git.stub!(:branch).and_return("redux")
@git.should_receive(:`).with("git checkout -b redux origin/redux")
@git.checkout
end

it 'should checkout the given treeish' do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with('git checkout origin/HEAD')
@git.checkout('origin/HEAD')
it 'should checkout the given commit' do
@git.should_receive(:`).with('git checkout 7e4f36231776ea4401b6e385df5f43c11633d59f')
@git.checkout('7e4f36231776ea4401b6e385df5f43c11633d59f')
end

it 'should checkout the given treeish' do
@git.should_receive(:`).with('git checkout origin/HEAD')
@git.checkout('origin/HEAD')
end
end
end
end

0 comments on commit 5437644

Please sign in to comment.