Commit 84805b4e authored by Douwe Maan's avatar Douwe Maan

Merge branch '4723-add-option-to-overwrite-diverged-branches-when-pull-mirroring' into 'master'

Resolve "Add option to overwrite diverged branches when pull mirroring"

Closes #4723

See merge request gitlab-org/gitlab-ee!4559
parents 217f6f04 1800a854
---
title: Add option to overwrite diverged branches for pull mirrors
merge_request: 4559
author:
type: added
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180213131630) do
ActiveRecord::Schema.define(version: 20180215143644) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -1913,6 +1913,7 @@ ActiveRecord::Schema.define(version: 20180213131630) do
t.boolean "only_mirror_protected_branches"
t.boolean "pull_mirror_available_overridden"
t.integer "jobs_cache_index"
t.boolean "mirror_overwrites_diverged_branches"
end
add_index "projects", ["ci_id"], name: "index_projects_on_ci_id", using: :btree
......
......@@ -102,6 +102,15 @@ You can choose to only pull the protected branches from your remote repository t
To use this option go to your project's repository settings page under pull mirror.
### Overwrite diverged branches
>[Introduced][ee-4559] in GitLab Enterprise Edition 10.6.
You can choose to always update your local branch with the remote version even
if your local version has diverged from the remote.
To use this option go to your project's repository settings page under pull mirror.
### Hard failure
>[Introduced][ee-3117] in GitLab Enterprise Edition 10.2.
......
......@@ -81,6 +81,7 @@ class Projects::MirrorsController < Projects::ApplicationController
:mirror_user_id,
:mirror_trigger_builds,
:only_mirror_protected_branches,
:mirror_overwrites_diverged_branches,
import_data_attributes: %i[
id
......
......@@ -5,6 +5,8 @@ class ProjectMirrorEntity < Grape::Entity
expose :username_only_import_url
expose :mirror_user_id
expose :mirror_trigger_builds
expose :only_mirror_protected_branches
expose :mirror_overwrites_diverged_branches
expose :import_data_attributes do |project|
import_data = project.import_data
......
......@@ -45,10 +45,7 @@ module Projects
elsif local_branch.dereferenced_target == upstream_branch.dereferenced_target
# Already up to date
elsif repository.diverged_from_upstream?(name)
# Cannot be updated
if name == project.default_branch
errors << "The default branch (#{project.default_branch}) has diverged from its upstream counterpart and could not be updated automatically."
end
handle_diverged_branch(upstream_branch, local_branch, name, errors)
else
begin
repository.ff_merge(current_user, upstream_branch.dereferenced_target, name)
......@@ -95,6 +92,20 @@ module Projects
fetch_result
end
def handle_diverged_branch(upstream, local, branch_name, errors)
if project.mirror_overwrites_diverged_branches?
newrev = upstream.dereferenced_target.sha
oldrev = local.dereferenced_target.sha
repository.update_branch(current_user, branch_name, newrev, oldrev)
elsif branch_name == project.default_branch
# Cannot be updated
errors << "The default branch (#{project.default_branch}) has diverged from its upstream counterpart and could not be updated automatically."
else
# We ignore diverged branches other than the default branch
end
end
# In Git is possible to tag blob objects, and those blob objects don't point to a Git commit so those tags
# have no target.
def repository_tags_with_target
......
......@@ -47,6 +47,15 @@
= f.label :only_mirror_protected_branches, class: 'label-light'
= link_to icon('question-circle'), help_page_path('user/project/protected_branches')
.form-group
= f.check_box :mirror_overwrites_diverged_branches, class: 'pull-left'
.prepend-left-20
= f.label :mirror_overwrites_diverged_branches, "Overwrite diverged branches", class: 'label-light'
.help-block
If disabled, a diverged local branch will not be automatically updated with commits from its remote counterpart,
to prevent local data loss. If the default branch (#{@project.default_branch}) has diverged and cannot be updated,
mirroring will fail. Other diverged branches are silently ignored.
- if @project.builds_enabled?
= render "shared/mirror_trigger_builds_setting", f: f
......
class AddMirrorOverwritesDivergedBranchesToProject < ActiveRecord::Migration
DOWNTIME = false
def change
add_column :projects, :mirror_overwrites_diverged_branches, :boolean
end
end
......@@ -20,6 +20,8 @@ describe ProjectMirrorEntity do
username_only_import_url: project.username_only_import_url,
mirror_user_id: project.mirror_user_id,
mirror_trigger_builds: project.mirror_trigger_builds,
only_mirror_protected_branches: project.only_mirror_protected_branches,
mirror_overwrites_diverged_branches: project.mirror_overwrites_diverged_branches,
import_data_attributes: {
id: import_data.id,
auth_method: 'password',
......@@ -48,6 +50,8 @@ describe ProjectMirrorEntity do
username_only_import_url: project.username_only_import_url,
mirror_user_id: project.mirror_user_id,
mirror_trigger_builds: project.mirror_trigger_builds,
only_mirror_protected_branches: project.only_mirror_protected_branches,
mirror_overwrites_diverged_branches: project.mirror_overwrites_diverged_branches,
import_data_attributes: {
id: import_data.id,
auth_method: 'ssh_public_key',
......@@ -75,6 +79,8 @@ describe ProjectMirrorEntity do
username_only_import_url: nil,
mirror_user_id: nil,
mirror_trigger_builds: false,
only_mirror_protected_branches: true,
mirror_overwrites_diverged_branches: nil,
import_data_attributes: nil,
remote_mirrors_attributes: [
{
......
......@@ -55,6 +55,8 @@ describe Projects::UpdateMirrorService do
end
describe "updating branches" do
subject { described_class.new(project, project.owner) }
context 'when mirror only protected branches option is set' do
let(:new_protected_branch_name) { 'new-branch' }
let(:protected_branch_name) { 'existing-branch' }
......@@ -69,7 +71,7 @@ describe Projects::UpdateMirrorService do
stub_fetch_mirror(project)
described_class.new(project, project.owner).execute
subject.execute
expect(project.repository.branch_names).to include(new_protected_branch_name)
end
......@@ -88,7 +90,7 @@ describe Projects::UpdateMirrorService do
stub_fetch_mirror(project)
described_class.new(project, project.owner).execute
subject.execute
expect(project.repository.find_branch(protected_branch_name).dereferenced_target)
.to eq(project.repository.find_branch('master').dereferenced_target)
......@@ -97,7 +99,7 @@ describe Projects::UpdateMirrorService do
it "does not update unprotected branches" do
stub_fetch_mirror(project)
described_class.new(project, project.owner).execute
subject.execute
expect(project.repository.find_branch(protected_branch_name).dereferenced_target)
.not_to eq(project.repository.find_branch('master').dereferenced_target)
......@@ -107,7 +109,7 @@ describe Projects::UpdateMirrorService do
it "creates new branches" do
stub_fetch_mirror(project)
described_class.new(project, project.owner).execute
subject.execute
expect(project.repository.branch_names).to include('new-branch')
end
......@@ -115,19 +117,49 @@ describe Projects::UpdateMirrorService do
it "updates existing branches" do
stub_fetch_mirror(project)
described_class.new(project, project.owner).execute
subject.execute
expect(project.repository.find_branch('existing-branch').dereferenced_target)
.to eq(project.repository.find_branch('master').dereferenced_target)
end
it "doesn't update diverged branches" do
stub_fetch_mirror(project)
context 'with diverged branches' do
before do
stub_fetch_mirror(project)
end
described_class.new(project, project.owner).execute
context 'when mirror_overwrites_diverged_branches is true' do
it 'update diverged branches' do
project.mirror_overwrites_diverged_branches = true
expect(project.repository.find_branch('markdown').dereferenced_target)
.not_to eq(project.repository.find_branch('master').dereferenced_target)
subject.execute
expect(project.repository.find_branch('markdown').dereferenced_target)
.to eq(project.repository.find_branch('master').dereferenced_target)
end
end
context 'when mirror_overwrites_diverged_branches is false' do
it "doesn't update diverged branches" do
project.mirror_overwrites_diverged_branches = false
subject.execute
expect(project.repository.find_branch('markdown').dereferenced_target)
.not_to eq(project.repository.find_branch('master').dereferenced_target)
end
end
context 'when mirror_overwrites_diverged_branches is nil' do
it "doesn't update diverged branches" do
project.mirror_overwrites_diverged_branches = nil
subject.execute
expect(project.repository.find_branch('markdown').dereferenced_target)
.not_to eq(project.repository.find_branch('master').dereferenced_target)
end
end
end
describe 'when project is empty' do
......@@ -139,7 +171,7 @@ describe Projects::UpdateMirrorService do
allow(project).to receive(:fetch_mirror) { create_file(repository) }
expect(CreateBranchService).not_to receive(:create_master_branch)
described_class.new(project, project.owner).execute
subject.execute
expect(repository.branch_names).not_to include('master')
end
......
......@@ -939,6 +939,10 @@ module Gitlab
nil
end
def update_branch(user, branch_name, newrev, oldrev)
Gitlab::Git::OperationService.new(user, self).update_branch(branch_name, newrev, oldrev)
end
AUTOCRLF_VALUES = {
"true" => true,
"false" => false,
......
......@@ -111,6 +111,7 @@ excluded_attributes:
- :remote_mirror_available_overridden
- :only_mirror_protected_branches
- :pull_mirror_available_overridden
- :mirror_overwrites_diverged_branches
snippets:
- :expired_at
merge_request_diff:
......
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