public
Rubygem
Fork of evilchelu/braid
Description: A simple tool for tracking vendor branches in git
Homepage: http://evil.che.lu/projects/braid
Clone URL: git://github.com/norbert/braid.git
Search Repo:
Refactor operations proxy and move exceptions
norbert (author)
Mon Jul 21 13:00:43 -0700 2008
commit  d20eb483783888a14dc2c594ac733580759ae0fa
tree    7c7d73e970e3725c68cabfd6fb0165caca73cf51
parent  168bf6d60cae8e8f983ef2c60854f8472c6250a7
...
16
17
18
19
20
21
 
 
 
22
23
24
25
26
27
28
29
 
 
 
30
31
32
...
16
17
18
 
 
 
19
20
21
22
 
 
 
 
 
 
 
23
24
25
26
27
28
0
@@ -16,17 +16,13 @@ module Braid
0
       klass = Commands.const_get(command.to_s.capitalize)
0
       klass.new.run(*args)
0
 
0
- rescue Operations::Git::VersionTooLow => error
0
- msg "git version #{REQUIRED_GIT_VERSION} or higher is required, #{error.message} is installed."
0
- msg "Exiting."
0
+ rescue Operations::VersionTooLow => error
0
+ msg "Error: git version too low: #{error.message}"
0
+ exit(1)
0
 
0
- rescue Operations::Git::LocalChangesPresent => error
0
- msg "Local changes are present, commit or stash them before running #{command}."
0
- msg "Exiting."
0
-
0
- rescue => error
0
- #msg "braid error: #{error}"
0
- raise error
0
+ rescue Operations::LocalChangesPresent => error
0
+ msg "Error: local changes are present, commit or stash them before running #{command}"
0
+ exit(1)
0
     end
0
 
0
     def self.msg(str)
...
 
1
2
3
...
5
6
7
8
 
 
 
 
 
9
10
11
12
13
14
 
 
 
 
 
15
16
 
17
18
19
...
40
41
42
43
 
44
45
46
...
81
82
83
84
85
86
87
88
89
90
91
92
93
 
94
95
96
...
212
213
214
215
 
216
217
218
219
220
221
222
223
224
225
 
 
226
227
228
229
230
231
 
 
 
232
233
234
...
244
245
246
247
 
248
249
250
251
252
253
 
254
255
256
...
264
265
266
267
268
269
270
 
 
 
271
272
273
274
 
 
 
275
276
277
278
 
 
 
279
280
281
...
1
2
3
4
...
6
7
8
 
9
10
11
12
13
14
15
16
 
 
 
17
18
19
20
21
22
 
23
24
25
26
...
47
48
49
 
50
51
52
53
...
88
89
90
 
 
 
 
 
 
 
 
 
 
91
92
93
94
...
210
211
212
 
213
214
215
216
 
 
 
 
 
 
 
217
218
219
220
221
 
 
 
222
223
224
225
226
227
...
237
238
239
 
240
241
242
243
 
 
 
244
245
246
247
...
255
256
257
 
 
 
 
258
259
260
261
 
 
 
262
263
264
265
 
 
 
266
267
268
269
270
271
0
@@ -1,3 +1,4 @@
0
+require 'singleton'
0
 require 'rubygems'
0
 require 'open4'
0
 
0
@@ -5,15 +6,21 @@ module Braid
0
   module Operations
0
     class ShellExecutionError < BraidError
0
     end
0
- class VersionError < BraidError
0
+ class VersionTooLow < BraidError
0
+ end
0
+ class UnknownRevision < BraidError
0
+ end
0
+ class LocalChangesPresent < BraidError
0
     end
0
 
0
     # The command proxy is meant to encapsulate commands such as git, git-svn and svn, that work with subcommands.
0
- #
0
- # It is expected that subclasses override the command method, define a COMMAND constant and have a VersionTooLow exception.
0
- class CommandProxy
0
+ class Proxy
0
+ include Singleton
0
+
0
+ def self.command; name.split('::').last.downcase; end # hax!
0
+
0
       def version
0
- status, out, err = exec!("#{self.class::COMMAND} --version")
0
+ status, out, err = exec!("#{self.class.command} --version")
0
         out.sub(/^.* version/, "").strip
0
       end
0
 
0
@@ -40,7 +47,7 @@ module Braid
0
       end
0
 
0
       def require_version!(required)
0
- require_version(required) || raise(self.class::VersionTooLow, version)
0
+ require_version(required) || raise(VersionTooLow, version)
0
       end
0
 
0
       private
0
@@ -81,16 +88,7 @@ module Braid
0
         end
0
     end
0
 
0
- class Git < CommandProxy
0
- COMMAND = "git"
0
-
0
- class UnknownRevision < BraidError
0
- end
0
- class LocalChangesPresent < BraidError
0
- end
0
- class VersionTooLow < VersionError
0
- end
0
-
0
+ class Git < Proxy
0
       def commit(message)
0
         status, out, err = exec("git commit -m #{message.inspect} --no-verify")
0
 
0
@@ -212,23 +210,18 @@ module Braid
0
 
0
       private
0
         def command(name)
0
- "#{COMMAND} #{name.to_s.gsub('_', '-')}"
0
+ "#{self.class.command} #{name.to_s.gsub('_', '-')}"
0
         end
0
     end
0
 
0
- class GitSvn < CommandProxy
0
- COMMAND = "git-svn"
0
-
0
- class UnknownRevision < BraidError
0
- end
0
- class VersionTooLow < VersionError
0
- end
0
+ class GitSvn < Proxy
0
+ def self.command; "git-svn"; end
0
 
0
       def commit_hash(remote, revision)
0
         status, out, err = invoke(:log, "--show-commit --oneline", "-r #{revision}", remote)
0
- part = out.split(" | ")[1]
0
- raise UnknownRevision, "unknown revision: #{revision}" unless part
0
- Git.new.rev_parse(part) # FIXME ugly ugly ugly
0
+ part = out.to_s.split(" | ")[1]
0
+ raise UnknownRevision, "r#{revision}" unless part
0
+ Git.instance.rev_parse(part) # FIXME ugly ugly ugly
0
       end
0
 
0
       def fetch(remote)
0
@@ -244,13 +237,11 @@ module Braid
0
 
0
       private
0
         def command(name)
0
- "#{COMMAND} #{name}"
0
+ "#{self.class.command} #{name}"
0
         end
0
     end
0
 
0
- class Svn < CommandProxy
0
- COMMAND = "svn"
0
-
0
+ class Svn < Proxy
0
       def clean_revision(revision)
0
         revision.to_i if revision
0
       end
0
@@ -264,18 +255,17 @@ module Braid
0
     end
0
 
0
     module VersionControl
0
- private
0
- def git
0
- Git.new
0
- end
0
+ def git
0
+ Git.instance
0
+ end
0
 
0
- def git_svn
0
- GitSvn.new
0
- end
0
+ def git_svn
0
+ GitSvn.instance
0
+ end
0
 
0
- def svn
0
- Svn.new
0
- end
0
+ def svn
0
+ Svn.instance
0
+ end
0
     end
0
   end
0
 end
...
1
2
3
4
5
6
7
8
9
...
1
2
 
 
 
 
3
4
5
0
@@ -1,9 +1,5 @@
0
 require File.dirname(__FILE__) + '/test_helper'
0
 
0
-def build_mirror
0
- Braid::Mirror.new("path", "url" => "url")
0
-end
0
-
0
 describe_shared "Braid::Config, in general" do
0
   db = "tmp.yml"
0
 
...
1
2
3
4
5
6
7
8
9
...
45
46
47
48
 
49
50
51
 
 
52
53
54
55
 
56
57
58
59
60
 
61
62
63
...
70
71
72
73
74
 
75
76
77
...
1
2
 
 
 
 
3
4
5
...
41
42
43
 
44
45
46
 
47
48
49
50
51
 
52
53
54
55
56
 
57
58
59
60
...
67
68
69
 
70
71
72
73
74
0
@@ -1,9 +1,5 @@
0
 require File.dirname(__FILE__) + '/test_helper'
0
 
0
-def new_from_options(url, options = {})
0
- @mirror = Braid::Mirror.new_from_options(url, options)
0
-end
0
-
0
 describe "Braid::Mirror.new_from_options" do
0
   it "should default branch to master" do
0
     new_from_options("git://path")
0
@@ -45,19 +41,20 @@ describe "Braid::Mirror.new_from_options" do
0
   end
0
 end
0
 
0
-describe "Braid::Mirror#local_changes?" do
0
+describe "Braid::Mirror#diff" do
0
   before(:each) do
0
     @mirror = new_from_options("git://path")
0
- Braid::Operations::Git.any_instance.expects(:rev_parse)
0
+ git.stubs(:rev_parse)
0
+ git.stubs(:tree_hash)
0
   end
0
 
0
   it "should return true when the diff is not empty" do
0
- Braid::Operations::Git.any_instance.expects(:diff_tree).returns("diff --git a/path b/path\n")
0
+ git.expects(:diff_tree).returns("diff --git a/path b/path\n")
0
     @mirror.local_changes?.should == true
0
   end
0
 
0
   it "should return false when the diff is empty" do
0
- Braid::Operations::Git.any_instance.expects(:diff_tree).returns("")
0
+ git.expects(:diff_tree).returns("")
0
     @mirror.local_changes?.should == false
0
   end
0
 end
0
@@ -70,8 +67,8 @@ describe "Braid::Mirror#base_revision" do
0
   end
0
 
0
   it "should be the parsed hash for git mirrors" do
0
- Braid::Operations::Git.any_instance.expects(:rev_parse).returns('a' * 40)
0
     @mirror = Braid::Mirror.new("path", "revision" => ('a' * 7))
0
+ git.expects(:rev_parse).returns('a' * 40)
0
     @mirror.send(:base_revision).should == 'a' * 40
0
   end
0
 end
...
1
2
3
4
5
6
7
8
...
21
22
23
24
25
26
27
28
 
29
30
31
32
33
34
35
 
 
36
37
38
39
40
41
42
43
44
45
 
46
47
48
...
51
52
53
54
55
56
57
58
59
60
61
 
62
63
64
...
1
2
3
 
 
4
5
6
...
19
20
21
 
 
22
23
 
24
25
26
27
28
29
 
 
30
31
32
33
34
35
 
 
36
37
38
 
39
40
41
42
...
45
46
47
 
 
48
49
50
51
52
 
53
54
55
56
0
@@ -1,8 +1,6 @@
0
 require File.dirname(__FILE__) + '/test_helper'
0
 
0
 describe "Braid::Operations::Git#remote_exists?" do
0
- include Braid::Operations::VersionControl
0
-
0
   before(:each) do
0
     File.expects(:readlines).returns(["[remote \"braid/git/one\"]\n", "[svn-remote \"braid/git/two\"]\n"])
0
   end
0
@@ -21,28 +19,24 @@ describe "Braid::Operations::Git#remote_exists?" do
0
 end
0
 
0
 describe "Braid::Operations::Git#rev_parse" do
0
- include Braid::Operations::VersionControl
0
-
0
   it "should return the full hash when a hash is found" do
0
     full_revision = 'a' * 40
0
- Braid::Operations::Git.any_instance.expects(:exec).returns([0, full_revision, ""])
0
+ git.expects(:exec).returns([0, full_revision, ""])
0
     git.rev_parse('a' * 7).should == full_revision
0
   end
0
 
0
   it "should raise a revision error when the hash is not found" do
0
     ambiguous_revision = 'b' * 7
0
- Braid::Operations::Git.any_instance.expects(:exec).returns([1, ambiguous_revision, "fatal: ..."])
0
- lambda { git.rev_parse(ambiguous_revision) }.should.raise(Braid::Operations::Git::UnknownRevision)
0
+ git.expects(:exec).returns([1, ambiguous_revision, "fatal: ..."])
0
+ lambda { git.rev_parse(ambiguous_revision) }.should.raise(Braid::Operations::UnknownRevision)
0
   end
0
 end
0
 
0
 describe "Braid::Operations::Git#version" do
0
- include Braid::Operations::VersionControl
0
-
0
   ACTUAL_VERSION = "1.5.5.1.98.gf0ec4"
0
 
0
   before(:each) do
0
- Braid::Operations::Git.any_instance.expects(:exec).returns([0, "git version #{ACTUAL_VERSION}\n", ""])
0
+ git.expects(:exec).returns([0, "git version #{ACTUAL_VERSION}\n", ""])
0
   end
0
 
0
   it "should extract from git --version output" do
0
@@ -51,14 +45,12 @@ describe "Braid::Operations::Git#version" do
0
 end
0
 
0
 describe "Braid::Operations::Git#require_version" do
0
- include Braid::Operations::VersionControl
0
-
0
   REQUIRED_VERSION = "1.5.4.5"
0
   PASS_VERSIONS = %w(1.5.4.6 1.5.5 1.6 1.5.4.5.2 1.5.5.1.98.gf0ec4)
0
   FAIL_VERSIONS = %w(1.5.4.4 1.5.4 1.5.3 1.4.5.6)
0
 
0
   def set_version(str)
0
- Braid::Operations::Git.any_instance.expects(:exec).returns([0, "git version #{str}\n", ""])
0
+ git.expects(:exec).returns([0, "git version #{str}\n", ""])
0
   end
0
 
0
   it "should return true for higher revisions" do
...
3
4
5
 
 
 
 
 
 
 
 
 
 
...
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -3,3 +3,13 @@ require 'test/spec'
0
 require 'mocha'
0
 
0
 require File.dirname(__FILE__) + '/../lib/braid'
0
+
0
+def new_from_options(url, options = {})
0
+ @mirror = Braid::Mirror.new_from_options(url, options)
0
+end
0
+
0
+def build_mirror
0
+ Braid::Mirror.new("path", "url" => "url")
0
+end
0
+
0
+include Braid::Operations::VersionControl

Comments

    No one has commented yet.