diff --git a/app/services/merge_requests/refresh_service.rb b/app/services/merge_requests/refresh_service.rb index 74448998dddde385fc25560b16580fb08ee95221..baf0936cc3d77942cddb8a8da4d5f0a6281f3abd 100644 --- a/app/services/merge_requests/refresh_service.rb +++ b/app/services/merge_requests/refresh_service.rb @@ -3,6 +3,7 @@ module MergeRequests def execute(oldrev, newrev, ref) return true unless ref =~ /heads/ + @oldrev, @newrev = oldrev, newrev @branch_name = ref.gsub("refs/heads/", "") @fork_merge_requests = @project.fork_merge_requests.opened @commits = @project.repository.commits_between(oldrev, newrev) @@ -35,6 +36,10 @@ module MergeRequests end end + def force_push? + Gitlab::ForcePushCheck.force_push?(@project, @oldrev, @newrev) + end + # Refresh merge request diff if we push to source or target branch of merge request # Note: we should update merge requests from forks too def reload_merge_requests @@ -43,8 +48,22 @@ module MergeRequests merge_requests = filter_merge_requests(merge_requests) merge_requests.each do |merge_request| - merge_request.reload_code - merge_request.mark_as_unchecked + + if merge_request.source_branch == @branch_name || force_push? + merge_request.reload_code + merge_request.mark_as_unchecked + else + mr_commit_ids = merge_request.commits.map(&:id) + push_commit_ids = @commits.map(&:id) + matches = mr_commit_ids & push_commit_ids + + if matches.any? + merge_request.reload_code + merge_request.mark_as_unchecked + else + merge_request.mark_as_unchecked + end + end end end diff --git a/lib/gitlab/force_push_check.rb b/lib/gitlab/force_push_check.rb new file mode 100644 index 0000000000000000000000000000000000000000..6a52cdba608ae3e426eba75eff4a02b537f2d740 --- /dev/null +++ b/lib/gitlab/force_push_check.rb @@ -0,0 +1,15 @@ +module Gitlab + class ForcePushCheck + def self.force_push?(project, oldrev, newrev) + return false if project.empty_repo? + + if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA + missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read + missed_refs.split("\n").size > 0 + else + false + end + end + end +end + diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 5f8cb19efdfb5b6d137c24384bb0b9bc12e55700..8b4729896b5fbddebdf2006f1d26b6353f711774 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -94,14 +94,7 @@ module Gitlab end def forced_push?(project, oldrev, newrev) - return false if project.empty_repo? - - if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA - missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read - missed_refs.split("\n").size > 0 - else - false - end + Gitlab::ForcePushCheck.force_push?(project, oldrev, newrev) end private