Skip to content

Commit abcb145

Browse files
author
scott Chacon
committed
added branch and checkout functionality
1 parent 3cb57d8 commit abcb145

File tree

8 files changed

+137
-23
lines changed

8 files changed

+137
-23
lines changed

EXAMPLES

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,33 @@ end
9797
g.reset # defaults to HEAD
9898
g.reset_hard(Git::Commit)
9999

100-
101-
***** IMPLEMENTED *****
102-
103-
g.branch('new_branch')
100+
g.branch('new_branch') # creates new or fetches existing
101+
g.branch('new_branch').checkout
104102
g.branch('new_branch').delete
103+
g.branch('existing_branch').checkout
105104

106105
g.checkout('new_branch')
107-
g.checkout('new_branch', :create_branch => true)
108-
g.checkout_b('new_branch')
106+
g.checkout(g.branch('new_branch'))
107+
108+
109+
***** IMPLEMENTED *****
110+
111+
g.branch(name).merge(branch2)
112+
g.branch(branch2).merge
109113

110114
g.merge('new_branch')
111115
g.merge(Git::Branch)
112116
g.merge(Git::Branch, Git::Branch)
117+
g.merge(Git::Remote)
118+
119+
g.add_remote(uri, name) # Git::Remote
120+
g.remotes
121+
g.remote(name).fetch
122+
g.remote(name).merge
123+
g.remote(name).merge(branch)
113124

114125
g.fetch
115-
g.fetch(Git::Repo)
126+
g.fetch(Git::Remote)
116127

117128
g.pull
118129
g.pull(Git::Repo, Git::Branch) # fetch and a merge

lib/git/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def branches
124124
Git::Branches.new(self)
125125
end
126126

127+
def branch(branch_name = 'master')
128+
Git::Branch.new(self, branch_name)
129+
end
130+
131+
127132
def lib
128133
Git::Lib.new(self)
129134
end
@@ -162,6 +167,10 @@ def commit_all(message, opts = {})
162167
opts = {:add_all => true}.merge(opts)
163168
self.lib.commit(message, opts)
164169
end
170+
171+
def checkout(branch, opts = {})
172+
self.lib.checkout(branch, opts)
173+
end
165174

166175
# convenience methods
167176

lib/git/branch.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
module Git
22
class Branch < Path
33

4-
attr_accessor :full, :remote, :name, :current
4+
attr_accessor :full, :remote, :name
55

66
@base = nil
77
@gcommit = nil
88

9-
def initialize(base, name, current = false)
9+
def initialize(base, name)
1010
@remote = nil
1111
@full = name
1212
@base = base
13-
@current = current
1413

1514
parts = name.split('/')
1615
if parts[1]
@@ -26,5 +25,36 @@ def gcommit
2625
@gcommit
2726
end
2827

28+
def checkout
29+
check_if_create
30+
@base.lib.checkout(@name)
31+
end
32+
33+
def create
34+
check_if_create
35+
end
36+
37+
def delete
38+
@base.lib.branch_delete(@name)
39+
end
40+
41+
def current
42+
determine_current
43+
end
44+
45+
def to_s
46+
@name
47+
end
48+
49+
private
50+
51+
def check_if_create
52+
@base.lib.branch_new(@name) rescue nil
53+
end
54+
55+
def determine_current
56+
@base.lib.branch_current == @name
57+
end
58+
2959
end
3060
end

lib/git/branches.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(base)
1313
@base = base
1414

1515
@base.lib.branches_all.each do |b|
16-
@branches[b[0]] = Git::Branch.new(@base, b[0], b[1])
16+
@branches[b[0]] = Git::Branch.new(@base, b[0])
1717
end
1818
end
1919

lib/git/lib.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def branches_all
9393
arr
9494
end
9595

96+
def branch_current
97+
branches_all.select { |b| b[1] }.first[0] rescue nil
98+
end
99+
100+
96101
# returns hash
97102
# [tree-ish] = [[line_no, match], [line_no, match2]]
98103
# [tree-ish] = [[line_no, match], [line_no, match2]]
@@ -235,6 +240,22 @@ def reset(commit, opts = {})
235240
command('reset', arr_opts)
236241
end
237242

243+
244+
def branch_new(branch)
245+
command('branch', branch)
246+
end
247+
248+
def branch_delete(branch)
249+
command('branch', ['-d', branch])
250+
end
251+
252+
def checkout(branch, opts = {})
253+
arr_opts = []
254+
arr_opts << '-f' if opts[:force]
255+
arr_opts << branch.to_s
256+
257+
command('checkout', arr_opts)
258+
end
238259

239260
private
240261

tests/test_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,17 @@ def in_temp_dir(remove_after = true)
2929
FileUtils.rm_r(tmp_path) if remove_after
3030
end
3131

32+
33+
def new_file(name, contents)
34+
File.open(name, 'w') do |f|
35+
f.puts contents
36+
end
37+
end
38+
39+
def append_file(name, contents)
40+
File.open(name, 'a') do |f|
41+
f.puts contents
42+
end
43+
end
44+
3245
end

tests/units/test_branch.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,46 @@ def test_branch_commit
4646
assert_equal(270, @git.branches[:test_branches].gcommit.size)
4747
end
4848

49+
def test_branch_create_and_switch
50+
in_temp_dir do |path|
51+
g = Git.clone(@wbare, 'branch_test')
52+
Dir.chdir('branch_test') do
53+
assert(!g.branch('new_branch').current)
54+
g.branch('other_branch').create
55+
g.branch('new_branch').checkout
56+
assert(g.branch('new_branch').current)
57+
58+
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
59+
60+
new_file('test-file1', 'blahblahblah1')
61+
new_file('test-file2', 'blahblahblah2')
62+
assert(g.status.untracked.assoc('test-file1'))
63+
64+
g.add(['test-file1', 'test-file2'])
65+
assert(!g.status.untracked.assoc('test-file1'))
66+
67+
g.reset
68+
assert(g.status.untracked.assoc('test-file1'))
69+
assert(!g.status.added.assoc('test-file1'))
70+
71+
g.branch('new_branch').delete
72+
assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
73+
74+
g.branch('master').checkout
75+
g.branch('new_branch').delete
76+
assert_equal(0, g.branches.select { |b| b.name == 'new_branch' }.size)
77+
78+
g.checkout('other_branch')
79+
assert(g.branch('other_branch').current)
80+
81+
g.checkout('master')
82+
assert(!g.branch('other_branch').current)
83+
84+
g.checkout(g.branch('other_branch'))
85+
assert(g.branch('other_branch').current)
86+
87+
end
88+
end
89+
end
90+
4991
end

tests/units/test_index_ops.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,4 @@ def test_reset
9191
end
9292
end
9393

94-
def new_file(name, contents)
95-
File.open(name, 'w') do |f|
96-
f.puts contents
97-
end
98-
end
99-
100-
def append_file(name, contents)
101-
File.open(name, 'a') do |f|
102-
f.puts contents
103-
end
104-
end
105-
10694
end

0 commit comments

Comments
 (0)