gitlab-grit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gitlab-grit might be problematic. Click here for more details.

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,323 @@
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
+ # GITLAB patch
146
+ # Skip all garbage unless we get real commit
147
+ while !lines.empty? && lines.first !~ /^commit [a-zA-Z0-9]*$/
148
+ lines.shift
149
+ end
150
+
151
+ id = lines.shift.split.last
152
+ tree = lines.shift.split.last
153
+
154
+ parents = []
155
+ parents << lines.shift.split.last while lines.first =~ /^parent/
156
+
157
+ author_line = lines.shift
158
+ author_line << lines.shift if lines[0] !~ /^committer /
159
+ author, authored_date = self.actor(author_line)
160
+
161
+ committer_line = lines.shift
162
+ committer_line << lines.shift if lines[0] && lines[0] != '' && lines[0] !~ /^encoding/
163
+ committer, committed_date = self.actor(committer_line)
164
+
165
+ # not doing anything with this yet, but it's sometimes there
166
+ encoding = lines.shift.split.last if lines.first =~ /^encoding/
167
+
168
+ # GITLAB patch
169
+ # Skip Signature and other raw data
170
+ lines.shift while lines.first =~ /^ /
171
+
172
+ lines.shift
173
+
174
+ message_lines = []
175
+ message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
176
+
177
+ lines.shift while lines.first && lines.first.empty?
178
+
179
+ commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
180
+ end
181
+
182
+ commits
183
+ end
184
+
185
+ # Show diffs between two trees.
186
+ #
187
+ # repo - The current Grit::Repo instance.
188
+ # a - A String named commit.
189
+ # b - An optional String named commit. Passing an array assumes you
190
+ # wish to omit the second named commit and limit the diff to the
191
+ # given paths.
192
+ # paths - An optional Array of paths to limit the diff.
193
+ # options - An optional Hash of options. Merged into {:full_index => true}.
194
+ #
195
+ # Returns Grit::Diff[] (baked)
196
+ def self.diff(repo, a, b = nil, paths = [], options = {})
197
+ if b.is_a?(Array)
198
+ paths = b
199
+ b = nil
200
+ end
201
+ paths.unshift("--") unless paths.empty?
202
+ paths.unshift(b) unless b.nil?
203
+ paths.unshift(a)
204
+ options = {:full_index => true}.update(options)
205
+ text = repo.git.diff(options, *paths)
206
+ Diff.list_from_string(repo, text)
207
+ end
208
+
209
+ def show
210
+ if parents.size > 1
211
+ diff = @repo.git.native(:diff, {:full_index => true}, "#{parents[0].id}...#{parents[1].id}")
212
+ else
213
+ diff = @repo.git.show({:full_index => true, :pretty => 'raw'}, @id)
214
+ end
215
+
216
+ if diff =~ /diff --git a/
217
+ diff = diff.sub(/.+?(diff --git a)/m, '\1')
218
+ else
219
+ diff = ''
220
+ end
221
+ Diff.list_from_string(@repo, diff)
222
+ end
223
+
224
+ # Shows diffs between the commit's parent and the commit.
225
+ #
226
+ # options - An optional Hash of options, passed to Grit::Commit.diff.
227
+ #
228
+ # Returns Grit::Diff[] (baked)
229
+ def diffs(options = {})
230
+ if parents.empty?
231
+ show
232
+ else
233
+ self.class.diff(@repo, parents.first.id, @id, [], options)
234
+ end
235
+ end
236
+
237
+ def stats
238
+ stats = @repo.commit_stats(self.sha, 1)[0][-1]
239
+ end
240
+
241
+ # Convert this Commit to a String which is just the SHA1 id
242
+ def to_s
243
+ @id
244
+ end
245
+
246
+ def sha
247
+ @id
248
+ end
249
+
250
+ def date
251
+ @committed_date
252
+ end
253
+
254
+ def to_patch
255
+ @repo.git.format_patch({'1' => true, :stdout => true}, to_s)
256
+ end
257
+
258
+ def notes
259
+ ret = {}
260
+ notes = Note.find_all(@repo)
261
+ notes.each do |note|
262
+ if n = note.commit.tree/(self.id)
263
+ ret[note.name] = n.data
264
+ end
265
+ end
266
+ ret
267
+ end
268
+
269
+ # Calculates the commit's Patch ID. The Patch ID is essentially the SHA1
270
+ # of the diff that the commit is introducing.
271
+ #
272
+ # Returns the 40 character hex String if a patch-id could be calculated
273
+ # or nil otherwise.
274
+ def patch_id
275
+ show = @repo.git.show({}, @id)
276
+ patch_line = @repo.git.native(:patch_id, :input => show)
277
+ if patch_line =~ /^([0-9a-f]{40}) [0-9a-f]{40}\n$/
278
+ $1
279
+ else
280
+ nil
281
+ end
282
+ end
283
+
284
+ # Pretty object inspection
285
+ def inspect
286
+ %Q{#<Grit::Commit "#{@id}">}
287
+ end
288
+
289
+ # private
290
+
291
+ # Parse out the actor (author or committer) info
292
+ #
293
+ # Returns [String (actor name and email), Time (acted at time)]
294
+ def self.actor(line)
295
+ m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
296
+ [Actor.from_string(actor), Time.at(epoch.to_i)]
297
+ end
298
+
299
+ def author_string
300
+ "%s <%s> %s %+05d" % [author.name, author.email, authored_date.to_i, 800]
301
+ end
302
+
303
+ def to_hash
304
+ {
305
+ 'id' => id,
306
+ 'parents' => parents.map { |p| { 'id' => p.id } },
307
+ 'tree' => tree.id,
308
+ 'message' => message,
309
+ 'author' => {
310
+ 'name' => author.name,
311
+ 'email' => author.email
312
+ },
313
+ 'committer' => {
314
+ 'name' => committer.name,
315
+ 'email' => committer.email
316
+ },
317
+ 'authored_date' => authored_date.xmlschema,
318
+ 'committed_date' => committed_date.xmlschema,
319
+ }
320
+ end
321
+ end # Commit
322
+
323
+ end # Grit