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