trans-grit 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/grit/actor.rb ADDED
@@ -0,0 +1,52 @@
1
+ module Grit
2
+
3
+ class Actor
4
+ attr_reader :name
5
+ attr_reader :email
6
+
7
+ def initialize(name, email)
8
+ @name = name
9
+ @email = email
10
+ end
11
+ alias_method :to_s, :name
12
+
13
+ # Create an Actor from a string.
14
+ #
15
+ # str - The String in this format: 'John Doe <jdoe@example.com>'
16
+ #
17
+ # Returns Git::Actor.
18
+ def self.from_string(str)
19
+ case str
20
+ when /<.+>/
21
+ m, name, email = *str.match(/(.*) <(.+?)>/)
22
+ return self.new(name, email)
23
+ else
24
+ return self.new(str, nil)
25
+ end
26
+ end
27
+
28
+ # Outputs an actor string for Git commits.
29
+ #
30
+ # actor = Actor.new('bob', 'bob@email.com')
31
+ # actor.output(time) # => "bob <bob@email.com> UNIX_TIME +0700"
32
+ #
33
+ # time - The Time the commit was authored or committed.
34
+ #
35
+ # Returns a String.
36
+ def output(time)
37
+ offset = time.utc_offset / 60
38
+ "%s <%s> %d %+.2d%.2d" % [
39
+ @name,
40
+ @email || "null",
41
+ time.to_i,
42
+ offset / 60,
43
+ offset.abs % 60]
44
+ end
45
+
46
+ # Pretty object inspection
47
+ def inspect
48
+ %Q{#<Grit::Actor "#{@name} <#{@email}>">}
49
+ end
50
+ end # Actor
51
+
52
+ end # Grit
data/lib/grit/blame.rb ADDED
@@ -0,0 +1,70 @@
1
+ module Grit
2
+
3
+ class Blame
4
+
5
+ attr_reader :lines
6
+
7
+ def initialize(repo, file, commit, lines=nil)
8
+ @repo = repo
9
+ @file = file
10
+ @commit = commit
11
+ if lines.nil?
12
+ @lines = []
13
+ load_blame
14
+ else
15
+ @lines = lines
16
+ end
17
+ end
18
+
19
+ def load_blame
20
+ output = @repo.git.blame({'p' => true}, @commit, '--', @file)
21
+ process_raw_blame(output)
22
+ end
23
+
24
+ def process_raw_blame(output)
25
+ lines, final = [], []
26
+ info, commits = {}, {}
27
+
28
+ # process the output
29
+ output.split("\n").each do |line|
30
+ if line[0, 1] == "\t"
31
+ lines << line[1, line.size]
32
+ elsif m = /^(\w{40}) (\d+) (\d+)/.match(line)
33
+ commit_id, old_lineno, lineno = m[1], m[2].to_i, m[3].to_i
34
+ commits[commit_id] = nil if !commits.key?(commit_id)
35
+ info[lineno] = [commit_id, old_lineno]
36
+ end
37
+ end
38
+
39
+ # load all commits in single call
40
+ @repo.batch(*commits.keys).each do |commit|
41
+ commits[commit.id] = commit
42
+ end
43
+
44
+ # get it together
45
+ info.sort.each do |lineno, (commit_id, old_lineno)|
46
+ commit = commits[commit_id]
47
+ final << BlameLine.new(lineno, old_lineno, commit, lines[lineno - 1])
48
+ end
49
+
50
+ @lines = final
51
+ end
52
+
53
+ # Pretty object inspection
54
+ def inspect
55
+ %Q{#<Grit::Blame "#{@file} <#{@commit}>">}
56
+ end
57
+
58
+ class BlameLine
59
+ attr_accessor :lineno, :oldlineno, :commit, :line
60
+ def initialize(lineno, oldlineno, commit, line)
61
+ @lineno = lineno
62
+ @oldlineno = oldlineno
63
+ @commit = commit
64
+ @line = line
65
+ end
66
+ end
67
+
68
+ end # Blame
69
+
70
+ end # Grit
data/lib/grit/blob.rb ADDED
@@ -0,0 +1,126 @@
1
+ module Grit
2
+
3
+ class Blob
4
+ DEFAULT_MIME_TYPE = "text/plain"
5
+
6
+ attr_reader :id
7
+ attr_reader :mode
8
+ attr_reader :name
9
+
10
+ # Create an unbaked Blob containing just the specified attributes
11
+ # +repo+ is the Repo
12
+ # +atts+ is a Hash of instance variable data
13
+ #
14
+ # Returns Grit::Blob (unbaked)
15
+ def self.create(repo, atts)
16
+ self.allocate.create_initialize(repo, atts)
17
+ end
18
+
19
+ # Initializer for Blob.create
20
+ # +repo+ is the Repo
21
+ # +atts+ is a Hash of instance variable data
22
+ #
23
+ # Returns Grit::Blob (unbaked)
24
+ def create_initialize(repo, atts)
25
+ @repo = repo
26
+ atts.each do |k, v|
27
+ instance_variable_set("@#{k}".to_sym, v)
28
+ end
29
+ self
30
+ end
31
+
32
+ # The size of this blob in bytes
33
+ #
34
+ # Returns Integer
35
+ def size
36
+ @size ||= @repo.git.cat_file({:s => true}, id).chomp.to_i
37
+ end
38
+
39
+ # The binary contents of this blob.
40
+ #
41
+ # Returns String
42
+ def data
43
+ @data ||= @repo.git.cat_file({:p => true}, id)
44
+ end
45
+
46
+ # The mime type of this file (based on the filename)
47
+ #
48
+ # Returns String
49
+ def mime_type
50
+ guesses = MIME::Types.type_for(self.name) rescue []
51
+ guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
52
+ end
53
+
54
+ # The blame information for the given file at the given commit
55
+ #
56
+ # Returns Array: [Grit::Commit, Array: [<line>]]
57
+ def self.blame(repo, commit, file)
58
+ data = repo.git.blame({:p => true}, commit, '--', file)
59
+
60
+ commits = {}
61
+ blames = []
62
+ info = nil
63
+
64
+ data.split("\n").each do |line|
65
+ parts = line.split(/\s+/, 2)
66
+ case parts.first
67
+ when /^[0-9A-Fa-f]{40}$/
68
+ case line
69
+ when /^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/
70
+ _, id, origin_line, final_line, group_lines = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+) (\d+)$/)
71
+ info = {:id => id}
72
+ blames << [nil, []]
73
+ when /^([0-9A-Fa-f]{40}) (\d+) (\d+)$/
74
+ _, id, origin_line, final_line = *line.match(/^([0-9A-Fa-f]{40}) (\d+) (\d+)$/)
75
+ info = {:id => id}
76
+ end
77
+ when /^(author|committer)/
78
+ case parts.first
79
+ when /^(.+)-mail$/
80
+ info["#{$1}_email".intern] = parts.last
81
+ when /^(.+)-time$/
82
+ info["#{$1}_date".intern] = Time.at(parts.last.to_i)
83
+ when /^(author|committer)$/
84
+ info[$1.intern] = parts.last
85
+ end
86
+ when /^filename/
87
+ info[:filename] = parts.last
88
+ when /^summary/
89
+ info[:summary] = parts.last
90
+ when ''
91
+ c = commits[info[:id]]
92
+ unless c
93
+ c = Commit.create(repo, :id => info[:id],
94
+ :author => Actor.from_string(info[:author] + ' ' + info[:author_email]),
95
+ :authored_date => info[:author_date],
96
+ :committer => Actor.from_string(info[:committer] + ' ' + info[:committer_email]),
97
+ :committed_date => info[:committer_date],
98
+ :message => info[:summary])
99
+ commits[info[:id]] = c
100
+ end
101
+ _, text = *line.match(/^\t(.*)$/)
102
+ blames.last[0] = c
103
+ blames.last[1] << text
104
+ info = nil
105
+ end
106
+ end
107
+
108
+ blames
109
+ end
110
+
111
+ def basename
112
+ File.basename(name)
113
+ end
114
+
115
+ # Pretty object inspection
116
+ def inspect
117
+ %Q{#<Grit::Blob "#{@id}">}
118
+ end
119
+
120
+ # Compares blobs by name
121
+ def <=>(other)
122
+ name <=> other.name
123
+ end
124
+ end # Blob
125
+
126
+ end # Grit
@@ -0,0 +1,313 @@
1
+ module Grit
2
+
3
+ class Commit
4
+ extend Lazy
5
+
6
+ attr_reader :id
7
+ attr_reader :repo
8
+ lazy_reader :parents
9
+ lazy_reader :tree
10
+ lazy_reader :author
11
+ lazy_reader :authored_date
12
+ lazy_reader :committer
13
+ lazy_reader :committed_date
14
+ lazy_reader :message
15
+ lazy_reader :short_message
16
+
17
+ # Parses output from the `git-cat-file --batch'.
18
+ #
19
+ # repo - Grit::Repo instance.
20
+ # sha - String SHA of the Commit.
21
+ # size - Fixnum size of the object.
22
+ # object - Parsed String output from `git cat-file --batch`.
23
+ #
24
+ # Returns an Array of Grit::Commit objects.
25
+ def self.parse_batch(repo, sha, size, object)
26
+ info, message = object.split("\n\n", 2)
27
+
28
+ lines = info.split("\n")
29
+ tree = lines.shift.split(' ', 2).last
30
+ parents = []
31
+ parents << lines.shift[7..-1] while lines.first[0, 6] == 'parent'
32
+ author, authored_date = Grit::Commit.actor(lines.shift)
33
+ committer, committed_date = Grit::Commit.actor(lines.shift)
34
+
35
+ Grit::Commit.new(
36
+ repo, sha, parents, tree,
37
+ author, authored_date,
38
+ committer, committed_date,
39
+ message.to_s.split("\n"))
40
+ end
41
+
42
+ # Instantiate a new Commit
43
+ # +id+ is the id of the commit
44
+ # +parents+ is an array of commit ids (will be converted into Commit instances)
45
+ # +tree+ is the correspdonding tree id (will be converted into a Tree object)
46
+ # +author+ is the author string
47
+ # +authored_date+ is the authored Time
48
+ # +committer+ is the committer string
49
+ # +committed_date+ is the committed Time
50
+ # +message+ is an array of commit message lines
51
+ #
52
+ # Returns Grit::Commit (baked)
53
+ def initialize(repo, id, parents, tree, author, authored_date, committer, committed_date, message)
54
+ @repo = repo
55
+ @id = id
56
+ @parents = parents.map { |p| Commit.create(repo, :id => p) }
57
+ @tree = Tree.create(repo, :id => tree)
58
+ @author = author
59
+ @authored_date = authored_date
60
+ @committer = committer
61
+ @committed_date = committed_date
62
+ @message = message.join("\n")
63
+ @short_message = message.find { |x| !x.strip.empty? } || ''
64
+ end
65
+
66
+ def id_abbrev
67
+ @id_abbrev ||= @repo.git.rev_parse({}, self.id).chomp[0, 7]
68
+ end
69
+
70
+ # Create an unbaked Commit containing just the specified attributes
71
+ # +repo+ is the Repo
72
+ # +atts+ is a Hash of instance variable data
73
+ #
74
+ # Returns Grit::Commit (unbaked)
75
+ def self.create(repo, atts)
76
+ self.allocate.create_initialize(repo, atts)
77
+ end
78
+
79
+ # Initializer for Commit.create
80
+ # +repo+ is the Repo
81
+ # +atts+ is a Hash of instance variable data
82
+ #
83
+ # Returns Grit::Commit (unbaked)
84
+ def create_initialize(repo, atts)
85
+ @repo = repo
86
+ atts.each do |k, v|
87
+ instance_variable_set("@#{k}", v)
88
+ end
89
+ self
90
+ end
91
+
92
+ def lazy_source
93
+ self.class.find_all(@repo, @id, {:max_count => 1}).first
94
+ end
95
+
96
+ # Count the number of commits reachable from this ref
97
+ # +repo+ is the Repo
98
+ # +ref+ is the ref from which to begin (SHA1 or name)
99
+ #
100
+ # Returns Integer
101
+ def self.count(repo, ref)
102
+ repo.git.rev_list({}, ref).size / 41
103
+ end
104
+
105
+ # Find all commits matching the given criteria.
106
+ # +repo+ is the Repo
107
+ # +ref+ is the ref from which to begin (SHA1 or name) or nil for --all
108
+ # +options+ is a Hash of optional arguments to git
109
+ # :max_count is the maximum number of commits to fetch
110
+ # :skip is the number of commits to skip
111
+ #
112
+ # Returns Grit::Commit[] (baked)
113
+ def self.find_all(repo, ref, options = {})
114
+ allowed_options = [:max_count, :skip, :since]
115
+
116
+ default_options = {:pretty => "raw"}
117
+ actual_options = default_options.merge(options)
118
+
119
+ if ref
120
+ output = repo.git.rev_list(actual_options, ref)
121
+ else
122
+ output = repo.git.rev_list(actual_options.merge(:all => true))
123
+ end
124
+
125
+ self.list_from_string(repo, output)
126
+ rescue Grit::GitRuby::Repository::NoSuchShaFound
127
+ []
128
+ end
129
+
130
+ # Parse out commit information into an array of baked Commit objects
131
+ # +repo+ is the Repo
132
+ # +text+ is the text output from the git command (raw format)
133
+ #
134
+ # Returns Grit::Commit[] (baked)
135
+ #
136
+ # really should re-write this to be more accepting of non-standard commit messages
137
+ # - it broke when 'encoding' was introduced - not sure what else might show up
138
+ #
139
+ def self.list_from_string(repo, text)
140
+ lines = text.split("\n")
141
+
142
+ commits = []
143
+
144
+ while !lines.empty?
145
+ id = lines.shift.split.last
146
+ tree = lines.shift.split.last
147
+
148
+ parents = []
149
+ parents << lines.shift.split.last while lines.first =~ /^parent/
150
+
151
+ author_line = lines.shift
152
+ author_line << lines.shift if lines[0] !~ /^committer /
153
+ author, authored_date = self.actor(author_line)
154
+
155
+ committer_line = lines.shift
156
+ committer_line << lines.shift if lines[0] && lines[0] != '' && lines[0] !~ /^encoding/
157
+ committer, committed_date = self.actor(committer_line)
158
+
159
+ # not doing anything with this yet, but it's sometimes there
160
+ encoding = lines.shift.split.last if lines.first =~ /^encoding/
161
+
162
+ lines.shift
163
+
164
+ message_lines = []
165
+ message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
166
+
167
+ lines.shift while lines.first && lines.first.empty?
168
+
169
+ commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
170
+ end
171
+
172
+ commits
173
+ end
174
+
175
+ # Show diffs between two trees.
176
+ #
177
+ # repo - The current Grit::Repo instance.
178
+ # a - A String named commit.
179
+ # b - An optional String named commit. Passing an array assumes you
180
+ # wish to omit the second named commit and limit the diff to the
181
+ # given paths.
182
+ # paths - An optional Array of paths to limit the diff.
183
+ # options - An optional Hash of options. Merged into {:full_index => true}.
184
+ #
185
+ # Returns Grit::Diff[] (baked)
186
+ def self.diff(repo, a, b = nil, paths = [], options = {})
187
+ if b.is_a?(Array)
188
+ paths = b
189
+ b = nil
190
+ end
191
+ paths.unshift("--") unless paths.empty?
192
+ paths.unshift(b) unless b.nil?
193
+ paths.unshift(a)
194
+ options = {:full_index => true}.update(options)
195
+ text = repo.git.diff(options, *paths)
196
+ Diff.list_from_string(repo, text)
197
+ end
198
+
199
+ def show
200
+ if parents.size > 1
201
+ diff = @repo.git.native(:diff, {:full_index => true}, "#{parents[0].id}...#{parents[1].id}")
202
+ else
203
+ diff = @repo.git.show({:full_index => true, :pretty => 'raw'}, @id)
204
+ end
205
+
206
+ if diff =~ /diff --git a/
207
+ diff = diff.sub(/.+?(diff --git a)/m, '\1')
208
+ else
209
+ diff = ''
210
+ end
211
+ Diff.list_from_string(@repo, diff)
212
+ end
213
+
214
+ # Shows diffs between the commit's parent and the commit.
215
+ #
216
+ # options - An optional Hash of options, passed to Grit::Commit.diff.
217
+ #
218
+ # Returns Grit::Diff[] (baked)
219
+ def diffs(options = {})
220
+ if parents.empty?
221
+ show
222
+ else
223
+ self.class.diff(@repo, parents.first.id, @id, [], options)
224
+ end
225
+ end
226
+
227
+ def stats
228
+ stats = @repo.commit_stats(self.sha, 1)[0][-1]
229
+ end
230
+
231
+ # Convert this Commit to a String which is just the SHA1 id
232
+ def to_s
233
+ @id
234
+ end
235
+
236
+ def sha
237
+ @id
238
+ end
239
+
240
+ def date
241
+ @committed_date
242
+ end
243
+
244
+ def to_patch
245
+ @repo.git.format_patch({'1' => true, :stdout => true}, to_s)
246
+ end
247
+
248
+ def notes
249
+ ret = {}
250
+ notes = Note.find_all(@repo)
251
+ notes.each do |note|
252
+ if n = note.commit.tree/(self.id)
253
+ ret[note.name] = n.data
254
+ end
255
+ end
256
+ ret
257
+ end
258
+
259
+ # Calculates the commit's Patch ID. The Patch ID is essentially the SHA1
260
+ # of the diff that the commit is introducing.
261
+ #
262
+ # Returns the 40 character hex String if a patch-id could be calculated
263
+ # or nil otherwise.
264
+ def patch_id
265
+ show = @repo.git.show({}, @id)
266
+ patch_line = @repo.git.native(:patch_id, :input => show)
267
+ if patch_line =~ /^([0-9a-f]{40}) [0-9a-f]{40}\n$/
268
+ $1
269
+ else
270
+ nil
271
+ end
272
+ end
273
+
274
+ # Pretty object inspection
275
+ def inspect
276
+ %Q{#<Grit::Commit "#{@id}">}
277
+ end
278
+
279
+ # private
280
+
281
+ # Parse out the actor (author or committer) info
282
+ #
283
+ # Returns [String (actor name and email), Time (acted at time)]
284
+ def self.actor(line)
285
+ m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
286
+ [Actor.from_string(actor), Time.at(epoch.to_i)]
287
+ end
288
+
289
+ def author_string
290
+ "%s <%s> %s %+05d" % [author.name, author.email, authored_date.to_i, 800]
291
+ end
292
+
293
+ def to_hash
294
+ {
295
+ 'id' => id,
296
+ 'parents' => parents.map { |p| { 'id' => p.id } },
297
+ 'tree' => tree.id,
298
+ 'message' => message,
299
+ 'author' => {
300
+ 'name' => author.name,
301
+ 'email' => author.email
302
+ },
303
+ 'committer' => {
304
+ 'name' => committer.name,
305
+ 'email' => committer.email
306
+ },
307
+ 'authored_date' => authored_date.xmlschema,
308
+ 'committed_date' => committed_date.xmlschema,
309
+ }
310
+ end
311
+ end # Commit
312
+
313
+ end # Grit