|
1 | 1 | module Git
|
2 | 2 |
|
3 |
| - # object that holds the last X commits on given branch |
| 3 | + # Return the last n commits that match the specified criteria |
| 4 | + # |
| 5 | + # @example The last (default number) of commits |
| 6 | + # git = Git.open('.') |
| 7 | + # Git::Log.new(git) #=> Enumerable of the last 30 commits |
| 8 | + # |
| 9 | + # @example The last n commits |
| 10 | + # Git::Log.new(git).max_commits(50) #=> Enumerable of last 50 commits |
| 11 | + # |
| 12 | + # @example All commits returned by `git log` |
| 13 | + # Git::Log.new(git).max_count(:all) #=> Enumerable of all commits |
| 14 | + # |
| 15 | + # @example All commits that match complex criteria |
| 16 | + # Git::Log.new(git) |
| 17 | + # .max_count(:all) |
| 18 | + # .object('README.md') |
| 19 | + # .since('10 years ago') |
| 20 | + # .between('v1.0.7', 'HEAD') |
| 21 | + # |
| 22 | + # @api public |
| 23 | + # |
4 | 24 | class Log
|
5 | 25 | include Enumerable
|
6 | 26 |
|
7 |
| - def initialize(base, count = 30) |
| 27 | + # Create a new Git::Log object |
| 28 | + # |
| 29 | + # @example |
| 30 | + # git = Git.open('.') |
| 31 | + # Git::Log.new(git) |
| 32 | + # |
| 33 | + # @param base [Git::Base] the git repository object |
| 34 | + # @param max_count [Integer, Symbol, nil] the number of commits to return, or |
| 35 | + # `:all` or `nil` to return all |
| 36 | + # |
| 37 | + # Passing max_count to {#initialize} is equivalent to calling {#max_count} on the object. |
| 38 | + # |
| 39 | + def initialize(base, max_count = 30) |
8 | 40 | dirty_log
|
9 | 41 | @base = base
|
10 |
| - @count = count |
| 42 | + max_count(max_count) |
| 43 | + end |
| 44 | + |
| 45 | + # The maximum number of commits to return |
| 46 | + # |
| 47 | + # @example All commits returned by `git log` |
| 48 | + # git = Git.open('.') |
| 49 | + # Git::Log.new(git).max_count(:all) |
| 50 | + # |
| 51 | + # @param num_or_all [Integer, Symbol, nil] the number of commits to return, or |
| 52 | + # `:all` or `nil` to return all |
| 53 | + # |
| 54 | + # @return [self] |
| 55 | + # |
| 56 | + def max_count(num_or_all) |
| 57 | + dirty_log |
| 58 | + @max_count = (num_or_all == :all) ? nil : num_or_all |
| 59 | + self |
11 | 60 | end
|
12 | 61 |
|
| 62 | + # Adds the --all flag to the git log command |
| 63 | + # |
| 64 | + # This asks for the logs of all refs (basically all commits reachable by HEAD, |
| 65 | + # branches, and tags). This does not control the maximum number of commits |
| 66 | + # returned. To control how many commits are returned, call {#max_count}. |
| 67 | + # |
| 68 | + # @example Return the last 50 commits reachable by all refs |
| 69 | + # git = Git.open('.') |
| 70 | + # Git::Log.new(git).max_count(50).all |
| 71 | + # |
| 72 | + # @return [self] |
| 73 | + # |
13 | 74 | def all
|
14 | 75 | dirty_log
|
15 | 76 | @all = true
|
@@ -119,7 +180,7 @@ def check_log
|
119 | 180 | # actually run the 'git log' command
|
120 | 181 | def run_log
|
121 | 182 | log = @base.lib.full_log_commits(
|
122 |
| - count: @count, all: @all, object: @object, path_limiter: @path, since: @since, |
| 183 | + count: @max_count, all: @all, object: @object, path_limiter: @path, since: @since, |
123 | 184 | author: @author, grep: @grep, skip: @skip, until: @until, between: @between,
|
124 | 185 | cherry: @cherry
|
125 | 186 | )
|
|
0 commit comments