Class: Grit::Commit
- Inherits:
-
Object
- Object
- Grit::Commit
- Defined in:
- lib/grit/commit.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.actor(line) ⇒ Object
Parse out the actor (author or committer) info.
-
.count(repo, ref) ⇒ Object
Count the number of commits reachable from this ref
repo
is the Reporef
is the ref from which to begin (SHA1 or name). -
.create(repo, atts) ⇒ Object
Create an unbaked Commit containing just the specified attributes
repo
is the Repoatts
is a Hash of instance variable data. -
.diff(repo, a, b = nil, paths = []) ⇒ Object
Show diffs between two trees:
repo
is the Repoa
is a named commitb
is an optional named commit. -
.find_all(repo, ref, options = {}) ⇒ Object
Find all commits matching the given criteria.
-
.list_from_string(repo, text) ⇒ Object
Parse out commit information into an array of baked Commit objects
repo
is the Repotext
is the text output from the git command (raw format).
Instance Method Summary collapse
- #author_string ⇒ Object
- #committer_string ⇒ Object
-
#create_initialize(repo, atts) ⇒ Object
Initializer for Commit.create
repo
is the Repoatts
is a Hash of instance variable data. - #date ⇒ Object
- #diffs ⇒ Object
- #id_abbrev ⇒ Object
-
#initialize(repo, id, parents, tree, author, authored_date, authored_tz, committer, committed_date, committed_tz, message) ⇒ Commit
constructor
Instantiate a new Commit
id
is the id of the commitparents
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 stringauthored_date
is the authored Timecommitter
is the committer stringcommitted_date
is the committed Timemessage
is an array of commit message lines. -
#inspect ⇒ Object
Pretty object inspection.
- #lazy_source ⇒ Object
- #sha ⇒ Object
- #show ⇒ Object
- #stats ⇒ Object
- #to_hash ⇒ Object
- #to_patch ⇒ Object
-
#to_s ⇒ Object
Convert this Commit to a String which is just the SHA1 id.
Constructor Details
#initialize(repo, id, parents, tree, author, authored_date, authored_tz, committer, committed_date, committed_tz, message) ⇒ Commit
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)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/grit/commit.rb', line 28 def initialize(repo, id, parents, tree, , , , committer, committed_date, committed_tz, ) @repo = repo @id = id @parents = parents.map { |p| Commit.create(repo, :id => p) } @tree = Tree.create(repo, :id => tree) @author = @authored_date = @authored_tz = @committer = committer @committed_date = committed_date @committed_tz = committed_tz @message = .join("\n") @short_message = [0] || '' end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/grit/commit.rb', line 4 def id @id end |
Class Method Details
.actor(line) ⇒ Object
Parse out the actor (author or committer) info
Returns [String (actor name and email), Time (acted at time), String (actor time zone)]
219 220 221 222 |
# File 'lib/grit/commit.rb', line 219 def self.actor(line) m, actor, epoch, tz = *line.match(/^.+? (.*) (\d+) ([-+]\d\d\d\d)$/) [Actor.from_string(actor), Time.at(epoch.to_i), tz] end |
.count(repo, ref) ⇒ Object
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
79 80 81 |
# File 'lib/grit/commit.rb', line 79 def self.count(repo, ref) repo.git.rev_list({}, ref).size / 41 end |
.create(repo, atts) ⇒ Object
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)
53 54 55 |
# File 'lib/grit/commit.rb', line 53 def self.create(repo, atts) self.allocate.create_initialize(repo, atts) end |
.diff(repo, a, b = nil, paths = []) ⇒ Object
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)
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/grit/commit.rb', line 158 def self.diff(repo, a, b = nil, paths = []) if b.is_a?(Array) paths = b b = nil end paths.unshift("--") unless paths.empty? paths.unshift(b) unless b.nil? paths.unshift(a) text = repo.git.diff({:full_index => true}, *paths) Diff.list_from_string(repo, text) end |
.find_all(repo, ref, options = {}) ⇒ Object
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)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/grit/commit.rb', line 91 def self.find_all(repo, ref, = {}) = [:max_count, :skip, :since] = {:pretty => "raw"} = .merge() if ref output = repo.git.rev_list(, ref) else output = repo.git.rev_list(.merge(:all => true)) end self.list_from_string(repo, output) rescue Grit::GitRuby::Repository::NoSuchShaFound [] end |
.list_from_string(repo, text) ⇒ Object
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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/grit/commit.rb', line 117 def self.list_from_string(repo, text) lines = text.split("\n") commits = [] while !lines.empty? id = lines.shift.split.last tree = lines.shift.split.last parents = [] parents << lines.shift.split.last while lines.first =~ /^parent/ , , = self.actor(lines.shift) committer, committed_date, committed_tz = self.actor(lines.shift) # not doing anything with this yet, but it's sometimes there encoding = lines.shift.split.last if lines.first =~ /^encoding/ lines.shift = [] << lines.shift[4..-1] while lines.first =~ /^ {4}/ lines.shift while lines.first && lines.first.empty? commits << Commit.new(repo, id, parents, tree, , , , committer, committed_date, committed_tz, ) end commits end |
Instance Method Details
#author_string ⇒ Object
224 225 226 |
# File 'lib/grit/commit.rb', line 224 def "%s <%s> %s %s" % [.name, .email, .to_i, ] end |
#committer_string ⇒ Object
228 229 230 |
# File 'lib/grit/commit.rb', line 228 def committer_string "%s <%s> %s %s" % [committer.name, committer.email, committed_date.to_i, committed_tz] end |
#create_initialize(repo, atts) ⇒ Object
Initializer for Commit.create
+repo+ is the Repo
+atts+ is a Hash of instance variable data
Returns Grit::Commit (unbaked)
62 63 64 65 66 67 68 |
# File 'lib/grit/commit.rb', line 62 def create_initialize(repo, atts) @repo = repo atts.each do |k, v| instance_variable_set("@#{k}", v) end self end |
#date ⇒ Object
201 202 203 |
# File 'lib/grit/commit.rb', line 201 def date @committed_date end |
#diffs ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/grit/commit.rb', line 180 def diffs if parents.empty? show else self.class.diff(@repo, parents.first.id, @id) end end |
#id_abbrev ⇒ Object
44 45 46 |
# File 'lib/grit/commit.rb', line 44 def id_abbrev @id_abbrev ||= @repo.git.rev_parse({}, self.id).chomp[0, 7] end |
#inspect ⇒ Object
Pretty object inspection
210 211 212 |
# File 'lib/grit/commit.rb', line 210 def inspect %Q{#<Grit::Commit "#{@id}">} end |
#lazy_source ⇒ Object
70 71 72 |
# File 'lib/grit/commit.rb', line 70 def lazy_source self.class.find_all(@repo, @id, {:max_count => 1}).first end |
#sha ⇒ Object
197 198 199 |
# File 'lib/grit/commit.rb', line 197 def sha @id end |
#show ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/grit/commit.rb', line 170 def show diff = @repo.git.show({:full_index => true, :pretty => 'raw'}, @id) if diff =~ /diff --git a/ diff = diff.sub(/.+?(diff --git a)/m, '\1') else diff = '' end Diff.list_from_string(@repo, diff) end |
#stats ⇒ Object
188 189 190 |
# File 'lib/grit/commit.rb', line 188 def stats stats = @repo.commit_stats(self.sha, 1)[0][-1] end |
#to_hash ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/grit/commit.rb', line 232 def to_hash { 'id' => id, 'parents' => parents.map { |p| { 'id' => p.id } }, 'tree' => tree.id, 'message' => , 'author' => { 'name' => .name, 'email' => .email }, 'committer' => { 'name' => committer.name, 'email' => committer.email }, 'authored_date' => .xmlschema, 'committed_date' => committed_date.xmlschema, } end |
#to_patch ⇒ Object
205 206 207 |
# File 'lib/grit/commit.rb', line 205 def to_patch @repo.git.format_patch({'1' => true, :stdout => true}, to_s) end |
#to_s ⇒ Object
Convert this Commit to a String which is just the SHA1 id
193 194 195 |
# File 'lib/grit/commit.rb', line 193 def to_s @id end |