Commit 6bac823a authored by Robert Speicher's avatar Robert Speicher

Update CommitRangeReferenceFilter to use CommitRange class

Also matches CommitReferenceFilter's new behavior of always using short
SHAs in the link text.
parent a8e66564
...@@ -32,11 +32,8 @@ module Gitlab ...@@ -32,11 +32,8 @@ module Gitlab
# Pattern used to extract commit range references from text # Pattern used to extract commit range references from text
# #
# The beginning and ending SHA1 sums can be between 6 and 40 hex
# characters, and the range selection can be double- or triple-dot.
#
# This pattern supports cross-project references. # This pattern supports cross-project references.
COMMIT_RANGE_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit_range>\h{6,40}\.{2,3}\h{6,40})/ COMMIT_RANGE_PATTERN = /(#{PROJECT_PATTERN}@)?(?<commit_range>#{CommitRange::PATTERN})/
def call def call
replace_text_nodes_matching(COMMIT_RANGE_PATTERN) do |content| replace_text_nodes_matching(COMMIT_RANGE_PATTERN) do |content|
...@@ -53,54 +50,34 @@ module Gitlab ...@@ -53,54 +50,34 @@ module Gitlab
# links have `gfm` and `gfm-commit_range` class names attached for # links have `gfm` and `gfm-commit_range` class names attached for
# styling. # styling.
def commit_range_link_filter(text) def commit_range_link_filter(text)
self.class.references_in(text) do |match, commit_range, project_ref| self.class.references_in(text) do |match, id, project_ref|
project = self.project_from_ref(project_ref) project = self.project_from_ref(project_ref)
from_id, to_id = split_commit_range(commit_range) range = CommitRange.new(id, project)
if valid_range?(project, from_id, to_id) if range.valid_commits?
push_result(:commit_range, [commit(from_id), commit(to_id)]) push_result(:commit_range, range.to_a)
url = url_for_commit_range(project, from_id, to_id) url = url_for_commit_range(project, range)
title = "Commits #{from_id} through #{to_id}" title = range.reference_title
klass = reference_class(:commit_range) klass = reference_class(:commit_range)
project_ref += '@' if project_ref project_ref += '@' if project_ref
%(<a href="#{url}" %(<a href="#{url}"
title="#{title}" title="#{title}"
class="#{klass}">#{project_ref}#{commit_range}</a>) class="#{klass}">#{project_ref}#{range}</a>)
else else
match match
end end
end end
end end
def split_commit_range(range) def url_for_commit_range(project, range)
from_id, to_id = range.split(/\.{2,3}/, 2)
from_id << "^" if range !~ /\.{3}/
[from_id, to_id]
end
def commit(id)
unless @commit_map[id]
@commit_map[id] = project.commit(id)
end
@commit_map[id]
end
def valid_range?(project, from_id, to_id)
project && project.valid_repo? && commit(from_id) && commit(to_id)
end
def url_for_commit_range(project, from_id, to_id)
h = Rails.application.routes.url_helpers h = Rails.application.routes.url_helpers
h.namespace_project_compare_url(project.namespace, project, h.namespace_project_compare_url(project.namespace, project,
from: from_id, to: to_id, range.to_param.merge(only_path: context[:only_path]))
only_path: context[:only_path])
end end
end end
end end
......
...@@ -42,13 +42,17 @@ module Gitlab::Markdown ...@@ -42,13 +42,17 @@ module Gitlab::Markdown
reference = "#{commit1.short_id}...#{commit2.id}" reference = "#{commit1.short_id}...#{commit2.id}"
reference2 = "#{commit1.id}...#{commit2.short_id}" reference2 = "#{commit1.id}...#{commit2.short_id}"
expect(filter("See #{reference}").css('a').first.text).to eq reference exp = commit1.short_id + '...' + commit2.short_id
expect(filter("See #{reference2}").css('a').first.text).to eq reference2
expect(filter("See #{reference}").css('a').first.text).to eq exp
expect(filter("See #{reference2}").css('a').first.text).to eq exp
end end
it 'links with adjacent text' do it 'links with adjacent text' do
doc = filter("See (#{reference}.)") doc = filter("See (#{reference}.)")
expect(doc.to_html).to match(/\(<a.+>#{Regexp.escape(reference)}<\/a>\.\)/)
exp = Regexp.escape("#{commit1.short_id}...#{commit2.short_id}")
expect(doc.to_html).to match(/\(<a.+>#{exp}<\/a>\.\)/)
end end
it 'ignores invalid commit IDs' do it 'ignores invalid commit IDs' do
...@@ -107,7 +111,9 @@ module Gitlab::Markdown ...@@ -107,7 +111,9 @@ module Gitlab::Markdown
it 'links with adjacent text' do it 'links with adjacent text' do
doc = filter("Fixed (#{reference}.)") doc = filter("Fixed (#{reference}.)")
expect(doc.to_html).to match(/\(<a.+>#{Regexp.escape(reference)}<\/a>\.\)/)
exp = Regexp.escape("#{project2.path_with_namespace}@#{commit1.short_id}...#{commit2.short_id}")
expect(doc.to_html).to match(/\(<a.+>#{exp}<\/a>\.\)/)
end end
it 'ignores invalid commit IDs on the referenced project' do it 'ignores invalid commit IDs on the referenced project' do
......
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