@@ -6,6 +6,7 @@ class Commit
6
6
7
7
attr_accessor :raw_commit , :head
8
8
9
+ MAX_COMMIT_MESSAGE_DISPLAY_SIZE = 10 . megabytes
9
10
MIN_SHA_LENGTH = 7
10
11
SERIALIZE_KEYS = [
11
12
:id , :message , :parent_ids ,
@@ -63,9 +64,7 @@ def find(repo, commit_id = "HEAD")
63
64
if is_enabled
64
65
repo . gitaly_commit_client . find_commit ( commit_id )
65
66
else
66
- obj = repo . rev_parse_target ( commit_id )
67
-
68
- obj . is_a? ( Rugged ::Commit ) ? obj : nil
67
+ rugged_find ( repo , commit_id )
69
68
end
70
69
end
71
70
@@ -76,6 +75,12 @@ def find(repo, commit_id = "HEAD")
76
75
nil
77
76
end
78
77
78
+ def rugged_find ( repo , commit_id )
79
+ obj = repo . rev_parse_target ( commit_id )
80
+
81
+ obj . is_a? ( Rugged ::Commit ) ? obj : nil
82
+ end
83
+
79
84
# Get last commit for HEAD
80
85
#
81
86
# Ex.
@@ -297,11 +302,40 @@ def rugged_extract_signature(repository, commit_id)
297
302
nil
298
303
end
299
304
end
305
+
306
+ def get_message ( repository , commit_id )
307
+ BatchLoader . for ( { repository : repository , commit_id : commit_id } ) . batch do |items , loader |
308
+ items_by_repo = items . group_by { |i | i [ :repository ] }
309
+
310
+ items_by_repo . each do |repo , items |
311
+ commit_ids = items . map { |i | i [ :commit_id ] }
312
+
313
+ messages = get_messages ( repository , commit_ids )
314
+
315
+ messages . each do |commit_sha , message |
316
+ loader . call ( { repository : repository , commit_id : commit_sha } , message )
317
+ end
318
+ end
319
+ end
320
+ end
321
+
322
+ def get_messages ( repository , commit_ids )
323
+ repository . gitaly_migrate ( :commit_messages ) do |is_enabled |
324
+ if is_enabled
325
+ repository . gitaly_commit_client . get_commit_messages ( commit_ids )
326
+ else
327
+ commit_ids . map { |id | [ id , rugged_find ( repository , id ) . message ] } . to_h
328
+ end
329
+ end
330
+ end
300
331
end
301
332
302
333
def initialize ( repository , raw_commit , head = nil )
303
334
raise "Nil as raw commit passed" unless raw_commit
304
335
336
+ @repository = repository
337
+ @head = head
338
+
305
339
case raw_commit
306
340
when Hash
307
341
init_from_hash ( raw_commit )
@@ -312,9 +346,6 @@ def initialize(repository, raw_commit, head = nil)
312
346
else
313
347
raise "Invalid raw commit type: #{ raw_commit . class } "
314
348
end
315
-
316
- @repository = repository
317
- @head = head
318
349
end
319
350
320
351
def sha
@@ -518,7 +549,7 @@ def init_from_gitaly(commit)
518
549
# TODO: Once gitaly "takes over" Rugged consider separating the
519
550
# subject from the message to make it clearer when there's one
520
551
# available but not the other.
521
- @message = ( commit . body . presence || commit . subject ) . dup
552
+ @message = message_from_gitaly_body
522
553
@authored_date = Time . at ( commit . author . date . seconds ) . utc
523
554
@author_name = commit . author . name . dup
524
555
@author_email = commit . author . email . dup
@@ -570,6 +601,25 @@ def gitaly_commit_author_from_rugged(author_or_committer)
570
601
def refs ( repo )
571
602
repo . refs_hash [ id ]
572
603
end
604
+
605
+ def message_from_gitaly_body
606
+ return @raw_commit . subject . dup if @raw_commit . body_size . zero?
607
+ return @raw_commit . body . dup if full_body_fetched_from_gitaly?
608
+
609
+ if @raw_commit . body_size > MAX_COMMIT_MESSAGE_DISPLAY_SIZE
610
+ "#{ @raw_commit . subject } \n \n --commit message is too big" . strip
611
+ else
612
+ fetch_body_from_gitaly
613
+ end
614
+ end
615
+
616
+ def full_body_fetched_from_gitaly?
617
+ @raw_commit . body . bytesize == @raw_commit . body_size
618
+ end
619
+
620
+ def fetch_body_from_gitaly
621
+ self . class . get_message ( @repository , id )
622
+ end
573
623
end
574
624
end
575
625
end
0 commit comments