Commit fdba7449 authored by Alexander Randa's avatar Alexander Randa

Fix long urls in the title of commit

parent 1b929586
...@@ -114,16 +114,16 @@ class Commit ...@@ -114,16 +114,16 @@ class Commit
# #
# Usually, the commit title is the first line of the commit message. # Usually, the commit title is the first line of the commit message.
# In case this first line is longer than 100 characters, it is cut off # In case this first line is longer than 100 characters, it is cut off
# after 80 characters and ellipses (`&hellp;`) are appended. # after 80 characters + `...`
def title def title
full_title.length > 100 ? full_title[0..79] << "…" : full_title return full_title if full_title.length < 100
full_title.truncate(81, separator: ' ', omission: '…')
end end
# Returns the full commits title # Returns the full commits title
def full_title def full_title
return @full_title if @full_title @full_title ||=
@full_title =
if safe_message.blank? if safe_message.blank?
no_commit_message no_commit_message
else else
...@@ -131,18 +131,13 @@ class Commit ...@@ -131,18 +131,13 @@ class Commit
end end
end end
# Returns the commits description # Returns full commit message if title is truncated (greater than 99 characters)
# # otherwise returns commit message without first line
# cut off, ellipses (`&hellp;`) are prepended to the commit message.
def description def description
title_end = safe_message.index("\n") return safe_message if full_title.length >= 100
@description ||=
if (!title_end && safe_message.length > 100) || (title_end && title_end > 100)
"…" << safe_message[80..-1]
else
safe_message.split("\n", 2)[1].try(:chomp) safe_message.split("\n", 2)[1].try(:chomp)
end end
end
def description? def description?
description.present? description.present?
......
---
title: Fix long urls in the title of commit
merge_request: 10938
author: Alexander Randa
...@@ -67,11 +67,11 @@ describe Commit, models: true do ...@@ -67,11 +67,11 @@ describe Commit, models: true do
expect(commit.title).to eq("--no commit message") expect(commit.title).to eq("--no commit message")
end end
it "truncates a message without a newline at 80 characters" do it 'truncates a message without a newline at natural break to 80 characters' do
message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit. Vivamus egestas lacinia lacus, sed rutrum mauris.' message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit. Vivamus egestas lacinia lacus, sed rutrum mauris.'
allow(commit).to receive(:safe_message).and_return(message) allow(commit).to receive(:safe_message).and_return(message)
expect(commit.title).to eq("#{message[0..79]}…") expect(commit.title).to eq('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis…')
end end
it "truncates a message with a newline before 80 characters at the newline" do it "truncates a message with a newline before 80 characters at the newline" do
...@@ -113,6 +113,28 @@ eos ...@@ -113,6 +113,28 @@ eos
end end
end end
describe 'description' do
it 'returns description of commit message if title less than 100 characters' do
message = <<eos
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit.
Vivamus egestas lacinia lacus, sed rutrum mauris.
eos
allow(commit).to receive(:safe_message).and_return(message)
expect(commit.description).to eq('Vivamus egestas lacinia lacus, sed rutrum mauris.')
end
it 'returns full commit message if commit title more than 100 characters' do
message = <<eos
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit. Vivamus egestas lacinia lacus, sed rutrum mauris.
Vivamus egestas lacinia lacus, sed rutrum mauris.
eos
allow(commit).to receive(:safe_message).and_return(message)
expect(commit.description).to eq(message)
end
end
describe "delegation" do describe "delegation" do
subject { commit } subject { commit }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment