Skip to content

Commit

Permalink
Merge pull request thoughtworks#51 from Picklive/cruisecontrol.rb
Browse files Browse the repository at this point in the history
---

Dont try to switch branch when default branch is not *master* and the build branch is the default branch.

`git branch --track development origin/development` fails if youre already on the *development* branch.
  • Loading branch information
bguthrie committed Jun 22, 2011
2 parents 3013aa2 + de2e8cd commit f22c7bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/source_control/git.rb
Expand Up @@ -24,7 +24,7 @@ def checkout(revision = nil, stdout = $stdout)
# need to read from command output, because otherwise tests break
git('clone', [@repository, path], :execute_in_project_directory => false)

if @branch and @branch != 'master'
if @branch and @branch != current_branch
git('branch', ['--track', @branch, "origin/#{@branch}"])
git('checkout', ['-q', @branch]) # git prints 'Switched to branch "branch"' to stderr unless you pass -q
end
Expand Down
22 changes: 22 additions & 0 deletions test/unit/source_control/git_test.rb
Expand Up @@ -72,6 +72,7 @@ def test_checkout_with_branch_should_perform_git_clone_branch_and_checkout
in_sandbox do
git = new_git(:repository => "git:/my_repo", :branch => "mybranch")
git.expects(:git).with("clone", ["git:/my_repo", '.'], :execute_in_project_directory => false)
git.stubs(:current_branch).returns('master')
git.expects(:git).with("branch", ["--track", 'mybranch', 'origin/mybranch'])
git.expects(:git).with("checkout", ["-q", 'mybranch'])
git.checkout
Expand All @@ -82,6 +83,27 @@ def test_checkout_with_master_branch_explicitly_specified_should_not_perform_git
in_sandbox do
git = new_git(:repository => "git:/my_repo", :branch => "master")
git.expects(:git).with("clone", ["git:/my_repo", '.'], :execute_in_project_directory => false)
git.stubs(:current_branch).returns('master')
git.checkout
end
end

def test_checkout_with_master_branch_explicitly_specified_when_master_is_not_default_should_perform_git_branch_and_checkout
in_sandbox do
git = new_git(:repository => "git:/my_repo", :branch => "master")
git.expects(:git).with("clone", ["git:/my_repo", '.'], :execute_in_project_directory => false)
git.stubs(:current_branch).returns('mybranch')
git.expects(:git).with("branch", ["--track", 'master', 'origin/master'])
git.expects(:git).with("checkout", ["-q", 'master'])
git.checkout
end
end

def test_checkout_with_default_branch_explicitly_specified_should_not_perform_git_branch_and_checkout
in_sandbox do
git = new_git(:repository => "git:/my_repo", :branch => "mybranch")
git.expects(:git).with("clone", ["git:/my_repo", '.'], :execute_in_project_directory => false)
git.stubs(:current_branch).returns('mybranch')
git.checkout
end
end
Expand Down

0 comments on commit f22c7bd

Please sign in to comment.