Skip to content

Commit 0c908da

Browse files
authored
#push without args should do same as git push with no args (#636)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 2b1974c commit 0c908da

File tree

3 files changed

+132
-9
lines changed

3 files changed

+132
-9
lines changed

lib/git/base.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,8 @@ def fetch(remote = 'origin', opts = {})
374374
#
375375
# @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
376376
#
377-
def push(remote = 'origin', branch = 'master', opts = {})
378-
# Small hack to keep backwards compatibility with the 'push(remote, branch, tags)' method signature.
379-
opts = {:tags => opts} if [true, false].include?(opts)
380-
381-
self.lib.push(remote, branch, opts)
377+
def push(*args, **options)
378+
self.lib.push(*args, **options)
382379
end
383380

384381
# merges one or more branches into the current working branch

lib/git/lib.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,20 +908,36 @@ def fetch(remote, opts)
908908
command('fetch', *arr_opts)
909909
end
910910

911-
def push(remote, branch = 'master', opts = {})
911+
def push(remote = nil, branch = nil, opts = nil)
912+
if opts.nil? && branch.instance_of?(Hash)
913+
opts = branch
914+
branch = nil
915+
end
916+
917+
if opts.nil? && remote.instance_of?(Hash)
918+
opts = remote
919+
remote = nil
920+
end
921+
922+
opts ||= {}
923+
912924
# Small hack to keep backwards compatibility with the 'push(remote, branch, tags)' method signature.
913925
opts = {:tags => opts} if [true, false].include?(opts)
914926

927+
raise ArgumentError, "You must specify a remote if a branch is specified" if remote.nil? && !branch.nil?
928+
915929
arr_opts = []
916930
arr_opts << '--mirror' if opts[:mirror]
917931
arr_opts << '--delete' if opts[:delete]
918932
arr_opts << '--force' if opts[:force] || opts[:f]
919-
arr_opts << remote
933+
arr_opts << remote if remote
934+
arr_opts_with_branch = arr_opts.dup
935+
arr_opts_with_branch << branch if branch
920936

921937
if opts[:mirror]
922-
command('push', *arr_opts)
938+
command('push', *arr_opts_with_branch)
923939
else
924-
command('push', *arr_opts, branch)
940+
command('push', *arr_opts_with_branch)
925941
command('push', '--tags', *arr_opts) if opts[:tags]
926942
end
927943
end

tests/units/test_push.rb

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
require 'test_helper'
2+
3+
class TestPush < Test::Unit::TestCase
4+
test 'push with no args' do
5+
expected_command_line = ['push']
6+
git_cmd = :push
7+
git_cmd_args = []
8+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
9+
end
10+
11+
test 'push with no args and options' do
12+
expected_command_line = ['push', '--force']
13+
git_cmd = :push
14+
git_cmd_args = [force: true]
15+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
16+
end
17+
18+
test 'push with only a remote name' do
19+
expected_command_line = ['push', 'origin']
20+
git_cmd = :push
21+
git_cmd_args = ['origin']
22+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
23+
end
24+
25+
test 'push with only a remote name and options' do
26+
expected_command_line = ['push', '--force', 'origin']
27+
git_cmd = :push
28+
git_cmd_args = ['origin', force: true]
29+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
30+
end
31+
32+
test 'push with only a branch name' do
33+
expected_command_line = ['push', 'master']
34+
git_cmd = :push
35+
git_cmd_args = [nil, 'origin']
36+
37+
in_temp_dir do
38+
git = Git.init('.', initial_branch: 'master')
39+
assert_raises(ArgumentError) { git.push(nil, 'master') }
40+
end
41+
end
42+
43+
test 'push with both remote and branch name' do
44+
expected_command_line = ['push', 'origin', 'master']
45+
git_cmd = :push
46+
git_cmd_args = ['origin', 'master']
47+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
48+
end
49+
50+
test 'push with force: true' do
51+
expected_command_line = ['push', '--force', 'origin', 'master']
52+
git_cmd = :push
53+
git_cmd_args = ['origin', 'master', force: true]
54+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
55+
end
56+
57+
test 'push with f: true' do
58+
expected_command_line = ['push', '--force', 'origin', 'master']
59+
git_cmd = :push
60+
git_cmd_args = ['origin', 'master', f: true]
61+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
62+
end
63+
64+
test 'push with mirror: true' do
65+
expected_command_line = ['push', '--force', 'origin', 'master']
66+
git_cmd = :push
67+
git_cmd_args = ['origin', 'master', f: true]
68+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
69+
end
70+
71+
test 'push with delete: true' do
72+
expected_command_line = ['push', '--delete', 'origin', 'master']
73+
git_cmd = :push
74+
git_cmd_args = ['origin', 'master', delete: true]
75+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
76+
end
77+
78+
test 'push with tags: true' do
79+
expected_command_line = ['push', '--tags', 'origin']
80+
git_cmd = :push
81+
git_cmd_args = ['origin', nil, tags: true]
82+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
83+
end
84+
85+
test 'when push succeeds an error should not be raised' do
86+
in_temp_dir do
87+
Git.init('remote.git', initial_branch: 'master', bare: true)
88+
89+
git = Git.clone('remote.git', 'local')
90+
Dir.chdir 'local' do
91+
File.write('File2.txt', 'hello world')
92+
git.add('File2.txt')
93+
git.commit('Second commit')
94+
assert_nothing_raised { git.push }
95+
end
96+
end
97+
end
98+
99+
test 'when push fails a Git::FailedError should be raised' do
100+
in_temp_dir do
101+
Git.init('remote.git', initial_branch: 'master', bare: true)
102+
103+
git = Git.clone('remote.git', 'local')
104+
Dir.chdir 'local' do
105+
# Pushing when there is nothing to push fails
106+
assert_raises(Git::FailedError) { git.push }
107+
end
108+
end
109+
end
110+
end

0 commit comments

Comments
 (0)