Class Grit::Commit
In: lib/grit/commit.rb
Parent: Object

Methods

actor   author_string   count   create   create_initialize   date   diff   diffs   find_all   id_abbrev   inspect   lazy_source   list_from_string   new   sha   show   stats   to_hash   to_patch   to_s  

Attributes

id  [R] 

Public Class methods

Parse out the actor (author or committer) info

Returns [String (actor name and email), Time (acted at time)]

[Source]

     # File lib/grit/commit.rb, line 213
213:     def self.actor(line)
214:       m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
215:       [Actor.from_string(actor), Time.at(epoch.to_i)]
216:     end

Count the number of commits reachable from this ref

  +repo+ is the Repo
  +ref+ is the ref from which to begin (SHA1 or name)

Returns Integer

[Source]

    # File lib/grit/commit.rb, line 74
74:     def self.count(repo, ref)
75:       repo.git.rev_list({}, ref).size / 41
76:     end

Create an unbaked Commit containing just the specified attributes

  +repo+ is the Repo
  +atts+ is a Hash of instance variable data

Returns Grit::Commit (unbaked)

[Source]

    # File lib/grit/commit.rb, line 48
48:     def self.create(repo, atts)
49:       self.allocate.create_initialize(repo, atts)
50:     end

Show diffs between two trees:

  +repo+ is the Repo
  +a+ is a named commit
  +b+ is an optional named commit.  Passing an array assumes you
    wish to omit the second named commit and limit the diff to the
    given paths.
  +paths* is an array of paths to limit the diff.

Returns Grit::Diff[] (baked)

[Source]

     # File lib/grit/commit.rb, line 152
152:     def self.diff(repo, a, b = nil, paths = [])
153:       if b.is_a?(Array)
154:         paths = b
155:         b     = nil
156:       end
157:       paths.unshift("--") unless paths.empty?
158:       paths.unshift(b)    unless b.nil?
159:       paths.unshift(a)
160:       text = repo.git.diff({:full_index => true}, *paths)
161:       Diff.list_from_string(repo, text)
162:     end

Find all commits matching the given criteria.

  +repo+ is the Repo
  +ref+ is the ref from which to begin (SHA1 or name) or nil for --all
  +options+ is a Hash of optional arguments to git
    :max_count is the maximum number of commits to fetch
    :skip is the number of commits to skip

Returns Grit::Commit[] (baked)

[Source]

     # File lib/grit/commit.rb, line 86
 86:     def self.find_all(repo, ref, options = {})
 87:       allowed_options = [:max_count, :skip, :since]
 88: 
 89:       default_options = {:pretty => "raw"}
 90:       actual_options = default_options.merge(options)
 91: 
 92:       if ref
 93:         output = repo.git.rev_list(actual_options, ref)
 94:       else
 95:         output = repo.git.rev_list(actual_options.merge(:all => true))
 96:       end
 97: 
 98:       self.list_from_string(repo, output)
 99:     rescue Grit::GitRuby::Repository::NoSuchShaFound
100:       []
101:     end

Parse out commit information into an array of baked Commit objects

  +repo+ is the Repo
  +text+ is the text output from the git command (raw format)

Returns Grit::Commit[] (baked)

really should re-write this to be more accepting of non-standard commit messages

  • it broke when ‘encoding’ was introduced - not sure what else might show up

[Source]

     # File lib/grit/commit.rb, line 112
112:     def self.list_from_string(repo, text)
113:       lines = text.split("\n")
114: 
115:       commits = []
116: 
117:       while !lines.empty?
118:         id = lines.shift.split.last
119:         tree = lines.shift.split.last
120: 
121:         parents = []
122:         parents << lines.shift.split.last while lines.first =~ /^parent/
123: 
124:         author, authored_date = self.actor(lines.shift)
125:         committer, committed_date = self.actor(lines.shift)
126: 
127:         # not doing anything with this yet, but it's sometimes there
128:         encoding = lines.shift.split.last if lines.first =~ /^encoding/
129: 
130:         lines.shift
131: 
132:         message_lines = []
133:         message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
134: 
135:         lines.shift while lines.first && lines.first.empty?
136: 
137:         commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
138:       end
139: 
140:       commits
141:     end

Instantiate a new Commit

  +id+ is the id of the commit
  +parents+ is an array of commit ids (will be converted into Commit instances)
  +tree+ is the correspdonding tree id (will be converted into a Tree object)
  +author+ is the author string
  +authored_date+ is the authored Time
  +committer+ is the committer string
  +committed_date+ is the committed Time
  +message+ is an array of commit message lines

Returns Grit::Commit (baked)

[Source]

    # File lib/grit/commit.rb, line 26
26:     def initialize(repo, id, parents, tree, author, authored_date, committer, committed_date, message)
27:       @repo = repo
28:       @id = id
29:       @parents = parents.map { |p| Commit.create(repo, :id => p) }
30:       @tree = Tree.create(repo, :id => tree)
31:       @author = author
32:       @authored_date = authored_date
33:       @committer = committer
34:       @committed_date = committed_date
35:       @message = message.join("\n")
36:       @short_message = message[0] || ''
37:     end

Public Instance methods

[Source]

     # File lib/grit/commit.rb, line 218
218:     def author_string
219:       "%s <%s> %s %+05d" % [author.name, author.email, authored_date.to_i, 800]
220:     end

Initializer for Commit.create

  +repo+ is the Repo
  +atts+ is a Hash of instance variable data

Returns Grit::Commit (unbaked)

[Source]

    # File lib/grit/commit.rb, line 57
57:     def create_initialize(repo, atts)
58:       @repo = repo
59:       atts.each do |k, v|
60:         instance_variable_set("@#{k}", v)
61:       end
62:       self
63:     end

[Source]

     # File lib/grit/commit.rb, line 195
195:     def date
196:       @committed_date
197:     end

[Source]

     # File lib/grit/commit.rb, line 174
174:     def diffs
175:       if parents.empty?
176:         show
177:       else
178:         self.class.diff(@repo, parents.first.id, @id)
179:       end
180:     end

[Source]

    # File lib/grit/commit.rb, line 39
39:     def id_abbrev
40:       @id_abbrev ||= @repo.git.rev_parse({}, self.id).chomp[0, 7]
41:     end

Pretty object inspection

[Source]

     # File lib/grit/commit.rb, line 204
204:     def inspect
205:       %Q{#<Grit::Commit "#{@id}">}
206:     end

[Source]

    # File lib/grit/commit.rb, line 65
65:     def lazy_source
66:       self.class.find_all(@repo, @id, {:max_count => 1}).first
67:     end

[Source]

     # File lib/grit/commit.rb, line 191
191:     def sha
192:       @id
193:     end

[Source]

     # File lib/grit/commit.rb, line 164
164:     def show
165:       diff = @repo.git.show({:full_index => true, :pretty => 'raw'}, @id)
166:       if diff =~ /diff --git a/
167:         diff = diff.sub(/.+?(diff --git a)/m, '\1')
168:       else
169:         diff = ''
170:       end
171:       Diff.list_from_string(@repo, diff)
172:     end

[Source]

     # File lib/grit/commit.rb, line 182
182:     def stats
183:       stats = @repo.commit_stats(self.sha, 1)[0][-1]
184:     end

[Source]

     # File lib/grit/commit.rb, line 222
222:     def to_hash
223:       {
224:         'id'       => id,
225:         'parents'  => parents.map { |p| { 'id' => p.id } },
226:         'tree'     => tree.id,
227:         'message'  => message,
228:         'author'   => {
229:           'name'  => author.name,
230:           'email' => author.email
231:         },
232:         'committer' => {
233:           'name'  => committer.name,
234:           'email' => committer.email
235:         },
236:         'authored_date'  => authored_date.xmlschema,
237:         'committed_date' => committed_date.xmlschema,
238:       }
239:     end

[Source]

     # File lib/grit/commit.rb, line 199
199:     def to_patch
200:       @repo.git.format_patch({'1' => true, :stdout => true}, to_s)
201:     end

Convert this Commit to a String which is just the SHA1 id

[Source]

     # File lib/grit/commit.rb, line 187
187:     def to_s
188:       @id
189:     end

[Validate]