davetron5000-grit 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. data/API.txt +101 -0
  2. data/History.txt +49 -0
  3. data/LICENSE +22 -0
  4. data/README.md +216 -0
  5. data/VERSION.yml +4 -0
  6. data/examples/ex_add_commit.rb +13 -0
  7. data/examples/ex_index.rb +14 -0
  8. data/lib/grit/actor.rb +41 -0
  9. data/lib/grit/blame.rb +61 -0
  10. data/lib/grit/blob.rb +126 -0
  11. data/lib/grit/commit.rb +242 -0
  12. data/lib/grit/commit_stats.rb +128 -0
  13. data/lib/grit/config.rb +44 -0
  14. data/lib/grit/diff.rb +70 -0
  15. data/lib/grit/errors.rb +7 -0
  16. data/lib/grit/git-ruby/commit_db.rb +52 -0
  17. data/lib/grit/git-ruby/file_index.rb +193 -0
  18. data/lib/grit/git-ruby/git_object.rb +350 -0
  19. data/lib/grit/git-ruby/internal/file_window.rb +58 -0
  20. data/lib/grit/git-ruby/internal/loose.rb +137 -0
  21. data/lib/grit/git-ruby/internal/pack.rb +382 -0
  22. data/lib/grit/git-ruby/internal/raw_object.rb +37 -0
  23. data/lib/grit/git-ruby/object.rb +325 -0
  24. data/lib/grit/git-ruby/repository.rb +736 -0
  25. data/lib/grit/git-ruby.rb +186 -0
  26. data/lib/grit/git.rb +140 -0
  27. data/lib/grit/index.rb +122 -0
  28. data/lib/grit/lazy.rb +33 -0
  29. data/lib/grit/merge.rb +45 -0
  30. data/lib/grit/ref.rb +99 -0
  31. data/lib/grit/repo.rb +447 -0
  32. data/lib/grit/ruby1.9.rb +7 -0
  33. data/lib/grit/status.rb +151 -0
  34. data/lib/grit/submodule.rb +88 -0
  35. data/lib/grit/tag.rb +66 -0
  36. data/lib/grit/tree.rb +123 -0
  37. data/lib/grit.rb +68 -0
  38. data/lib/open3_detach.rb +46 -0
  39. data/test/bench/benchmarks.rb +126 -0
  40. data/test/helper.rb +18 -0
  41. data/test/profile.rb +21 -0
  42. data/test/suite.rb +6 -0
  43. data/test/test_actor.rb +51 -0
  44. data/test/test_blame.rb +32 -0
  45. data/test/test_blame_tree.rb +33 -0
  46. data/test/test_blob.rb +83 -0
  47. data/test/test_commit.rb +206 -0
  48. data/test/test_commit_stats.rb +33 -0
  49. data/test/test_commit_write.rb +54 -0
  50. data/test/test_config.rb +58 -0
  51. data/test/test_diff.rb +18 -0
  52. data/test/test_file_index.rb +56 -0
  53. data/test/test_git.rb +84 -0
  54. data/test/test_grit.rb +32 -0
  55. data/test/test_head.rb +47 -0
  56. data/test/test_index_status.rb +40 -0
  57. data/test/test_merge.rb +17 -0
  58. data/test/test_raw.rb +16 -0
  59. data/test/test_real.rb +19 -0
  60. data/test/test_reality.rb +17 -0
  61. data/test/test_remote.rb +14 -0
  62. data/test/test_repo.rb +347 -0
  63. data/test/test_rubygit.rb +188 -0
  64. data/test/test_rubygit_alt.rb +40 -0
  65. data/test/test_rubygit_index.rb +76 -0
  66. data/test/test_rubygit_iv2.rb +28 -0
  67. data/test/test_submodule.rb +69 -0
  68. data/test/test_tag.rb +67 -0
  69. data/test/test_tree.rb +101 -0
  70. metadata +141 -0
data/lib/grit/tag.rb ADDED
@@ -0,0 +1,66 @@
1
+ module Grit
2
+
3
+ class Tag < Ref
4
+ def self.find_all(repo, options = {})
5
+ refs = []
6
+ already = {}
7
+
8
+ Dir.chdir(repo.path) do
9
+ files = Dir.glob(prefix + '/**/*')
10
+
11
+ files.each do |ref|
12
+ next if !File.file?(ref)
13
+
14
+ id = File.read(ref).chomp
15
+ name = ref.sub("#{prefix}/", '')
16
+ commit = commit_from_sha(repo, id)
17
+
18
+ if !already[name]
19
+ refs << self.new(name, commit)
20
+ already[name] = true
21
+ end
22
+ end
23
+
24
+ if File.file?('packed-refs')
25
+ lines = File.readlines('packed-refs')
26
+ lines.each_with_index do |line, i|
27
+ if m = /^(\w{40}) (.*?)$/.match(line)
28
+ next if !Regexp.new('^' + prefix).match(m[2])
29
+ name = m[2].sub("#{prefix}/", '')
30
+
31
+ # Annotated tags in packed-refs include a reference
32
+ # to the commit object on the following line.
33
+ next_line = lines[i+1]
34
+ if next_line && next_line[0] == ?^
35
+ commit = Commit.create(repo, :id => next_line[1..-1].chomp)
36
+ else
37
+ commit = commit_from_sha(repo, m[1])
38
+ end
39
+
40
+ if !already[name]
41
+ refs << self.new(name, commit)
42
+ already[name] = true
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ refs
50
+ end
51
+
52
+ def self.commit_from_sha(repo, id)
53
+ git_ruby_repo = GitRuby::Repository.new(repo.path)
54
+ object = git_ruby_repo.get_object_by_sha1(id)
55
+
56
+ if object.type == :commit
57
+ Commit.create(repo, :id => id)
58
+ elsif object.type == :tag
59
+ Commit.create(repo, :id => object.object)
60
+ else
61
+ raise "Unknown object type."
62
+ end
63
+ end
64
+ end
65
+
66
+ end
data/lib/grit/tree.rb ADDED
@@ -0,0 +1,123 @@
1
+ module Grit
2
+
3
+ class Tree
4
+ lazy_reader :contents
5
+ attr_reader :id
6
+ attr_reader :mode
7
+ attr_reader :name
8
+
9
+ # Construct the contents of the tree
10
+ # +repo+ is the Repo
11
+ # +treeish+ is the reference
12
+ # +paths+ is an optional Array of directory paths to restrict the tree
13
+ #
14
+ # Returns Grit::Tree (baked)
15
+ def self.construct(repo, treeish, paths = [])
16
+ output = repo.git.ls_tree({}, treeish, *paths)
17
+ self.allocate.construct_initialize(repo, treeish, output)
18
+ end
19
+
20
+ def construct_initialize(repo, id, text)
21
+ @repo = repo
22
+ @id = id
23
+ @contents = []
24
+
25
+ text.split("\n").each do |line|
26
+ @contents << content_from_string(repo, line)
27
+ end
28
+ @contents.compact!
29
+
30
+ self
31
+ end
32
+
33
+ def lazy_source
34
+ Tree.construct(@repo, @id, [])
35
+ end
36
+
37
+ # Create an unbaked Tree containing just the specified attributes
38
+ # +repo+ is the Repo
39
+ # +atts+ is a Hash of instance variable data
40
+ #
41
+ # Returns Grit::Tree (unbaked)
42
+ def self.create(repo, atts)
43
+ self.allocate.create_initialize(repo, atts)
44
+ end
45
+
46
+ # Initializer for Tree.create
47
+ # +repo+ is the Repo
48
+ # +atts+ is a Hash of instance variable data
49
+ #
50
+ # Returns Grit::Tree (unbaked)
51
+ def create_initialize(repo, atts)
52
+ @repo = repo
53
+
54
+ atts.each do |k, v|
55
+ instance_variable_set("@#{k}", v)
56
+ end
57
+ self
58
+ end
59
+
60
+ # Parse a content item and create the appropriate object
61
+ # +repo+ is the Repo
62
+ # +text+ is the single line containing the items data in `git ls-tree` format
63
+ #
64
+ # Returns Grit::Blob or Grit::Tree
65
+ def content_from_string(repo, text)
66
+ mode, type, id, name = text.split(" ", 4)
67
+ case type
68
+ when "tree"
69
+ Tree.create(repo, :id => id, :mode => mode, :name => name)
70
+ when "blob"
71
+ Blob.create(repo, :id => id, :mode => mode, :name => name)
72
+ when "link"
73
+ Blob.create(repo, :id => id, :mode => mode, :name => name)
74
+ when "commit"
75
+ Submodule.create(repo, :id => id, :mode => mode, :name => name)
76
+ else
77
+ raise "Invalid type: #{type}"
78
+ end
79
+ end
80
+
81
+ # Find the named object in this tree's contents
82
+ #
83
+ # Examples
84
+ # Repo.new('/path/to/grit').tree/'lib'
85
+ # # => #<Grit::Tree "6cc23ee138be09ff8c28b07162720018b244e95e">
86
+ # Repo.new('/path/to/grit').tree/'README.txt'
87
+ # # => #<Grit::Blob "8b1e02c0fb554eed2ce2ef737a68bb369d7527df">
88
+ #
89
+ # Returns Grit::Blob or Grit::Tree or nil if not found
90
+ def /(file)
91
+ if file =~ /\//
92
+ file.split("/").inject(self) { |acc, x| acc/x } rescue nil
93
+ else
94
+ self.contents.find { |c| c.name == file }
95
+ end
96
+ end
97
+
98
+ def basename
99
+ File.basename(name)
100
+ end
101
+
102
+ # Pretty object inspection
103
+ def inspect
104
+ %Q{#<Grit::Tree "#{@id}">}
105
+ end
106
+
107
+ # Find only Tree objects from contents
108
+ def trees
109
+ contents.select {|v| v.kind_of? Tree}
110
+ end
111
+
112
+ # Find only Blob objects from contents
113
+ def blobs
114
+ contents.select {|v| v.kind_of? Blob}
115
+ end
116
+
117
+ # Compares trees by name
118
+ def <=>(other)
119
+ name <=> other.name
120
+ end
121
+ end # Tree
122
+
123
+ end # Grit
data/lib/grit.rb ADDED
@@ -0,0 +1,68 @@
1
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ # core
4
+ require 'fileutils'
5
+ require 'time'
6
+
7
+ # stdlib
8
+ require 'timeout'
9
+ require 'logger'
10
+ require 'digest/sha1'
11
+
12
+ if defined? RUBY_ENGINE && RUBY_ENGINE == 'jruby'
13
+ require 'open3'
14
+ else
15
+ require 'open3_detach'
16
+ end
17
+
18
+ # third party
19
+ require 'rubygems'
20
+ gem "mime-types", ">=0"
21
+ require 'mime/types'
22
+
23
+ # ruby 1.9 compatibility
24
+ require 'grit/ruby1.9'
25
+
26
+ # internal requires
27
+ require 'grit/lazy'
28
+ require 'grit/errors'
29
+ require 'grit/git-ruby'
30
+ require 'grit/git'
31
+ require 'grit/ref'
32
+ require 'grit/tag'
33
+ require 'grit/commit'
34
+ require 'grit/commit_stats'
35
+ require 'grit/tree'
36
+ require 'grit/blob'
37
+ require 'grit/actor'
38
+ require 'grit/diff'
39
+ require 'grit/config'
40
+ require 'grit/repo'
41
+ require 'grit/index'
42
+ require 'grit/status'
43
+ require 'grit/submodule'
44
+ require 'grit/blame'
45
+ require 'grit/merge'
46
+
47
+
48
+ module Grit
49
+ class << self
50
+ # Set +debug+ to true to log all git calls and responses
51
+ attr_accessor :debug
52
+ attr_accessor :use_git_ruby
53
+ # The standard +logger+ for debugging git calls - this defaults to a plain STDOUT logger
54
+ attr_accessor :logger
55
+ def log(str)
56
+ logger.debug { str }
57
+ end
58
+ end
59
+ self.debug = false
60
+ self.use_git_ruby = true
61
+
62
+ @logger ||= ::Logger.new(STDOUT)
63
+
64
+ def self.version
65
+ yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
66
+ "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"
67
+ end
68
+ end
@@ -0,0 +1,46 @@
1
+ module Open3
2
+ extend self
3
+
4
+ def popen3(*cmd)
5
+ pw = IO::pipe # pipe[0] for read, pipe[1] for write
6
+ pr = IO::pipe
7
+ pe = IO::pipe
8
+
9
+ pid = fork{
10
+ # child
11
+ fork{
12
+ # grandchild
13
+ pw[1].close
14
+ STDIN.reopen(pw[0])
15
+ pw[0].close
16
+
17
+ pr[0].close
18
+ STDOUT.reopen(pr[1])
19
+ pr[1].close
20
+
21
+ pe[0].close
22
+ STDERR.reopen(pe[1])
23
+ pe[1].close
24
+
25
+ exec(*cmd)
26
+ }
27
+ exit!(0)
28
+ }
29
+
30
+ pw[0].close
31
+ pr[1].close
32
+ pe[1].close
33
+ Process.waitpid(pid)
34
+ pi = [pw[1], pr[0], pe[0]]
35
+ pw[1].sync = true
36
+ if defined? yield
37
+ begin
38
+ return yield(*pi)
39
+ ensure
40
+ Process.detach(pid) if pid
41
+ pi.each { |p| p.close unless p.closed? }
42
+ end
43
+ end
44
+ pi
45
+ end
46
+ end
@@ -0,0 +1,126 @@
1
+ require 'fileutils'
2
+ require 'benchmark'
3
+ require 'rubygems'
4
+ require 'ruby-prof'
5
+ require 'memcache'
6
+ require 'pp'
7
+
8
+ gem 'grit', '=0.7.0'
9
+ #require '../../lib/grit'
10
+
11
+ def main
12
+ @wbare = File.expand_path(File.join('../../', 'test', 'dot_git'))
13
+
14
+ in_temp_dir do
15
+ #result = RubyProf.profile do
16
+
17
+ git = Grit::Repo.new('.')
18
+ puts Grit::VERSION
19
+ #Grit::GitRuby.cache_client = MemCache.new 'localhost:11211', :namespace => 'grit'
20
+ #Grit.debug = true
21
+
22
+ #pp Grit::GitRuby.cache_client.stats
23
+
24
+ commit1 = '5e3ee1198672257164ce3fe31dea3e40848e68d5'
25
+ commit2 = 'ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a'
26
+
27
+ Benchmark.bm(8) do |x|
28
+
29
+ run_code(x, 'packobj') do
30
+ @commit = git.commit('5e3ee1198672257164ce3fe31dea3e40848e68d5')
31
+ @tree = git.tree('cd7422af5a2e0fff3e94d6fb1a8fff03b2841881')
32
+ @blob = git.blob('4232d073306f01cf0b895864e5a5cfad7dd76fce')
33
+ @commit.parents[0].parents[0].parents[0]
34
+ end
35
+
36
+ run_code(x, 'commits 1') do
37
+ git.commits.size
38
+ end
39
+
40
+ run_code(x, 'commits 2') do
41
+ log = git.commits('master', 15)
42
+ log.size
43
+ log.size
44
+ log.first
45
+ git.commits('testing').map { |c| c.message }
46
+ end
47
+
48
+ run_code(x, 'big revlist') do
49
+ c = git.commits('master', 200)
50
+ end
51
+
52
+ run_code(x, 'log') do
53
+ log = git.log('master')
54
+ log.size
55
+ log.size
56
+ log.first
57
+ end
58
+
59
+ run_code(x, 'diff') do
60
+ c = git.diff(commit1, commit2)
61
+ end
62
+
63
+ run_code(x, 'commit-diff') do
64
+ c = git.commit_diff(commit1)
65
+ end
66
+
67
+ run_code(x, 'heads') do
68
+ c = git.heads.collect { |b| b.commit.id }
69
+ end
70
+
71
+ # run_code(x, 'config', 100) do
72
+ # c = git.config['user.name']
73
+ # c = git.config['user.email']
74
+ # end
75
+
76
+ #run_code(x, 'commit count') do
77
+ # c = git.commit_count('testing')
78
+ #end
79
+
80
+
81
+ end
82
+ #end
83
+
84
+ #printer = RubyProf::FlatPrinter.new(result)
85
+ #printer.print(STDOUT, 0)
86
+
87
+ end
88
+
89
+
90
+ end
91
+
92
+
93
+ def run_code(x, name, times = 30)
94
+ x.report(name.ljust(12)) do
95
+ for i in 1..times do
96
+ yield i
97
+ end
98
+ end
99
+
100
+ #end
101
+
102
+ # Print a graph profile to text
103
+ end
104
+
105
+ def new_file(name, contents)
106
+ File.open(name, 'w') do |f|
107
+ f.puts contents
108
+ end
109
+ end
110
+
111
+
112
+ def in_temp_dir(remove_after = true)
113
+ filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
114
+ tmp_path = File.join("/tmp/", filename)
115
+ FileUtils.mkdir(tmp_path)
116
+ Dir.chdir tmp_path do
117
+ FileUtils.cp_r(@wbare, File.join(tmp_path, '.git'))
118
+ yield tmp_path
119
+ end
120
+ puts tmp_path
121
+ #FileUtils.rm_r(tmp_path) if remove_after
122
+ end
123
+
124
+ main()
125
+
126
+ ##pp Grit::GitRuby.cache_client.stats
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib grit])
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ gem "mocha", ">=0"
6
+ require 'mocha'
7
+
8
+ GRIT_REPO = File.join(File.dirname(__FILE__), *%w[..])
9
+
10
+ include Grit
11
+
12
+ def fixture(name)
13
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', name))
14
+ end
15
+
16
+ def absolute_project_path
17
+ File.expand_path(File.join(File.dirname(__FILE__), '..'))
18
+ end
data/test/profile.rb ADDED
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib grit])
2
+ include Grit
3
+
4
+ def recurse(tree, indent = "")
5
+ tree.contents.each do |c|
6
+ case c
7
+ when Tree
8
+ # puts "#{indent}#{c.name} (#{c.id})"
9
+ recurse(c, indent + " ")
10
+ end
11
+ end
12
+ end
13
+
14
+ 10.times do
15
+ r = Repo.new("/Users/schacon/projects/ambition")
16
+ t = r.tree
17
+
18
+ recurse(t)
19
+ end
20
+
21
+ #500.times { puts `git --git-dir /Users/schacon/projects/ambition/.git ls-tree master` }
data/test/suite.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+
3
+ tests = Dir["#{File.dirname(__FILE__)}/test_*.rb"]
4
+ tests.each do |file|
5
+ require file
6
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestActor < Test::Unit::TestCase
4
+ def setup
5
+
6
+ end
7
+
8
+ # from_string
9
+
10
+ def test_from_string_should_separate_name_and_email
11
+ a = Actor.from_string("Tom Werner <tom@example.com>")
12
+ assert_equal "Tom Werner", a.name
13
+ assert_equal "tom@example.com", a.email
14
+ end
15
+
16
+ def test_from_string_should_handle_just_name
17
+ a = Actor.from_string("Tom Werner")
18
+ assert_equal "Tom Werner", a.name
19
+ assert_equal nil, a.email
20
+ end
21
+
22
+ # inspect
23
+
24
+ def test_inspect
25
+ a = Actor.from_string("Tom Werner <tom@example.com>")
26
+ assert_equal %Q{#<Grit::Actor "Tom Werner <tom@example.com>">}, a.inspect
27
+ end
28
+
29
+ # to_s
30
+
31
+ def test_to_s_should_alias_name
32
+ a = Actor.from_string("Tom Werner <tom@example.com>")
33
+ assert_equal a.name, a.to_s
34
+ end
35
+
36
+ # to_git_format
37
+
38
+ def test_to_git_format_should_match_from_string
39
+ string = "Tom Werner <tom@example.com>"
40
+ a = Actor.from_string(string)
41
+ assert_equal(string,a.to_git_format)
42
+ end
43
+
44
+ # to_git_format
45
+
46
+ def test_to_git_format_should_match_from_string_with_nil_email
47
+ string = "Tom Werner"
48
+ a = Actor.from_string(string)
49
+ assert_equal(string,a.to_git_format)
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'pp'
3
+
4
+ class TestBlame < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @r = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git]), :is_bare => true)
8
+ end
9
+
10
+ def test_simple_blame
11
+ commit = '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
12
+ blame = @r.blame('History.txt', commit)
13
+ assert_equal 5, blame.lines.size
14
+ line = blame.lines[2]
15
+ assert_equal '* 1 major enhancement', line.line
16
+ assert_equal 3, line.lineno
17
+ assert_equal 3, line.oldlineno
18
+ assert_equal '634396b2f541a9f2d58b00be1a07f0c358b999b3', line.commit.id
19
+ end
20
+
21
+ def test_depth_blame
22
+ commit = '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
23
+ blame = @r.blame('lib/grit.rb', commit)
24
+ assert_equal 37, blame.lines.size
25
+ line = blame.lines[24]
26
+ assert_equal "require 'grit/diff'", line.line
27
+ assert_equal 25, line.lineno
28
+ assert_equal 16, line.oldlineno
29
+ assert_equal '46291865ba0f6e0c9818b11be799fe2db6964d56', line.commit.id
30
+ end
31
+
32
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'pp'
3
+
4
+ class TestBlameTree < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @git = Git.new(File.join(File.dirname(__FILE__), *%w[dot_git]))
8
+ end
9
+
10
+ def test_blame_tree
11
+ commit = '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
12
+ tree = @git.blame_tree(commit)
13
+ last_commit_sha = tree['History.txt']
14
+ assert_equal last_commit_sha, '7bcc0ee821cdd133d8a53e8e7173a334fef448aa'
15
+ end
16
+
17
+ def test_blame_tree_path
18
+ commit = '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
19
+ tree = @git.blame_tree(commit, 'lib')
20
+ last_commit_sha = tree['lib/grit.rb']
21
+ assert_equal last_commit_sha, '5a0943123f6872e75a9b1dd0b6519dd42a186fda'
22
+ last_commit_sha = tree['lib/grit']
23
+ assert_equal last_commit_sha, '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
24
+ end
25
+
26
+ def test_blame_tree_multi_path
27
+ commit = '2d3acf90f35989df8f262dc50beadc4ee3ae1560'
28
+ tree = @git.blame_tree(commit, 'lib/grit')
29
+ last_commit_sha = tree['lib/grit/diff.rb']
30
+ assert_equal last_commit_sha, '22825175e37f22c9418d756ca69b574d75602994'
31
+ end
32
+
33
+ end
data/test/test_blob.rb ADDED
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestBlob < Test::Unit::TestCase
4
+ def setup
5
+ @r = Repo.new(GRIT_REPO)
6
+ @b = Blob.allocate
7
+ end
8
+
9
+ # blob
10
+ def test_nosuch_blob
11
+ t = @r.blob('blahblah')
12
+ assert t.is_a?(Blob)
13
+ end
14
+
15
+ def test_data_should_return_blob_contents
16
+ Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob'))
17
+ blob = Blob.create(@r, :id => 'abc')
18
+ assert_equal "Hello world", blob.data
19
+ end
20
+
21
+ def test_data_should_cache
22
+ Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob')).times(1)
23
+ blob = Blob.create(@r, :id => 'abc')
24
+ blob.data
25
+ blob.data
26
+ end
27
+
28
+ # size
29
+
30
+ def test_size_should_return_file_size
31
+ Git.any_instance.expects(:cat_file).returns(fixture('cat_file_blob_size'))
32
+ blob = Blob.create(@r, :id => 'abc')
33
+ assert_equal 11, blob.size
34
+ end
35
+
36
+ # data
37
+
38
+ # mime_type
39
+
40
+ def test_mime_type_should_return_mime_type_for_known_types
41
+ blob = Blob.create(@r, :id => 'abc', :name => 'foo.png')
42
+ assert_equal "image/png", blob.mime_type
43
+ end
44
+
45
+ def test_mime_type_should_return_text_plain_for_unknown_types
46
+ blob = Blob.create(@r, :id => 'abc')
47
+ assert_equal "text/plain", blob.mime_type
48
+ end
49
+
50
+ # blame
51
+
52
+ def test_blame
53
+ Git.any_instance.expects(:blame).returns(fixture('blame'))
54
+ b = Blob.blame(@r, 'master', 'lib/grit.rb')
55
+ assert_equal 13, b.size
56
+ assert_equal 25, b.inject(0) { |acc, x| acc + x.last.size }
57
+ assert_equal b[0].first.object_id, b[9].first.object_id
58
+ c = b.first.first
59
+ c.expects(:__bake__).times(0)
60
+ assert_equal '634396b2f541a9f2d58b00be1a07f0c358b999b3', c.id
61
+ assert_equal 'Tom Preston-Werner', c.author.name
62
+ assert_equal 'tom@mojombo.com', c.author.email
63
+ assert_equal Time.at(1191997100), c.authored_date
64
+ assert_equal 'Tom Preston-Werner', c.committer.name
65
+ assert_equal 'tom@mojombo.com', c.committer.email
66
+ assert_equal Time.at(1191997100), c.committed_date
67
+ assert_equal 'initial grit setup', c.message
68
+ # c.expects(:__bake__).times(1)
69
+ # assert_equal Tree, c.tree.class
70
+ end
71
+
72
+ # inspect
73
+
74
+ def test_inspect
75
+ @b = Blob.create(@r, :id => 'abc')
76
+ assert_equal %Q{#<Grit::Blob "abc">}, @b.inspect
77
+ end
78
+
79
+ def test_basename
80
+ @b = Blob.create(@r, :name => 'foo/bar.rb')
81
+ assert_equal "bar.rb", @b.basename
82
+ end
83
+ end