significantly improved log performance
[rubygit.git] / lib / git / log.rb
blobc91538dc34414bbacea33fbb54e49487bd67e174
1 module Git
2   
3   # object that holds the last X commits on given branch
4   class Log
5     include Enumerable
6     
7     @base = nil
8     @commits = nil
9     
10     @object = nil
11     @path = nil
12     @count = nil
13     @since = nil
14     @between = nil
15     
16     @dirty_flag = nil
17     
18     def initialize(base, count = 30)
19       dirty_log
20       @base = base
21       @count = count
22     end
24     def object(objectish)
25       dirty_log
26       @object = objectish
27       return self
28     end
29     
30     def path(path)
31       dirty_log
32       @path = path
33       return self
34     end
35     
36     def since(date)
37       dirty_log
38       @since = date
39       return self
40     end
41     
42     def between(sha1, sha2 = nil)
43       dirty_log
44       @between = [sha1, sha2]
45       return self
46     end
47     
48     def to_s
49       self.map { |c| c.to_s }.join("\n")
50     end
51     
53     # forces git log to run
54     
55     def size
56       check_log
57       @commits.size rescue nil
58     end
59     
60     def each
61       check_log
62       @commits.each do |c|
63         yield c
64       end
65     end
66     
67     def first
68       check_log
69       @commits.first rescue nil
70     end
71     
72     private 
73     
74       def dirty_log
75         @dirty_flag = true
76       end
77       
78       def check_log
79         if @dirty_flag
80           run_log
81           @dirty_flag = false
82         end
83       end
84       
85       # actually run the 'git log' command
86       def run_log      
87         log = @base.lib.log_commits(:count => @count, :object => @object, 
88                                     :path_limiter => @path, :since => @since, :between => @between)
89         @commits = log.map { |l| Git::Object::Commit.new(@base, l) }
90       end
91       
92   end
93   
94 end