@@ -6,6 +6,7 @@ class Commit
66
77 attr_accessor :raw_commit , :head
88
9+ MAX_COMMIT_MESSAGE_DISPLAY_SIZE = 10 . megabytes
910 MIN_SHA_LENGTH = 7
1011 SERIALIZE_KEYS = [
1112 :id , :message , :parent_ids ,
@@ -63,9 +64,7 @@ def find(repo, commit_id = "HEAD")
6364 if is_enabled
6465 repo . gitaly_commit_client . find_commit ( commit_id )
6566 else
66- obj = repo . rev_parse_target ( commit_id )
67-
68- obj . is_a? ( Rugged ::Commit ) ? obj : nil
67+ rugged_find ( repo , commit_id )
6968 end
7069 end
7170
@@ -76,6 +75,12 @@ def find(repo, commit_id = "HEAD")
7675 nil
7776 end
7877
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+
7984 # Get last commit for HEAD
8085 #
8186 # Ex.
@@ -297,11 +302,40 @@ def rugged_extract_signature(repository, commit_id)
297302 nil
298303 end
299304 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
300331 end
301332
302333 def initialize ( repository , raw_commit , head = nil )
303334 raise "Nil as raw commit passed" unless raw_commit
304335
336+ @repository = repository
337+ @head = head
338+
305339 case raw_commit
306340 when Hash
307341 init_from_hash ( raw_commit )
@@ -312,9 +346,6 @@ def initialize(repository, raw_commit, head = nil)
312346 else
313347 raise "Invalid raw commit type: #{ raw_commit . class } "
314348 end
315-
316- @repository = repository
317- @head = head
318349 end
319350
320351 def sha
@@ -518,7 +549,7 @@ def init_from_gitaly(commit)
518549 # TODO: Once gitaly "takes over" Rugged consider separating the
519550 # subject from the message to make it clearer when there's one
520551 # available but not the other.
521- @message = ( commit . body . presence || commit . subject ) . dup
552+ @message = message_from_gitaly_body
522553 @authored_date = Time . at ( commit . author . date . seconds ) . utc
523554 @author_name = commit . author . name . dup
524555 @author_email = commit . author . email . dup
@@ -570,6 +601,25 @@ def gitaly_commit_author_from_rugged(author_or_committer)
570601 def refs ( repo )
571602 repo . refs_hash [ id ]
572603 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
573623 end
574624 end
575625end
0 commit comments