summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-29 15:08:59 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-29 15:08:59 +0000
commit23288f62da73fb0e30d8e7ce306665e8fda1b932 (patch)
tree2baf1339e4d7c7c35d6b8a52cfb90597a5d4cdf1 /app/models
parent7cc6872401eb487ed20dbb9d455f8bb9c97d9e39 (diff)
downloadgitlab-ce-23288f62da73fb0e30d8e7ce306665e8fda1b932.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/cached_commit.rb17
-rw-r--r--app/models/merge_request.rb6
-rw-r--r--app/models/merge_request_context_commit.rb35
-rw-r--r--app/models/merge_request_context_commit_diff_file.rb17
-rw-r--r--app/models/merge_request_diff.rb4
-rw-r--r--app/models/merge_request_diff_commit.rb17
-rw-r--r--app/models/project.rb4
7 files changed, 85 insertions, 15 deletions
diff --git a/app/models/concerns/cached_commit.rb b/app/models/concerns/cached_commit.rb
new file mode 100644
index 00000000000..183d5728743
--- /dev/null
+++ b/app/models/concerns/cached_commit.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+module CachedCommit
+ extend ActiveSupport::Concern
+
+ def to_hash
+ Gitlab::Git::Commit::SERIALIZE_KEYS.each_with_object({}) do |key, hash|
+ hash[key] = public_send(key) # rubocop:disable GitlabSecurity/PublicSend
+ end
+ end
+
+ # We don't save these, because they would need a table or a serialised
+ # field. They aren't used anywhere, so just pretend the commit has no parents.
+ def parent_ids
+ []
+ end
+end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 48c5c0152b5..3174a3269b4 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -34,6 +34,8 @@ class MergeRequest < ApplicationRecord
has_internal_id :iid, scope: :target_project, track_if: -> { !importing? }, init: ->(s) { s&.target_project&.merge_requests&.maximum(:iid) }
has_many :merge_request_diffs
+ has_many :merge_request_context_commits
+ has_many :merge_request_context_commit_diff_files, through: :merge_request_context_commits, source: :diff_files
has_many :merge_request_milestones
has_many :milestones, through: :merge_request_milestones
@@ -399,6 +401,10 @@ class MergeRequest < ApplicationRecord
"#{project.to_reference_base(from, full: full)}#{reference}"
end
+ def context_commits
+ @context_commits ||= merge_request_context_commits.map(&:to_commit)
+ end
+
def commits(limit: nil)
return merge_request_diff.commits(limit: limit) if persisted?
diff --git a/app/models/merge_request_context_commit.rb b/app/models/merge_request_context_commit.rb
new file mode 100644
index 00000000000..eecb10e6dbc
--- /dev/null
+++ b/app/models/merge_request_context_commit.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+class MergeRequestContextCommit < ApplicationRecord
+ include CachedCommit
+ include ShaAttribute
+
+ belongs_to :merge_request
+ has_many :diff_files, class_name: 'MergeRequestContextCommitDiffFile'
+
+ sha_attribute :sha
+
+ validates :sha, presence: true
+ validates :sha, uniqueness: { message: 'has already been added' }
+
+ # delete all MergeRequestContextCommit & MergeRequestContextCommitDiffFile for given merge_request & commit SHAs
+ def self.delete_bulk(merge_request, commits)
+ commit_ids = commits.map(&:sha)
+ merge_request.merge_request_context_commits.where(sha: commit_ids).delete_all
+ end
+
+ # create MergeRequestContextCommit by given commit sha and it's diff file record
+ def self.bulk_insert(*args)
+ Gitlab::Database.bulk_insert('merge_request_context_commits', *args)
+ end
+
+ def to_commit
+ # Here we are storing the commit sha because to_hash removes the sha parameter and we lose
+ # the reference, this happens because we are storing the ID in db and the Commit class replaces
+ # id with sha and removes it, so in our case it will be some incremented integer which is not
+ # what we want
+ commit_hash = attributes.except('id').to_hash
+ commit_hash['id'] = sha
+ Commit.from_hash(commit_hash, merge_request.target_project)
+ end
+end
diff --git a/app/models/merge_request_context_commit_diff_file.rb b/app/models/merge_request_context_commit_diff_file.rb
new file mode 100644
index 00000000000..9dce7c53ab6
--- /dev/null
+++ b/app/models/merge_request_context_commit_diff_file.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class MergeRequestContextCommitDiffFile < ApplicationRecord
+ include Gitlab::EncodingHelper
+ include ShaAttribute
+ include DiffFile
+
+ belongs_to :merge_request_context_commit, inverse_of: :diff_files
+
+ sha_attribute :sha
+ alias_attribute :id, :sha
+
+ # create MergeRequestContextCommitDiffFile by given diff file record(s)
+ def self.bulk_insert(*args)
+ Gitlab::Database.bulk_insert('merge_request_context_commit_diff_files', *args)
+ end
+end
diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb
index fa633a1a725..ffe95e8f034 100644
--- a/app/models/merge_request_diff.rb
+++ b/app/models/merge_request_diff.rb
@@ -560,6 +560,10 @@ class MergeRequestDiff < ApplicationRecord
opening_external_diff do
collection = merge_request_diff_files
+ if options[:include_context_commits]
+ collection += merge_request.merge_request_context_commit_diff_files
+ end
+
if paths = options[:paths]
collection = collection.where('old_path IN (?) OR new_path IN (?)', paths, paths)
end
diff --git a/app/models/merge_request_diff_commit.rb b/app/models/merge_request_diff_commit.rb
index b897bbc8cf5..aa41a68f184 100644
--- a/app/models/merge_request_diff_commit.rb
+++ b/app/models/merge_request_diff_commit.rb
@@ -2,6 +2,7 @@
class MergeRequestDiffCommit < ApplicationRecord
include ShaAttribute
+ include CachedCommit
belongs_to :merge_request_diff
@@ -9,8 +10,6 @@ class MergeRequestDiffCommit < ApplicationRecord
alias_attribute :id, :sha
def self.create_bulk(merge_request_diff_id, commits)
- sha_attribute = Gitlab::Database::ShaAttribute.new
-
rows = commits.map.with_index do |commit, index|
# See #parent_ids.
commit_hash = commit.to_hash.except(:parent_ids)
@@ -19,7 +18,7 @@ class MergeRequestDiffCommit < ApplicationRecord
commit_hash.merge(
merge_request_diff_id: merge_request_diff_id,
relative_order: index,
- sha: sha_attribute.serialize(sha), # rubocop:disable Cop/ActiveRecordSerialize
+ sha: Gitlab::Database::ShaAttribute.serialize(sha), # rubocop:disable Cop/ActiveRecordSerialize
authored_date: Gitlab::Database.sanitize_timestamp(commit_hash[:authored_date]),
committed_date: Gitlab::Database.sanitize_timestamp(commit_hash[:committed_date])
)
@@ -27,16 +26,4 @@ class MergeRequestDiffCommit < ApplicationRecord
Gitlab::Database.bulk_insert(self.table_name, rows)
end
-
- def to_hash
- Gitlab::Git::Commit::SERIALIZE_KEYS.each_with_object({}) do |key, hash|
- hash[key] = public_send(key) # rubocop:disable GitlabSecurity/PublicSend
- end
- end
-
- # We don't save these, because they would need a table or a serialised
- # field. They aren't used anywhere, so just pretend the commit has no parents.
- def parent_ids
- []
- end
end
diff --git a/app/models/project.rb b/app/models/project.rb
index 3aa8430f3a2..8cb35904d92 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -763,6 +763,10 @@ class Project < ApplicationRecord
Feature.enabled?(:unlink_fork_network_upon_visibility_decrease, self, default_enabled: true)
end
+ def context_commits_enabled?
+ Feature.enabled?(:context_commits, default_enabled: true)
+ end
+
def empty_repo?
repository.empty?
end