summaryrefslogtreecommitdiffstats
path: root/lib/git/log.rb
blob: 9437ea6df5a0d595c563be98b523768e0d96487d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module Git
  
  # object that holds the last X commits on given branch
  class Log
    include Enumerable
    
    @base = nil
    @commits = nil
    
    @object = nil
    @path = nil
    @count = nil
    @since = nil
    @between = nil
    
    @dirty_flag = nil
    
    def initialize(base, count = 30)
      dirty_log
      @base = base
      @count = count
    end

    def object(objectish)
      dirty_log
      @object = objectish
      return self
    end
    
    def path(path)
      dirty_log
      @path = path
      return self
    end
    
    def since(date)
      dirty_log
      @since = date
      return self
    end
    
    def between(sha1, sha2 = nil)
      dirty_log
      @between = [sha1, sha2]
      return self
    end
    
    def to_s
      self.map { |c| c.to_s }.join("\n")
    end
    

    # forces git log to run
    
    def size
      check_log
      @commits.size rescue nil
    end
    
    def each
      check_log
      @commits.each do |c|
        yield c
      end
    end
    
    def first
      check_log
      @commits.first rescue nil
    end
    
    private 
    
      def dirty_log
        @dirty_flag = true
      end
      
      def check_log
        if @dirty_flag
          run_log
          @dirty_flag = false
        end
      end
      
      # actually run the 'git log' command
      def run_log      
        log = @base.lib.full_log_commits(:count => @count, :object => @object, 
                                    :path_limiter => @path, :since => @since, :between => @between)
        @commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
      end
      
  end
  
end