Commit 98ce6ae1e8028894355842ab657d84d4a6dec059

cleanup branch command. lazy caching of branch variables.

Commit diff

Support/lib/commands/branch.rb

 
11class SCM::Git::Branch < SCM::Git::CommandProxyBase
22 def [](name)
3 SCM::Git::BranchProxy.new(@base, self, name)
3 SCM::Git::Branch::Proxy.new(@base, self, name)
44 end
55
66 def create_and_switch(name)
2929 result
3030 end
3131
32 def all_for_local_or_remote(side)
33 list(side).map do |branch_params|
34 BranchProxy.new(@base, self, branch_params[:name], :current => branch_params[:default], :local => (side==:local))
35 end
36 end
37
3238 def all(which = [:local, :remote])
3339 branches = []
3440 [which].flatten.each do |side|
35 branches.concat list(which).map { |branch_params|
36 next if branch_params[:name] == "(no branch)"
37 SCM::Git::BranchProxy.new(@base, self, branch_params[:name], :current => branch_params[:default], :local => (side==:local))
38 }
41 branches.concat all_for_local_or_remote(side)
3942 end
4043
4144 branches.compact
117117 end
118118 { :outcome => outcome, :output => output, :remote => remote, :branch => branch }
119119 end
120end
121
122class SCM::Git::BranchProxy
123 attr_reader :name
124
125 def initialize(base, parent, name, options = {})
126 @base = base
127 @parent = parent
128 @name = name
129 @current = options[:current]
130 @local = options[:local]
131 end
132120
133 def local?
134 @local
135 end
121 class BranchProxy
122 attr_reader :name
136123
137 def current?
138 @current
139 end
124 def initialize(base, parent, name, options = {})
125 @base = base
126 @parent = parent
127 @name = name
128 @current = options[:current]
129 @local = options[:local]
130 end
140131
141 def remote?
142 ! @local
143 end
132 def local?
133 @local
134 end
144135
145 def default?
146 raise "implement me"
147 end
136 def current?
137 @current
138 end
148139
149 def remote
150 @base.config["branch.#{name}.remote"]
151 end
140 def remote?
141 ! @local
142 end
152143
153 def remote=(value)
154 @base.config["branch.#{name}.remote"] = value
155 end
144 def default?
145 raise "implement me"
146 end
156147
157 def merge
158 @base.config["branch.#{name}.merge"]
159 end
148 def remote(reload = false)
149 @remote = nil if reload
150 @remote ||= @base.config["branch.#{name}.remote"]
151 end
160152
161 def merge=(value)
162 @base.config["branch.#{name}.merge"] = value
153 def remote=(value)
154 @remote = nil
155 @base.config["branch.#{name}.remote"] = value
156 end
157
158 def merge(reload = false)
159 @merge = nil if (reload)
160 @merge ||= @base.config["branch.#{name}.merge"]
161 end
162
163 def merge=(value)
164 @merge = nil
165 @base.config["branch.#{name}.merge"] = value
166 end
163167 end
164end
168end
169
toggle raw diff

Support/spec/lib/commands/branch_spec.rb

 
55 @git = Git.new
66 Git.reset_mock!
77 end
8
89 include SpecHelpers
910
1011 it "should list ignore (no branch)" do
1616
1717 @git.branch.list_names.should == ["test_mod"]
1818 end
19
20 describe "listing branches" do
21 before(:each) do
22 Git.command_response["branch"] = <<EOF
23* test_mod
24 master
25EOF
26 Git.command_response["branch", "-r"] = <<EOF
27 origin/alpha
28 origin/test_mod
29 origin/master
30EOF
31 end
32
33 it "should return a list of branches" do
34 @git.branch.all.should have(5).branches
35 end
36
37 it "should filter to the remote branches" do
38 @git.branch.all(:remote).should have(3).branches
39 end
40
41 it "should filter to the local branches" do
42 @git.branch.all(:local).should have(2).branches
43 end
44 end
1945end
toggle raw diff