Skip to content

Commit 303ffc8

Browse files
author
scott Chacon
committed
changed logging to be far more efficient if you're accessing all the commit objects
1 parent f64d546 commit 303ffc8

File tree

7 files changed

+69
-20
lines changed

7 files changed

+69
-20
lines changed

TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
* more documentation
22

3+
* logging (Git.open(:log => Logger.new))
34

4-
* more low level index actions
5-
- (with_index), checkout-index, read-tree, write-tree, update-ref
65

76
* git revert, stash, rebase
87

benchmark.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'benchmark'
33
require 'rubygems'
44
require 'ruby-prof'
5-
#require_gem 'git', '1.0.3'
6-
require 'lib/git'
5+
require_gem 'git', '1.0.3'
6+
#require 'lib/git'
77

88
def main
99
@wbare = File.expand_path(File.join('tests', 'files', 'working.git'))
@@ -66,8 +66,8 @@ def main
6666
log.size
6767
log.size
6868
log.first
69-
g.log.between('v2.5').object('example.txt').size
70-
g.log.since("2 years ago").size
69+
g.log.between('v2.5').object('example.txt').map { |c| c.message }
70+
g.log.since("2 years ago").map { |c| c.message }
7171
end
7272
end
7373

camping/gitweb.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def get repo_id, tree1, tree2
142142
@git = Git.bare(@repo.path)
143143
@tree1 = tree1
144144
@tree2 = tree2
145-
@diff = @git.diff(tree1, tree2)
145+
@diff = @git.diff(tree2, tree1)
146146
render :diff
147147
end
148148
end
@@ -255,7 +255,7 @@ def commit
255255
@commit.parents.each do |p|
256256
code { a p.sha, :href => R(Commit, @repo, p.sha) }
257257
span.space ' '
258-
a 'diff', :href => R(DiffTwo, @repo, p.sha, @commit.sha)
258+
a 'diff', :href => R(Diff, @repo, p.sha, @commit.sha)
259259
span.space ' '
260260
a 'archive', :href => R(Archive, @repo, p.gtree.sha)
261261
br

lib/git/lib.rb

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ def log_commits(opts = {})
6969
command_lines('log', arr_opts, true).map { |l| l.split.first }
7070
end
7171

72+
def full_log_commits(opts = {})
73+
arr_opts = ['--pretty=raw']
74+
arr_opts << "-#{opts[:count]}" if opts[:count]
75+
arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
76+
arr_opts << "#{opts[:between][0].to_s}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
77+
arr_opts << opts[:object] if opts[:object].is_a? String
78+
arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
79+
80+
full_log = command_lines('log', arr_opts, true)
81+
process_commit_data(full_log)
82+
end
83+
7284
def revparse(string)
7385
command('rev-parse', string)
7486
end
@@ -87,28 +99,52 @@ def object_size(sha)
8799

88100
# returns useful array of raw commit object data
89101
def commit_data(sha)
102+
sha = sha.to_s
103+
cdata = command_lines('cat-file', ['commit', sha])
104+
process_commit_data(cdata, sha)
105+
end
106+
107+
def process_commit_data(data, sha = nil)
90108
in_message = false
91109

92-
hsh = {'message' => '', 'parent' => []}
93-
command_lines('cat-file', ['commit', sha.to_s]).each do |line|
94-
if in_message
110+
if sha
111+
hsh = {'sha' => sha, 'message' => '', 'parent' => []}
112+
else
113+
hsh_array = []
114+
end
115+
116+
data.each do |line|
117+
if in_message && line != ''
95118
hsh['message'] += line + "\n"
96119
end
97-
120+
98121
if (line != '') && !in_message
99122
data = line.split
100123
key = data.shift
101124
value = data.join(' ')
125+
if key == 'commit'
126+
sha = value
127+
hsh_array << hsh if hsh
128+
hsh = {'sha' => sha, 'message' => '', 'parent' => []}
129+
end
102130
if key == 'parent'
103131
hsh[key] << value
104132
else
105133
hsh[key] = value
106134
end
135+
elsif in_message && line == ''
136+
in_message = false
107137
else
108138
in_message = true
109139
end
110140
end
111-
hsh
141+
142+
if hsh_array
143+
hsh_array << hsh if hsh
144+
hsh_array
145+
else
146+
hsh
147+
end
112148
end
113149

114150
def object_contents(sha)

lib/git/log.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def check_log
8484

8585
# actually run the 'git log' command
8686
def run_log
87-
log = @base.lib.log_commits(:count => @count, :object => @object,
87+
log = @base.lib.full_log_commits(:count => @count, :object => @object,
8888
:path_limiter => @path, :since => @since, :between => @between)
89-
@commits = log.map { |l| Git::Object::Commit.new(@base, l) }
89+
@commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
9090
end
9191

9292
end

lib/git/object.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ class Commit < AbstractObject
151151
@committer = nil
152152
@message = nil
153153

154+
def initialize(base, sha, init = nil)
155+
super(base, sha)
156+
if init
157+
set_commit(init)
158+
end
159+
end
160+
154161
def message
155162
check_commit
156163
@message
@@ -199,6 +206,14 @@ def committer_date
199206
def diff_parent
200207
diff(parent)
201208
end
209+
210+
def set_commit(data)
211+
@committer = Git::Author.new(data['committer'])
212+
@author = Git::Author.new(data['author'])
213+
@tree = Tree.new(@base, data['tree'])
214+
@parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
215+
@message = data['message'].chomp
216+
end
202217

203218
private
204219

@@ -210,11 +225,7 @@ def setup
210225
def check_commit
211226
if !@tree
212227
data = @base.lib.commit_data(@objectish)
213-
@committer = Git::Author.new(data['committer'])
214-
@author = Git::Author.new(data['author'])
215-
@tree = Tree.new(@base, data['tree'])
216-
@parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
217-
@message = data['message'].chomp
228+
set_commit(data)
218229
end
219230
end
220231

tests/units/test_lib.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def test_log_commits
4444

4545
a = @lib.log_commits :count => 20, :path_limiter => 'ex_dir/'
4646
assert_equal(1, a.size)
47+
48+
a = @lib.full_log_commits :count => 20
49+
assert_equal(20, a.size)
4750
end
4851

4952
def test_revparse

0 commit comments

Comments
 (0)