Skip to content

Commit cc76ac6

Browse files
author
Bamboo
committed
Merge pull request #79 from xystushi/review_130918_change_default_action_of_checkout
Default action of checkout is to create a local branch.
2 parents 1c455aa + 3132484 commit cc76ac6

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ $ git review browse 42
5555

5656
```
5757
$ git review checkout 42
58-
> checkout changes from request #42 to your local repository in a headless state
58+
> checkout remote branch from request #42 and create a local branch from it
5959
```
6060

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

6666

bin/git-review

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646

4747
desc 'Checkout a request\'s changes to local repo'
4848
command :checkout do |c|
49-
c.switch [:b, :branch]
49+
c.switch [:b, :branch], :default_value => true
5050
c.action do |global, opts, args|
5151
help_now!('Request number is required.') if args.empty?
5252
::GitReview::Commands.checkout(args.shift, opts[:branch])

lib/git-review/commands.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ def browse(number)
4040
end
4141

4242
# Checkout a specified request's changes to your local repository.
43-
def checkout(number, branch=false)
43+
def checkout(number, branch=true)
4444
request = get_request_by_number(number)
4545
puts 'Checking out changes to your local repository.'
4646
puts 'To get back to your original state, just run:'
4747
puts
4848
puts ' git checkout master'
4949
puts
5050
if branch
51-
git_call("checkout #{request.head.ref}")
52-
else
5351
git_call("checkout pr/#{request.number}")
52+
rename_branch(request)
53+
else
54+
git_call("checkout #{request.head.sha}")
5455
end
5556
end
5657

@@ -343,6 +344,18 @@ def get_request_by_number(request_number)
343344
request || (raise ::GitReview::InvalidRequestIDError)
344345
end
345346

347+
def rename_branch(request)
348+
ref = request.head.ref
349+
user = request.head.user.login
350+
number = request.number
351+
new_name = "#{ref}_#{user}_pr_#{number}"
352+
if local.branch_exists?(:local, new_name)
353+
git_call("checkout #{new_name}")
354+
else
355+
git_call("branch -m #{new_name}")
356+
end
357+
end
358+
346359
end
347360

348361
end

spec/git-review/commands_spec.rb

+43-4
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,53 @@
123123
subject.stub(:puts)
124124
end
125125

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

131-
it 'creates a local branch if the optional param --branch is appended' do
132-
subject.should_receive(:git_call).with("checkout #{head_ref}")
133-
subject.checkout(1, true)
132+
it 'creates a headless state if --no-branch is specified' do
133+
subject.stub(:rename_branch)
134+
subject.should_receive(:git_call).with("checkout #{head_sha}")
135+
subject.checkout(1, false)
136+
end
137+
138+
describe '#rename_branch' do
139+
140+
let(:branch_name) {
141+
ref = request.head.ref
142+
user = request.head.user.login
143+
number = request.number
144+
"#{ref}_#{user}_pr_#{number}"
145+
}
146+
147+
context 'when the new branch does not exist' do
148+
149+
before(:each) do
150+
local.stub(:branch_exists?).and_return(false)
151+
end
152+
153+
it 'renames branch from pr/<number> to a more meaningful name' do
154+
subject.should_receive(:git_call).with("branch -m #{branch_name}")
155+
subject.send(:rename_branch,request)
156+
end
157+
158+
end
159+
160+
context 'when the new branch already exists' do
161+
162+
before(:each) do
163+
local.stub(:branch_exists?).and_return(true)
164+
end
165+
166+
it 'checks out that branch instead' do
167+
subject.should_receive(:git_call).with("checkout #{branch_name}")
168+
subject.send(:rename_branch,request)
169+
end
170+
171+
end
172+
134173
end
135174

136175
end

0 commit comments

Comments
 (0)