Skip to content

Commit e806b6b

Browse files
author
scott Chacon
committed
only call rev-parse and cat-file for size and type when needed
1 parent de33ae2 commit e806b6b

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

lib/git/base.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,18 @@ def config(name = nil, value = nil)
144144
def object(objectish)
145145
Git::Object.new(self, objectish)
146146
end
147-
alias_method :gtree, :object
148-
alias_method :gcommit, :object
149-
alias_method :gblob, :object
147+
148+
def gtree(objectish)
149+
Git::Object.new(self, objectish, 'tree')
150+
end
151+
152+
def gcommit(objectish)
153+
Git::Object.new(self, objectish, 'commit')
154+
end
155+
156+
def gcommit(objectish)
157+
Git::Object.new(self, objectish, 'blob')
158+
end
150159

151160
# returns a Git::Log object with count commits
152161
def log(count = 30)
@@ -302,7 +311,7 @@ def tags
302311

303312
# returns a Git::Tag object
304313
def tag(tag_name)
305-
Git::Object.new(self, tag_name, true)
314+
Git::Object.new(self, tag_name, 'tag', true)
306315
end
307316

308317
# creates a new git tag (Git::Tag)

lib/git/object.rb

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,30 @@ class GitTagNameDoesNotExist< StandardError
77
class Object
88

99
class AbstractObject
10-
attr_accessor :sha, :size, :type, :mode
10+
attr_accessor :objectish, :size, :type, :mode
1111

1212
@base = nil
1313
@contents = nil
14+
@size = nil
15+
@sha = nil
1416

15-
def initialize(base, sha)
17+
def initialize(base, objectish)
1618
@base = base
17-
@sha = sha.to_s
18-
@size = @base.lib.object_size(@sha)
19+
@objectish = objectish.to_s
1920
setup
2021
end
21-
22+
23+
def sha
24+
@sha || @sha = @base.lib.revparse(@objectish)
25+
end
26+
27+
def size
28+
@size || @size = @base.lib.object_size(@objectish)
29+
end
30+
2231
# caches the contents of this call in memory
2332
def contents
24-
@contents || @contents = @base.lib.object_contents(@sha)
33+
@contents || @contents = @base.lib.object_contents(@objectish)
2534
end
2635

2736
def contents_array
@@ -33,26 +42,26 @@ def setup
3342
end
3443

3544
def to_s
36-
@sha
45+
sha
3746
end
3847

3948
def grep(string, path_limiter = nil, opts = {})
40-
default = {:object => @sha, :path_limiter => path_limiter}
49+
default = {:object => sha, :path_limiter => path_limiter}
4150
grep_options = default.merge(opts)
4251
@base.lib.grep(string, grep_options)
4352
end
4453

4554
def diff(objectish)
46-
Git::Diff.new(@base, @sha, objectish)
55+
Git::Diff.new(@base, @objectish, objectish)
4756
end
4857

4958
def log(count = 30)
50-
Git::Log.new(@base, count).object(@sha)
59+
Git::Log.new(@base, count).object(@objectish)
5160
end
5261

5362
# creates an archive of this object (tree)
5463
def archive(file = nil, opts = {})
55-
@base.lib.archive(@sha, file, opts)
64+
@base.lib.archive(@objectish, file, opts)
5665
end
5766

5867
def tree?
@@ -126,7 +135,7 @@ def check_tree
126135
if !@trees
127136
@trees = {}
128137
@blobs = {}
129-
data = @base.lib.ls_tree(@sha)
138+
data = @base.lib.ls_tree(@objectish)
130139
data['tree'].each { |k, d| @trees[k] = Tree.new(@base, d[:sha], d[:mode]) }
131140
data['blob'].each { |k, d| @blobs[k] = Blob.new(@base, d[:sha], d[:mode]) }
132141
end
@@ -148,7 +157,7 @@ def message
148157
end
149158

150159
def name
151-
@base.lib.namerev(@sha)
160+
@base.lib.namerev(sha)
152161
end
153162

154163
def gtree
@@ -200,7 +209,7 @@ def setup
200209
# see if this object has been initialized and do so if not
201210
def check_commit
202211
if !@tree
203-
data = @base.lib.commit_data(@sha)
212+
data = @base.lib.commit_data(@objectish)
204213
@committer = Git::Author.new(data['committer'])
205214
@author = Git::Author.new(data['author'])
206215
@tree = Tree.new(@base, data['tree'])
@@ -230,16 +239,17 @@ def setup
230239
class << self
231240
# if we're calling this, we don't know what type it is yet
232241
# so this is our little factory method
233-
def new(base, objectish, is_tag = false)
242+
def new(base, objectish, type = nil, is_tag = false)
234243
if is_tag
235244
sha = base.lib.tag_sha(objectish)
236245
if sha == ''
237246
raise Git::GitTagNameDoesNotExist.new(objectish)
238247
end
239248
return Tag.new(base, sha, objectish)
240249
else
241-
sha = base.lib.revparse(objectish)
242-
type = base.lib.object_type(sha)
250+
if !type
251+
type = base.lib.object_type(objectish)
252+
end
243253
end
244254

245255
klass =
@@ -248,7 +258,7 @@ def new(base, objectish, is_tag = false)
248258
when /commit/: Commit
249259
when /tree/: Tree
250260
end
251-
klass::new(base, sha)
261+
klass::new(base, objectish)
252262
end
253263
end
254264

0 commit comments

Comments
 (0)