schacon / grit forked from mojombo/grit

Grit is a Ruby library for extracting information from a git repository in an object oriented manner - this fork tries to intergrate as much pure-ruby functionality as possible

This URL has Read+Write access

grit / lib / grit / commit.rb
d01a4cfa » mojombo 2007-10-10 convert to Grit module, ref... 1 module Grit
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 2
3 class Commit
6ffe5340 » mojombo 2007-10-13 branches is alias of heads 4 attr_reader :id
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 5 lazy_reader :parents
6 lazy_reader :tree
7 lazy_reader :author
8 lazy_reader :authored_date
9 lazy_reader :committer
10 lazy_reader :committed_date
11 lazy_reader :message
ff3f41fe » mojombo 2008-02-24 get full message from commi... 12 lazy_reader :short_message
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 13
6ffe5340 » mojombo 2007-10-13 branches is alias of heads 14 # Instantiate a new Commit
15 # +id+ is the id of the commit
16 # +parents+ is an array of commit ids (will be converted into Commit instances)
17 # +tree+ is the correspdonding tree id (will be converted into a Tree object)
917522c4 » mojombo 2007-10-13 more code comments throughout 18 # +author+ is the author string
19 # +authored_date+ is the authored Time
20 # +committer+ is the committer string
21 # +committed_date+ is the committed Time
ff3f41fe » mojombo 2008-02-24 get full message from commi... 22 # +message+ is an array of commit message lines
917522c4 » mojombo 2007-10-13 more code comments throughout 23 #
24 # Returns Grit::Commit (baked)
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 25 def initialize(repo, id, parents, tree, author, authored_date, committer, committed_date, message)
26 @repo = repo
6ffe5340 » mojombo 2007-10-13 branches is alias of heads 27 @id = id
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 28 @parents = parents.map { |p| Commit.create(repo, :id => p) }
fc128af2 » mojombo 2007-10-13 commit now returns Tree obj... 29 @tree = Tree.create(repo, :id => tree)
6ffe5340 » mojombo 2007-10-13 branches is alias of heads 30 @author = author
31 @authored_date = authored_date
32 @committer = committer
33 @committed_date = committed_date
ff3f41fe » mojombo 2008-02-24 get full message from commi... 34 @message = message.join("\n")
35 @short_message = message[0] || ''
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 36 end
37
b86b48e3 » mojombo 2007-12-28 abbreviated sha1 and archiving 38 def id_abbrev
c7f19905 » schacon 2008-07-08 pulled in changes from github 39 @id_abbrev ||= @repo.git.rev_parse({}, self.id).chomp[0, 7]
b86b48e3 » mojombo 2007-12-28 abbreviated sha1 and archiving 40 end
41
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 42 # Create an unbaked Commit containing just the specified attributes
43 # +repo+ is the Repo
44 # +atts+ is a Hash of instance variable data
45 #
46 # Returns Grit::Commit (unbaked)
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 47 def self.create(repo, atts)
48 self.allocate.create_initialize(repo, atts)
49 end
50
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 51 # Initializer for Commit.create
52 # +repo+ is the Repo
53 # +atts+ is a Hash of instance variable data
54 #
55 # Returns Grit::Commit (unbaked)
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 56 def create_initialize(repo, atts)
57 @repo = repo
58 atts.each do |k, v|
4c596908 » defunkt 2007-10-29 rework lazy some to be more... 59 instance_variable_set("@#{k}", v)
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 60 end
61 self
62 end
63
4c596908 » defunkt 2007-10-29 rework lazy some to be more... 64 def lazy_source
65 self.class.find_all(@repo, @id, {:max_count => 1}).first
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 66 end
67
7235ace8 » mojombo 2008-02-09 add Repo#commit_count 68 # Count the number of commits reachable from this ref
69 # +repo+ is the Repo
70 # +ref+ is the ref from which to begin (SHA1 or name)
71 #
72 # Returns Integer
73 def self.count(repo, ref)
74 repo.git.rev_list({}, ref).strip.split("\n").size
75 end
76
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 77 # Find all commits matching the given criteria.
78 # +repo+ is the Repo
179f9198 » mojombo 2008-03-06 enable --all for Commit.fin... 79 # +ref+ is the ref from which to begin (SHA1 or name) or nil for --all
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 80 # +options+ is a Hash of optional arguments to git
81 # :max_count is the maximum number of commits to fetch
82 # :skip is the number of commits to skip
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 83 #
84 # Returns Grit::Commit[] (baked)
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 85 def self.find_all(repo, ref, options = {})
6f6cb7cd » mojombo 2008-01-25 add commits_since 86 allowed_options = [:max_count, :skip, :since]
c7f19905 » schacon 2008-07-08 pulled in changes from github 87
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 88 default_options = {:pretty => "raw"}
89 actual_options = default_options.merge(options)
90
179f9198 » mojombo 2008-03-06 enable --all for Commit.fin... 91 if ref
92 output = repo.git.rev_list(actual_options, ref)
93 else
94 output = repo.git.rev_list(actual_options.merge(:all => true))
95 end
c7f19905 » schacon 2008-07-08 pulled in changes from github 96
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 97 self.list_from_string(repo, output)
c7f19905 » schacon 2008-07-08 pulled in changes from github 98 rescue Grit::GitRuby::Repository::NoSuchShaFound
99 []
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 100 end
101
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 102 # Parse out commit information into an array of baked Commit objects
103 # +repo+ is the Repo
104 # +text+ is the text output from the git command (raw format)
105 #
106 # Returns Grit::Commit[] (baked)
c7f19905 » schacon 2008-07-08 pulled in changes from github 107 #
108 # really should re-write this to be more accepting of non-standard commit messages
109 # - it broke when 'encoding' was introduced - not sure what else might show up
110 #
3b193020 » mojombo 2007-10-13 big refactor to do lazy loa... 111 def self.list_from_string(repo, text)
f6b34615 » mojombo 2008-02-24 repsect blank lines in comm... 112 lines = text.split("\n")
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 113
114 commits = []
548a8aa8 » schacon 2008-04-19 dude - way faster 115
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 116 while !lines.empty?
117 id = lines.shift.split.last
118 tree = lines.shift.split.last
119
120 parents = []
121 parents << lines.shift.split.last while lines.first =~ /^parent/
122
123 author, authored_date = self.actor(lines.shift)
124 committer, committed_date = self.actor(lines.shift)
125
c7f19905 » schacon 2008-07-08 pulled in changes from github 126 # not doing anything with this yet, but it's sometimes there
127 encoding = lines.shift.split.last if lines.first =~ /^encoding/
128
f6b34615 » mojombo 2008-02-24 repsect blank lines in comm... 129 lines.shift
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 130
f6b34615 » mojombo 2008-02-24 repsect blank lines in comm... 131 message_lines = []
55737f3f » mojombo 2008-02-29 do not strip leading spaces... 132 message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
f6b34615 » mojombo 2008-02-24 repsect blank lines in comm... 133
134 lines.shift while lines.first && lines.first.empty?
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 135
f6b34615 » mojombo 2008-02-24 repsect blank lines in comm... 136 commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 137 end
138
139 commits
140 end
141
c9cf68fc » technoweenie 2008-02-10 allow Grit::Commit.diff to ... 142 # Show diffs between two trees:
143 # +repo+ is the Repo
144 # +a+ is a named commit
145 # +b+ is an optional named commit. Passing an array assumes you
146 # wish to omit the second named commit and limit the diff to the
147 # given paths.
148 # +paths* is an array of paths to limit the diff.
149 #
150 # Returns Grit::Diff[] (baked)
151 def self.diff(repo, a, b = nil, paths = [])
152 if b.is_a?(Array)
153 paths = b
154 b = nil
155 end
156 paths.unshift("--") unless paths.empty?
157 paths.unshift(b) unless b.nil?
158 paths.unshift(a)
159 text = repo.git.diff({:full_index => true}, *paths)
46291865 » mojombo 2007-10-29 implement commit diff 160 Diff.list_from_string(repo, text)
161 end
1d22e6fd » defunkt 2007-10-29 add #diffs to commit object 162
163 def diffs
c45fa0fe » defunkt 2008-01-19 display the first commit of... 164 if parents.empty?
59ddc326 » mojombo 2008-02-09 deal with empty initial commit 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
c45fa0fe » defunkt 2008-01-19 display the first commit of... 171 Diff.list_from_string(@repo, diff)
172 else
173 self.class.diff(@repo, parents.first.id, @id)
174 end
1d22e6fd » defunkt 2007-10-29 add #diffs to commit object 175 end
46291865 » mojombo 2007-10-29 implement commit diff 176
4aa0e11f » mojombo 2007-10-13 add comments and make Comme... 177 # Convert this Commit to a String which is just the SHA1 id
6ffe5340 » mojombo 2007-10-13 branches is alias of heads 178 def to_s
179 @id
180 end
181
c12f398c » mojombo 2007-10-13 add inspects to objects 182 # Pretty object inspection
183 def inspect
184 %Q{#<Grit::Commit "#{@id}">}
185 end
186
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 187 # private
188
917522c4 » mojombo 2007-10-13 more code comments throughout 189 # Parse out the actor (author or committer) info
190 #
191 # Returns [String (actor name and email), Time (acted at time)]
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 192 def self.actor(line)
193 m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
e80bbd2c » mojombo 2007-10-24 add Actor class to encapsul... 194 [Actor.from_string(actor), Time.at(epoch.to_i)]
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 195 end
a55a6983 » defunkt 2008-03-10 add in Commit#to_hash method 196
197 def to_hash
198 {
ad44b88d » defunkt 2008-03-10 Touch up Commit#to_hash 199 'id' => id,
200 'parents' => parents.map { |p| { 'id' => p.id } },
201 'tree' => tree.id,
202 'message' => message,
203 'author' => {
204 'name' => author.name,
205 'email' => author.email
a55a6983 » defunkt 2008-03-10 add in Commit#to_hash method 206 },
ad44b88d » defunkt 2008-03-10 Touch up Commit#to_hash 207 'committer' => {
208 'name' => committer.name,
209 'email' => committer.email
a55a6983 » defunkt 2008-03-10 add in Commit#to_hash method 210 },
ad44b88d » defunkt 2008-03-10 Touch up Commit#to_hash 211 'authored_date' => authored_date.xmlschema,
212 'committed_date' => committed_date.xmlschema,
a55a6983 » defunkt 2008-03-10 add in Commit#to_hash method 213 }
214 end
2c6af5a4 » mojombo 2007-10-10 implement Repo.commits 215 end # Commit
216
1d22e6fd » defunkt 2007-10-29 add #diffs to commit object 217 end # Grit