Skip to content

Commit

Permalink
Merge pull request #79 from xystushi/review_130918_change_default_act…
Browse files Browse the repository at this point in the history
…ion_of_checkout

Default action of checkout is to create a local branch.
  • Loading branch information
Bamboo committed Oct 7, 2013
2 parents 1c455aa + 3132484 commit cc76ac6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ $ git review browse 42

```
$ git review checkout 42
> checkout changes from request #42 to your local repository in a headless state
> checkout remote branch from request #42 and create a local branch from it
```

```
$ git review checkout 42 --branch
> checkout remote branch from request #42 and create a local branch from it
$ git review checkout 42 --no-branch
> checkout changes from request #42 to your local repository in a headless state
```


Expand Down
2 changes: 1 addition & 1 deletion bin/git-review
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end

desc 'Checkout a request\'s changes to local repo'
command :checkout do |c|
c.switch [:b, :branch]
c.switch [:b, :branch], :default_value => true
c.action do |global, opts, args|
help_now!('Request number is required.') if args.empty?
::GitReview::Commands.checkout(args.shift, opts[:branch])
Expand Down
19 changes: 16 additions & 3 deletions lib/git-review/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ def browse(number)
end

# Checkout a specified request's changes to your local repository.
def checkout(number, branch=false)
def checkout(number, branch=true)
request = get_request_by_number(number)
puts 'Checking out changes to your local repository.'
puts 'To get back to your original state, just run:'
puts
puts ' git checkout master'
puts
if branch
git_call("checkout #{request.head.ref}")
else
git_call("checkout pr/#{request.number}")
rename_branch(request)
else
git_call("checkout #{request.head.sha}")
end
end

Expand Down Expand Up @@ -343,6 +344,18 @@ def get_request_by_number(request_number)
request || (raise ::GitReview::InvalidRequestIDError)
end

def rename_branch(request)
ref = request.head.ref
user = request.head.user.login
number = request.number
new_name = "#{ref}_#{user}_pr_#{number}"
if local.branch_exists?(:local, new_name)
git_call("checkout #{new_name}")
else
git_call("branch -m #{new_name}")
end
end

end

end
Expand Down
47 changes: 43 additions & 4 deletions spec/git-review/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,53 @@
subject.stub(:puts)
end

it 'creates a headless state in the local repo with the requests code' do
it 'creates a local branch in the local repo with the requests code' do
subject.stub(:rename_branch)
subject.should_receive(:git_call).with("checkout pr/#{request_number}")
subject.checkout(1)
end

it 'creates a local branch if the optional param --branch is appended' do
subject.should_receive(:git_call).with("checkout #{head_ref}")
subject.checkout(1, true)
it 'creates a headless state if --no-branch is specified' do
subject.stub(:rename_branch)
subject.should_receive(:git_call).with("checkout #{head_sha}")
subject.checkout(1, false)
end

describe '#rename_branch' do

let(:branch_name) {
ref = request.head.ref
user = request.head.user.login
number = request.number
"#{ref}_#{user}_pr_#{number}"
}

context 'when the new branch does not exist' do

before(:each) do
local.stub(:branch_exists?).and_return(false)
end

it 'renames branch from pr/<number> to a more meaningful name' do
subject.should_receive(:git_call).with("branch -m #{branch_name}")
subject.send(:rename_branch,request)
end

end

context 'when the new branch already exists' do

before(:each) do
local.stub(:branch_exists?).and_return(true)
end

it 'checks out that branch instead' do
subject.should_receive(:git_call).with("checkout #{branch_name}")
subject.send(:rename_branch,request)
end

end

end

end
Expand Down

0 comments on commit cc76ac6

Please sign in to comment.