Commit 988836bc authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor MergeRequestDiff model

Since MergeRequestDiff is not about branches and current state of merge
request diff anymore I removed most of branch related method and added
validation for head/start/base commit sha. From this point
MergeRequestDiff is about saving diff between branches only once at moment of
creation. Once created MergeRequestDiff should not be changes. Because
of that we should not rely on changes in source/target branches when
read from MergeRequestDiff
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 5d5f2cf3
...@@ -8,9 +8,6 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -8,9 +8,6 @@ class MergeRequestDiff < ActiveRecord::Base
belongs_to :merge_request belongs_to :merge_request
delegate :source_branch_sha, :target_branch_sha,
:target_branch, :source_branch, to: :merge_request, prefix: nil
state_machine :state, initial: :empty do state_machine :state, initial: :empty do
state :collected state :collected
state :overflow state :overflow
...@@ -25,14 +22,23 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -25,14 +22,23 @@ class MergeRequestDiff < ActiveRecord::Base
serialize :st_commits serialize :st_commits
serialize :st_diffs serialize :st_diffs
after_initialize :set_diff_range validates :head_commit_sha, presence: true
validates :start_commit_sha, presence: true
validates :base_commit_sha, presence: true
after_initialize :ensure_head_commit_sha, if: :persisted?
before_create :set_diff_range, unless: :importing?
after_create :reload_content, unless: :importing? after_create :reload_content, unless: :importing?
after_save :keep_around_commits, unless: :importing? after_save :keep_around_commits, unless: :importing?
def ensure_head_commit_sha
self.head_commit_sha ||= last_commit.sha
end
def set_diff_range def set_diff_range
self.start_commit_sha ||= target_branch_sha self.start_commit_sha ||= merge_request.target_branch_sha
self.head_commit_sha ||= source_branch_sha self.head_commit_sha ||= merge_request.source_branch_sha
self.base_commit_sha ||= branch_base_sha self.base_commit_sha ||= find_base_sha
end end
def reload_content def reload_content
...@@ -199,14 +205,10 @@ class MergeRequestDiff < ActiveRecord::Base ...@@ -199,14 +205,10 @@ class MergeRequestDiff < ActiveRecord::Base
project.repository project.repository
end end
def branch_base_commit def find_base_sha
return unless source_branch_sha && target_branch_sha return unless head_commit_sha && start_commit_sha
project.merge_base_commit(source_branch_sha, target_branch_sha)
end
def branch_base_sha project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
branch_base_commit.try(:sha)
end end
def utf8_st_diffs def utf8_st_diffs
......
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