Skip to content

Commit b81ee93

Browse files
author
scott Chacon
committed
added merging functions
1 parent abcb145 commit b81ee93

File tree

6 files changed

+168
-11
lines changed

6 files changed

+168
-11
lines changed

EXAMPLES

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,17 @@ g.branch('existing_branch').checkout
105105
g.checkout('new_branch')
106106
g.checkout(g.branch('new_branch'))
107107

108-
109-
***** IMPLEMENTED *****
110-
111108
g.branch(name).merge(branch2)
112-
g.branch(branch2).merge
109+
g.branch(branch2).merge # merges HEAD with branch2
113110

111+
g.branch(name).in_branch(message) { # add files } # auto-commits
114112
g.merge('new_branch')
115-
g.merge(Git::Branch)
116-
g.merge(Git::Branch, Git::Branch)
117-
g.merge(Git::Remote)
113+
g.merge('origin/remote_branch')
114+
g.merge(b.branch('master'))
115+
g.merge([branch1, branch2])
116+
117+
118+
***** IMPLEMENTED *****
118119

119120
g.add_remote(uri, name) # Git::Remote
120121
g.remotes

lib/git/base.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def reset(commitish = nil, opts = {})
156156

157157
def reset_hard(commitish = nil, opts = {})
158158
opts = {:hard => true}.merge(opts)
159-
self.lib.reset(path, opts)
159+
self.lib.reset(commitish, opts)
160160
end
161161

162162
def commit(message, opts = {})
@@ -172,11 +172,20 @@ def checkout(branch, opts = {})
172172
self.lib.checkout(branch, opts)
173173
end
174174

175+
def merge(branch, message = 'merge')
176+
self.lib.merge(branch, message)
177+
end
178+
175179
# convenience methods
176180

177181
def revparse(objectish)
178182
self.lib.revparse(objectish)
179183
end
184+
185+
def current_branch
186+
self.lib.branch_current
187+
end
188+
180189

181190
end
182191

lib/git/branch.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,24 @@ def gcommit
2727

2828
def checkout
2929
check_if_create
30-
@base.lib.checkout(@name)
30+
@base.checkout(@name)
31+
end
32+
33+
34+
# g.branch('new_branch').in_branch do
35+
# # create new file
36+
# # do other stuff
37+
# return true # auto commits and switches back
38+
# end
39+
def in_branch (message = 'in branch work')
40+
old_current = @base.lib.branch_current
41+
checkout
42+
if yield
43+
@base.commit_all(message)
44+
else
45+
@base.reset_hard
46+
end
47+
@base.checkout(old_current)
3148
end
3249

3350
def create
@@ -42,6 +59,19 @@ def current
4259
determine_current
4360
end
4461

62+
def merge(branch = nil, message = nil)
63+
if branch
64+
in_branch do
65+
@base.merge(branch, message)
66+
false
67+
end
68+
# merge a branch into this one
69+
else
70+
# merge this branch into the current one
71+
@base.merge(@name)
72+
end
73+
end
74+
4575
def to_s
4676
@name
4777
end

lib/git/lib.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ def checkout(branch, opts = {})
257257
command('checkout', arr_opts)
258258
end
259259

260+
def merge(branch, message = nil)
261+
arr_opts = []
262+
arr_opts << ["-m '#{message}'"] if message
263+
arr_opts << branch.to_a.join(' ')
264+
command('merge', arr_opts)
265+
end
266+
267+
260268
private
261269

262270
def command_lines(cmd, opts = {})
@@ -278,7 +286,10 @@ def command(cmd, opts = {})
278286
#puts "git #{cmd} #{opts}"
279287
#puts out
280288
#puts
281-
if $?.exitstatus > 1
289+
if $?.exitstatus > 0
290+
if $?.exitstatus == 1 && out == ''
291+
return ''
292+
end
282293
raise Git::GitExecuteError.new(out)
283294
end
284295
out

tests/units/test_branch.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_branch_create_and_switch
6868
assert(g.status.untracked.assoc('test-file1'))
6969
assert(!g.status.added.assoc('test-file1'))
7070

71-
g.branch('new_branch').delete
71+
assert_raise Git::GitExecuteError do
72+
g.branch('new_branch').delete
73+
end
7274
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
7375

7476
g.branch('master').checkout

tests/units/test_merge.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestMerge < Test::Unit::TestCase
6+
def setup
7+
set_file_paths
8+
end
9+
10+
def test_branch_and_merge
11+
in_temp_dir do |path|
12+
g = Git.clone(@wbare, 'branch_merge_test')
13+
Dir.chdir('branch_merge_test') do
14+
15+
g.branch('new_branch').in_branch('test') do
16+
assert_equal('new_branch', g.current_branch)
17+
new_file('new_file_1', 'hello')
18+
new_file('new_file_2', 'hello')
19+
g.add
20+
true
21+
end
22+
23+
assert_equal('master', g.current_branch)
24+
25+
new_file('new_file_3', 'hello')
26+
g.add
27+
28+
assert(!g.status['new_file_1']) # file is not there
29+
30+
assert(g.branch('new_branch').merge)
31+
assert(g.status['new_file_1']) # file has been merged in
32+
end
33+
end
34+
end
35+
36+
def test_branch_and_merge_two
37+
in_temp_dir do |path|
38+
g = Git.clone(@wbare, 'branch_merge_test')
39+
Dir.chdir('branch_merge_test') do
40+
41+
g.branch('new_branch').in_branch('test') do
42+
assert_equal('new_branch', g.current_branch)
43+
new_file('new_file_1', 'hello')
44+
new_file('new_file_2', 'hello')
45+
g.add
46+
true
47+
end
48+
49+
g.branch('new_branch2').in_branch('test') do
50+
assert_equal('new_branch2', g.current_branch)
51+
new_file('new_file_3', 'hello')
52+
new_file('new_file_4', 'hello')
53+
g.add
54+
true
55+
end
56+
57+
g.branch('new_branch').merge('new_branch2')
58+
assert(!g.status['new_file_3']) # still in master branch
59+
60+
g.branch('new_branch').checkout
61+
assert(g.status['new_file_3']) # file has been merged in
62+
63+
g.branch('master').checkout
64+
g.merge(g.branch('new_branch'))
65+
assert(g.status['new_file_3']) # file has been merged in
66+
67+
end
68+
end
69+
end
70+
71+
def test_branch_and_merge_multiple
72+
in_temp_dir do |path|
73+
g = Git.clone(@wbare, 'branch_merge_test')
74+
Dir.chdir('branch_merge_test') do
75+
76+
g.branch('new_branch').in_branch('test') do
77+
assert_equal('new_branch', g.current_branch)
78+
new_file('new_file_1', 'hello')
79+
new_file('new_file_2', 'hello')
80+
g.add
81+
true
82+
end
83+
84+
g.branch('new_branch2').in_branch('test') do
85+
assert_equal('new_branch2', g.current_branch)
86+
new_file('new_file_3', 'hello')
87+
new_file('new_file_4', 'hello')
88+
g.add
89+
true
90+
end
91+
92+
assert(!g.status['new_file_1']) # still in master branch
93+
assert(!g.status['new_file_3']) # still in master branch
94+
95+
g.merge(['new_branch', 'new_branch2'])
96+
97+
assert(g.status['new_file_1']) # file has been merged in
98+
assert(g.status['new_file_3']) # file has been merged in
99+
100+
end
101+
end
102+
end
103+
104+
end

0 commit comments

Comments
 (0)