Commit 84a15902 authored by Douwe Maan's avatar Douwe Maan

Let commit model know about its project.

parent b0ed2ff1
...@@ -79,7 +79,7 @@ module Emails ...@@ -79,7 +79,7 @@ module Emails
@disable_diffs = disable_diffs @disable_diffs = disable_diffs
if @compare if @compare
@commits = Commit.decorate(compare.commits) @commits = Commit.decorate(compare.commits, @project)
@diffs = compare.diffs @diffs = compare.diffs
end end
...@@ -101,8 +101,8 @@ module Emails ...@@ -101,8 +101,8 @@ module Emails
if @commits.length > 1 if @commits.length > 1
@target_url = namespace_project_compare_url(@project.namespace, @target_url = namespace_project_compare_url(@project.namespace,
@project, @project,
from: Commit.new(@compare.base), from: Commit.new(@compare.base, @project),
to: Commit.new(@compare.head)) to: Commit.new(@compare.head, @project))
@subject << "Deleted " if @reverse_compare @subject << "Deleted " if @reverse_compare
@subject << "#{@commits.length} commits: #{@commits.first.title}" @subject << "#{@commits.length} commits: #{@commits.first.title}"
else else
......
...@@ -6,6 +6,8 @@ class Commit ...@@ -6,6 +6,8 @@ class Commit
attr_mentionable :safe_message attr_mentionable :safe_message
attr_accessor :project
# Safe amount of changes (files and lines) in one commit to render # Safe amount of changes (files and lines) in one commit to render
# Used to prevent 500 error on huge commits by suppressing diff # Used to prevent 500 error on huge commits by suppressing diff
# #
...@@ -18,12 +20,12 @@ class Commit ...@@ -18,12 +20,12 @@ class Commit
DIFF_HARD_LIMIT_LINES = 50000 unless defined?(DIFF_HARD_LIMIT_LINES) DIFF_HARD_LIMIT_LINES = 50000 unless defined?(DIFF_HARD_LIMIT_LINES)
class << self class << self
def decorate(commits) def decorate(commits, project)
commits.map do |commit| commits.map do |commit|
if commit.kind_of?(Commit) if commit.kind_of?(Commit)
commit commit
else else
self.new(commit) self.new(commit, project)
end end
end end
end end
...@@ -41,10 +43,11 @@ class Commit ...@@ -41,10 +43,11 @@ class Commit
attr_accessor :raw attr_accessor :raw
def initialize(raw_commit) def initialize(raw_commit, project)
raise "Nil as raw commit passed" unless raw_commit raise "Nil as raw commit passed" unless raw_commit
@raw = raw_commit @raw = raw_commit
@project = project
end end
def id def id
...@@ -169,6 +172,6 @@ class Commit ...@@ -169,6 +172,6 @@ class Commit
end end
def parents def parents
@parents ||= Commit.decorate(super) @parents ||= Commit.decorate(super, project)
end end
end end
...@@ -67,7 +67,7 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -67,7 +67,7 @@ class MergeRequestDiff < ActiveRecord::Base
end end
def load_commits(array) def load_commits(array)
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash)) } array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
end end
def dump_diffs(diffs) def dump_diffs(diffs)
...@@ -88,7 +88,7 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -88,7 +88,7 @@ class MergeRequestDiff < ActiveRecord::Base
commits = compare_result.commits commits = compare_result.commits
if commits.present? if commits.present?
commits = Commit.decorate(commits). commits = Commit.decorate(commits, merge_request.source_project).
sort_by(&:created_at). sort_by(&:created_at).
reverse reverse
end end
......
...@@ -254,7 +254,11 @@ class Project < ActiveRecord::Base ...@@ -254,7 +254,11 @@ class Project < ActiveRecord::Base
end end
def repository def repository
@repository ||= Repository.new(path_with_namespace) @repository ||= Repository.new(path_with_namespace, nil, self)
end
def commit(id)
repository.commit(id)
end end
def saved? def saved?
......
...@@ -112,7 +112,7 @@ class ProjectWiki ...@@ -112,7 +112,7 @@ class ProjectWiki
end end
def repository def repository
Repository.new(path_with_namespace, default_branch) Repository.new(path_with_namespace, default_branch, @project)
end end
def default_branch def default_branch
......
class Repository class Repository
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
attr_accessor :raw_repository, :path_with_namespace attr_accessor :raw_repository, :path_with_namespace, :project
def initialize(path_with_namespace, default_branch = nil) def initialize(path_with_namespace, default_branch = nil, project = nil)
@path_with_namespace = path_with_namespace @path_with_namespace = path_with_namespace
@raw_repository = Gitlab::Git::Repository.new(path_to_repo) if path_with_namespace @raw_repository = Gitlab::Git::Repository.new(path_to_repo) if path_with_namespace
@project = project
rescue Gitlab::Git::Repository::NoRepository rescue Gitlab::Git::Repository::NoRepository
nil nil
end end
...@@ -28,7 +29,7 @@ class Repository ...@@ -28,7 +29,7 @@ class Repository
def commit(id = 'HEAD') def commit(id = 'HEAD')
return nil unless raw_repository return nil unless raw_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, @project) if commit
commit commit
rescue Rugged::OdbError rescue Rugged::OdbError
nil nil
...@@ -42,13 +43,13 @@ class Repository ...@@ -42,13 +43,13 @@ class Repository
limit: limit, limit: limit,
offset: offset, offset: offset,
) )
commits = Commit.decorate(commits) if commits.present? commits = Commit.decorate(commits, @project) if commits.present?
commits commits
end end
def commits_between(from, to) def commits_between(from, to)
commits = Gitlab::Git::Commit.between(raw_repository, from, to) commits = Gitlab::Git::Commit.between(raw_repository, from, to)
commits = Commit.decorate(commits) if commits.present? commits = Commit.decorate(commits, @project) if commits.present?
commits commits
end end
......
...@@ -29,7 +29,7 @@ module MergeRequests ...@@ -29,7 +29,7 @@ module MergeRequests
# At this point we decide if merge request can be created # At this point we decide if merge request can be created
# If we have at least one commit to merge -> creation allowed # If we have at least one commit to merge -> creation allowed
if commits.present? if commits.present?
merge_request.compare_commits = Commit.decorate(commits) merge_request.compare_commits = Commit.decorate(commits, merge_request.source_project)
merge_request.can_be_created = true merge_request.can_be_created = true
merge_request.compare_failed = false merge_request.compare_failed = false
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
.file-content.blame.highlight .file-content.blame.highlight
%table %table
- @blame.each do |commit, lines, since| - @blame.each do |commit, lines, since|
- commit = Commit.new(commit) - commit = Commit.new(commit, @project)
%tr %tr
%td.blame-commit %td.blame-commit
%span.commit %span.commit
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
Commits (#{@commits.count}) Commits (#{@commits.count})
- if @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE - if @commits.size > MergeRequestDiff::COMMITS_SAFE_SIZE
%ul.well-list %ul.well-list
- Commit.decorate(@commits.first(MergeRequestDiff::COMMITS_SAFE_SIZE)).each do |commit| - Commit.decorate(@commits.first(MergeRequestDiff::COMMITS_SAFE_SIZE), @project).each do |commit|
= render "projects/commits/inline_commit", commit: commit, project: @project = render "projects/commits/inline_commit", commit: commit, project: @project
%li.warning-row.unstyled %li.warning-row.unstyled
other #{@commits.size - MergeRequestDiff::COMMITS_SAFE_SIZE} commits hidden to prevent performance issues. other #{@commits.size - MergeRequestDiff::COMMITS_SAFE_SIZE} commits hidden to prevent performance issues.
- else - else
%ul.well-list= render Commit.decorate(@commits), project: @project %ul.well-list= render Commit.decorate(@commits, @project), project: @project
...@@ -137,8 +137,7 @@ class IrkerWorker ...@@ -137,8 +137,7 @@ class IrkerWorker
end end
def commit_from_id(project, id) def commit_from_id(project, id)
commit = Gitlab::Git::Commit.find(project.repository, id) project.commit(id)
Commit.new(commit)
end end
def files_count(commit) def files_count(commit)
......
...@@ -251,11 +251,11 @@ module API ...@@ -251,11 +251,11 @@ module API
class Compare < Grape::Entity class Compare < Grape::Entity
expose :commit, using: Entities::RepoCommit do |compare, options| expose :commit, using: Entities::RepoCommit do |compare, options|
Commit.decorate(compare.commits).last Commit.decorate(compare.commits, nil).last
end end
expose :commits, using: Entities::RepoCommit do |compare, options| expose :commits, using: Entities::RepoCommit do |compare, options|
Commit.decorate(compare.commits) Commit.decorate(compare.commits, nil)
end end
expose :diffs, using: Entities::RepoDiff do |compare, options| expose :diffs, using: Entities::RepoDiff do |compare, options|
......
...@@ -670,8 +670,8 @@ describe Notify do ...@@ -670,8 +670,8 @@ describe Notify do
let(:example_site_path) { root_path } let(:example_site_path) { root_path }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) } let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
let(:commits) { Commit.decorate(compare.commits) } let(:commits) { Commit.decorate(compare.commits, nil) }
let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: Commit.new(compare.base), to: Commit.new(compare.head)) } let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: Commit.new(compare.base, project), to: Commit.new(compare.head, project)) }
let(:send_from_committer_email) { false } let(:send_from_committer_email) { false }
subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare, reverse_compare: false, send_from_committer_email: send_from_committer_email) } subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare, reverse_compare: false, send_from_committer_email: send_from_committer_email) }
...@@ -774,7 +774,7 @@ describe Notify do ...@@ -774,7 +774,7 @@ describe Notify do
let(:example_site_path) { root_path } let(:example_site_path) { root_path }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_commit.parent_id, sample_commit.id) } let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_commit.parent_id, sample_commit.id) }
let(:commits) { Commit.decorate(compare.commits) } let(:commits) { Commit.decorate(compare.commits, nil) }
let(:diff_path) { namespace_project_commit_path(project.namespace, project, commits.first) } let(:diff_path) { namespace_project_commit_path(project.namespace, project, commits.first) }
subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare) } subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare) }
......
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