Skip to content

Commit de33ae2

Browse files
author
scott Chacon
committed
added object content caching
1 parent a6e6c05 commit de33ae2

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

lib/git/object.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ class AbstractObject
1010
attr_accessor :sha, :size, :type, :mode
1111

1212
@base = nil
13-
13+
@contents = nil
14+
1415
def initialize(base, sha)
1516
@base = base
1617
@sha = sha.to_s
1718
@size = @base.lib.object_size(@sha)
1819
setup
1920
end
2021

22+
# caches the contents of this call in memory
2123
def contents
22-
@base.lib.object_contents(@sha)
24+
@contents || @contents = @base.lib.object_contents(@sha)
2325
end
2426

2527
def contents_array

tests/units/test_index_ops.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_add
2323
assert(!g.status.untracked.assoc('test-file'))
2424
assert(!g.status.changed.assoc('example.txt'))
2525

26-
append_file('example.txt', 'hahahaha')
26+
new_file('example.txt', 'hahahaha')
2727
assert(g.status.changed.assoc('example.txt'))
2828

2929
g.add

tests/units/test_object.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_blob
9696
def test_blob_contents
9797
o = @git.object('v2.6:example.txt')
9898
assert_equal('replace with new text', o.contents)
99+
assert_equal('replace with new text', o.contents) # this should be cached
99100
end
100101

101102
def test_revparse

0 commit comments

Comments
 (0)