Commit b956605f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'ambiguous-sha' into 'master'

Fix ambiguous sha problem with mentioned commit

Before: write in database only 6 chars of commit sha. This cause to `Ambiguous SHA1 prefix` exception.

- [x] write full commit sha in db.
- [x] Standardise usage of sha truncation: 8 characters everywhere.
- [x] prevent exception when ambiguous sha requested in markdown

Fixes #1644

See merge request !1171
parents 66dd61ef b02c21df
...@@ -120,4 +120,8 @@ module CommitsHelper ...@@ -120,4 +120,8 @@ module CommitsHelper
class: 'commit-short-id') class: 'commit-short-id')
end end
end end
def truncate_sha(sha)
Commit.truncate_sha(sha)
end
end end
...@@ -19,13 +19,24 @@ class Commit ...@@ -19,13 +19,24 @@ class Commit
class << self class << self
def decorate(commits) def decorate(commits)
commits.map { |c| self.new(c) } commits.map do |commit|
if commit.kind_of?(Commit)
commit
else
self.new(commit)
end
end
end end
# Calculate number of lines to render for diffs # Calculate number of lines to render for diffs
def diff_line_count(diffs) def diff_line_count(diffs)
diffs.reduce(0) { |sum, d| sum + d.diff.lines.count } diffs.reduce(0) { |sum, d| sum + d.diff.lines.count }
end end
# Truncate sha to 8 characters
def truncate_sha(sha)
sha[0..7]
end
end end
attr_accessor :raw attr_accessor :raw
...@@ -111,7 +122,7 @@ class Commit ...@@ -111,7 +122,7 @@ class Commit
# Mentionable override. # Mentionable override.
def gfm_reference def gfm_reference
"commit #{sha[0..5]}" "commit #{id}"
end end
def method_missing(m, *args, &block) def method_missing(m, *args, &block)
...@@ -124,6 +135,11 @@ class Commit ...@@ -124,6 +135,11 @@ class Commit
super super
end end
# Truncate sha to 8 characters
def short_id
@raw.short_id(7)
end
def parents def parents
@parents ||= Commit.decorate(super) @parents ||= Commit.decorate(super)
end end
......
...@@ -266,7 +266,7 @@ class Event < ActiveRecord::Base ...@@ -266,7 +266,7 @@ class Event < ActiveRecord::Base
end end
def note_short_commit_id def note_short_commit_id
note_commit_id[0..8] Commit.truncate_sha(note_commit_id)
end end
def note_commit? def note_commit?
......
...@@ -55,7 +55,7 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -55,7 +55,7 @@ class MergeRequestDiff < ActiveRecord::Base
end end
def last_commit_short_sha def last_commit_short_sha
@last_commit_short_sha ||= last_commit.sha[0..10] @last_commit_short_sha ||= last_commit.short_id
end end
private private
......
...@@ -30,6 +30,8 @@ class Repository ...@@ -30,6 +30,8 @@ class Repository
commit = Gitlab::Git::Commit.find(raw_repository, id) commit = Gitlab::Git::Commit.find(raw_repository, id)
commit = Commit.new(commit) if commit commit = Commit.new(commit) if commit
commit commit
rescue Rugged::OdbError => ex
nil
end end
def commits(ref, path = nil, limit = nil, offset = nil, skip_merges = false) def commits(ref, path = nil, limit = nil, offset = nil, skip_merges = false)
......
%li.commit %li.commit
.commit-row-title .commit-row-title
= link_to commit[:id][0..8], project_commit_path(project, commit[:id]), class: "commit_short_id", alt: '' = link_to truncate_sha(commit[:id]), project_commit_path(project, commit[:id]), class: "commit_short_id", alt: ''
&nbsp; &nbsp;
= gfm event_commit_title(commit[:message]), project = gfm event_commit_title(commit[:message]), project
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
- event.commits.first(15).each do |commit| - event.commits.first(15).each do |commit|
%p %p
%strong= commit[:author][:name] %strong= commit[:author][:name]
= link_to "(##{commit[:id][0...8]})", project_commit_path(event.project, id: commit[:id]) = link_to "(##{truncate_sha(commit[:id])})", project_commit_path(event.project, id: commit[:id])
%i %i
at at
= commit[:timestamp].to_time.to_s(:short) = commit[:timestamp].to_time.to_s(:short)
......
...@@ -22,4 +22,4 @@ ...@@ -22,4 +22,4 @@
- if event.commits_count > 2 - if event.commits_count > 2
%span ... and #{event.commits_count - 2} more commits. %span ... and #{event.commits_count - 2} more commits.
= link_to project_compare_path(event.project, from: event.commit_from, to: event.commit_to) do = link_to project_compare_path(event.project, from: event.commit_from, to: event.commit_to) do
%strong Compare &rarr; #{event.commit_from[0..7]}...#{event.commit_to[0..7]} %strong Compare &rarr; #{truncate_sha(event.commit_from)}...#{truncate_sha(event.commit_to)}
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
%tr %tr
%td.blame-commit %td.blame-commit
%span.commit %span.commit
= link_to commit.short_id(8), project_commit_path(@project, commit), class: "commit_short_id" = link_to commit.short_id, project_commit_path(@project, commit), class: "commit_short_id"
&nbsp; &nbsp;
= commit_author_link(commit, avatar: true, size: 16) = commit_author_link(commit, avatar: true, size: 16)
&nbsp; &nbsp;
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
.commit-info-row .commit-info-row
%span.cgray= pluralize(@commit.parents.count, "parent") %span.cgray= pluralize(@commit.parents.count, "parent")
- @commit.parents.each do |parent| - @commit.parents.each do |parent|
= link_to parent.id[0...10], project_commit_path(@project, parent) = link_to parent.short_id, project_commit_path(@project, parent)
- if @branches.any? - if @branches.any?
.commit-info-row .commit-info-row
......
%li.commit.js-toggle-container %li.commit.js-toggle-container
.commit-row-title .commit-row-title
= link_to commit.short_id(8), project_commit_path(project, commit), class: "commit_short_id" = link_to commit.short_id, project_commit_path(project, commit), class: "commit_short_id"
&nbsp; &nbsp;
%span.str-truncated %span.str-truncated
= link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message" = link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message"
......
%li.commit.inline-commit %li.commit.inline-commit
.commit-row-title .commit-row-title
= link_to commit.short_id(8), project_commit_path(project, commit), class: "commit_short_id" = link_to commit.short_id, project_commit_path(project, commit), class: "commit_short_id"
&nbsp; &nbsp;
%span.str-truncated %span.str-truncated
= link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message" = link_to_gfm commit.title, project_commit_path(project, commit.id), class: "commit-row-message"
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
@ @
%span.monospace %span.monospace
- if commit.nil? - if commit.nil?
#{submodule_item.id[0..10]} #{truncate_sha(submodule_item.id)}
- else - else
= link_to "#{submodule_item.id[0..10]}", commit = link_to "#{truncate_sha(submodule_item.id)}", commit
%td %td
%td.hidden-xs %td.hidden-xs
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
%tr %tr
%td %td
= link_to project_wiki_path(@project, @page, version_id: commit.id) do = link_to project_wiki_path(@project, @page, version_id: commit.id) do
= commit.id[0..10] = truncate_sha(commit.id)
%td %td
= commit.author.name = commit.author.name
%td %td
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
= project.name_with_namespace = project.name_with_namespace
&middot; &middot;
= link_to project_commit_path(project, note.commit_id, anchor: dom_id(note)) do = link_to project_commit_path(project, note.commit_id, anchor: dom_id(note)) do
Commit #{note.commit_id[0..8]} Commit #{truncate_sha(note.commit_id)}
- else - else
= link_to project do = link_to project do
= project.name_with_namespace = project.name_with_namespace
......
...@@ -8,7 +8,7 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps ...@@ -8,7 +8,7 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps
commit = @project.repository.commit commit = @project.repository.commit
page.should have_content(@project.name) page.should have_content(@project.name)
page.should have_content(commit.message[0..20]) page.should have_content(commit.message[0..20])
page.should have_content(commit.id.to_s[0..5]) page.should have_content(commit.short_id)
end end
step 'I click atom feed link' do step 'I click atom feed link' do
......
...@@ -111,7 +111,7 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps ...@@ -111,7 +111,7 @@ class Spinach::Features::ProjectMergeRequests < Spinach::FeatureSteps
step 'I click on the commit in the merge request' do step 'I click on the commit in the merge request' do
within '.mr-commits' do within '.mr-commits' do
click_link sample_commit.id[0..8] click_link Commit.truncate_sha(sample_commit.id)
end end
end end
......
...@@ -60,7 +60,7 @@ describe GitlabMarkdownHelper do ...@@ -60,7 +60,7 @@ describe GitlabMarkdownHelper do
end end
it "should link using a short id" do it "should link using a short id" do
actual = "Backported from #{commit.short_id(6)}" actual = "Backported from #{commit.short_id}"
gfm(actual).should match(expected) gfm(actual).should match(expected)
end end
......
...@@ -75,7 +75,7 @@ eos ...@@ -75,7 +75,7 @@ eos
it_behaves_like 'a mentionable' do it_behaves_like 'a mentionable' do
let(:subject) { commit } let(:subject) { commit }
let(:mauthor) { create :user, email: commit.author_email } let(:mauthor) { create :user, email: commit.author_email }
let(:backref_text) { "commit #{subject.sha[0..5]}" } let(:backref_text) { "commit #{subject.id}" }
let(:set_mentionable_text) { ->(txt){ subject.stub(safe_message: txt) } } let(:set_mentionable_text) { ->(txt){ subject.stub(safe_message: txt) } }
# Include the subject in the repository stub. # Include the subject in the repository stub.
......
...@@ -228,7 +228,7 @@ describe Note do ...@@ -228,7 +228,7 @@ describe Note do
it { should be_valid } it { should be_valid }
its(:noteable) { should == issue } its(:noteable) { should == issue }
its(:note) { should == "_mentioned in commit #{commit.sha[0..5]}_" } its(:note) { should == "_mentioned in commit #{commit.sha}_" }
end end
context 'merge request from an issue' do context 'merge request from an issue' do
...@@ -267,7 +267,7 @@ describe Note do ...@@ -267,7 +267,7 @@ describe Note do
its(:noteable_type) { should == "Commit" } its(:noteable_type) { should == "Commit" }
its(:noteable_id) { should be_nil } its(:noteable_id) { should be_nil }
its(:commit_id) { should == commit.id } its(:commit_id) { should == commit.id }
its(:note) { should == "_mentioned in commit #{parent_commit.id[0...6]}_" } its(:note) { should == "_mentioned in commit #{parent_commit.id}_" }
end end
end end
......
...@@ -30,15 +30,15 @@ def common_mentionable_setup ...@@ -30,15 +30,15 @@ def common_mentionable_setup
"!#{mentioned_mr.iid}, " + "!#{mentioned_mr.iid}, " +
"#{ext_proj.path_with_namespace}##{ext_issue.iid}, " + "#{ext_proj.path_with_namespace}##{ext_issue.iid}, " +
"#{ext_proj.path_with_namespace}!#{ext_mr.iid}, " + "#{ext_proj.path_with_namespace}!#{ext_mr.iid}, " +
"#{ext_proj.path_with_namespace}@#{ext_commit.id[0..5]}, " + "#{ext_proj.path_with_namespace}@#{ext_commit.short_id}, " +
"#{mentioned_commit.sha[0..5]} and itself as #{backref_text}" "#{mentioned_commit.sha[0..10]} and itself as #{backref_text}"
end end
before do before do
# Wire the project's repository to return the mentioned commit, and +nil+ for any # Wire the project's repository to return the mentioned commit, and +nil+ for any
# unrecognized commits. # unrecognized commits.
commitmap = { '123456' => mentioned_commit } commitmap = { '1234567890a' => mentioned_commit }
extra_commits.each { |c| commitmap[c.sha[0..5]] = c } extra_commits.each { |c| commitmap[c.short_id] = c }
mproject.repository.stub(:commit) { |sha| commitmap[sha] } mproject.repository.stub(:commit) { |sha| commitmap[sha] }
set_mentionable_text.call(ref_string) set_mentionable_text.call(ref_string)
end end
...@@ -54,7 +54,6 @@ shared_examples 'a mentionable' do ...@@ -54,7 +54,6 @@ shared_examples 'a mentionable' do
it "extracts references from its reference property" do it "extracts references from its reference property" do
# De-duplicate and omit itself # De-duplicate and omit itself
refs = subject.references(mproject) refs = subject.references(mproject)
refs.should have(6).items refs.should have(6).items
refs.should include(mentioned_issue) refs.should include(mentioned_issue)
refs.should include(mentioned_mr) refs.should include(mentioned_mr)
...@@ -90,7 +89,7 @@ shared_examples 'an editable mentionable' do ...@@ -90,7 +89,7 @@ shared_examples 'an editable mentionable' do
it 'creates new cross-reference notes when the mentionable text is edited' do it 'creates new cross-reference notes when the mentionable text is edited' do
new_text = "still mentions ##{mentioned_issue.iid}, " + new_text = "still mentions ##{mentioned_issue.iid}, " +
"#{mentioned_commit.sha[0..5]}, " + "#{mentioned_commit.sha[0..10]}, " +
"#{ext_issue.iid}, " + "#{ext_issue.iid}, " +
"new refs: ##{other_issue.iid}, " + "new refs: ##{other_issue.iid}, " +
"#{ext_proj.path_with_namespace}##{other_ext_issue.iid}" "#{ext_proj.path_with_namespace}##{other_ext_issue.iid}"
......
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