Commit 1800a854 authored by Tiago Botelho's avatar Tiago Botelho

Adds option to overwrite a diverged branch when mirroring

parent 0c71e3df
---
title: Add option to overwrite diverged branches for pull mirrors
merge_request: 4559
author:
type: added
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -1913,6 +1913,7 @@ ActiveRecord::Schema.define(version: 20180213131630) do ...@@ -1913,6 +1913,7 @@ ActiveRecord::Schema.define(version: 20180213131630) do
t.boolean "only_mirror_protected_branches" t.boolean "only_mirror_protected_branches"
t.boolean "pull_mirror_available_overridden" t.boolean "pull_mirror_available_overridden"
t.integer "jobs_cache_index" t.integer "jobs_cache_index"
t.boolean "mirror_overwrites_diverged_branches"
end end
add_index "projects", ["ci_id"], name: "index_projects_on_ci_id", using: :btree 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 ...@@ -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. 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 ### Hard failure
>[Introduced][ee-3117] in GitLab Enterprise Edition 10.2. >[Introduced][ee-3117] in GitLab Enterprise Edition 10.2.
......
...@@ -81,6 +81,7 @@ class Projects::MirrorsController < Projects::ApplicationController ...@@ -81,6 +81,7 @@ class Projects::MirrorsController < Projects::ApplicationController
:mirror_user_id, :mirror_user_id,
:mirror_trigger_builds, :mirror_trigger_builds,
:only_mirror_protected_branches, :only_mirror_protected_branches,
:mirror_overwrites_diverged_branches,
import_data_attributes: %i[ import_data_attributes: %i[
id id
......
...@@ -5,6 +5,8 @@ class ProjectMirrorEntity < Grape::Entity ...@@ -5,6 +5,8 @@ class ProjectMirrorEntity < Grape::Entity
expose :username_only_import_url expose :username_only_import_url
expose :mirror_user_id expose :mirror_user_id
expose :mirror_trigger_builds expose :mirror_trigger_builds
expose :only_mirror_protected_branches
expose :mirror_overwrites_diverged_branches
expose :import_data_attributes do |project| expose :import_data_attributes do |project|
import_data = project.import_data import_data = project.import_data
......
...@@ -45,10 +45,7 @@ module Projects ...@@ -45,10 +45,7 @@ module Projects
elsif local_branch.dereferenced_target == upstream_branch.dereferenced_target elsif local_branch.dereferenced_target == upstream_branch.dereferenced_target
# Already up to date # Already up to date
elsif repository.diverged_from_upstream?(name) elsif repository.diverged_from_upstream?(name)
# Cannot be updated handle_diverged_branch(upstream_branch, local_branch, name, errors)
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
else else
begin begin
repository.ff_merge(current_user, upstream_branch.dereferenced_target, name) repository.ff_merge(current_user, upstream_branch.dereferenced_target, name)
...@@ -95,6 +92,20 @@ module Projects ...@@ -95,6 +92,20 @@ module Projects
fetch_result fetch_result
end 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 # 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. # have no target.
def repository_tags_with_target def repository_tags_with_target
......
...@@ -47,6 +47,15 @@ ...@@ -47,6 +47,15 @@
= f.label :only_mirror_protected_branches, class: 'label-light' = f.label :only_mirror_protected_branches, class: 'label-light'
= link_to icon('question-circle'), help_page_path('user/project/protected_branches') = 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? - if @project.builds_enabled?
= render "shared/mirror_trigger_builds_setting", f: f = 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 ...@@ -20,6 +20,8 @@ describe ProjectMirrorEntity do
username_only_import_url: project.username_only_import_url, username_only_import_url: project.username_only_import_url,
mirror_user_id: project.mirror_user_id, mirror_user_id: project.mirror_user_id,
mirror_trigger_builds: project.mirror_trigger_builds, 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: { import_data_attributes: {
id: import_data.id, id: import_data.id,
auth_method: 'password', auth_method: 'password',
...@@ -48,6 +50,8 @@ describe ProjectMirrorEntity do ...@@ -48,6 +50,8 @@ describe ProjectMirrorEntity do
username_only_import_url: project.username_only_import_url, username_only_import_url: project.username_only_import_url,
mirror_user_id: project.mirror_user_id, mirror_user_id: project.mirror_user_id,
mirror_trigger_builds: project.mirror_trigger_builds, 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: { import_data_attributes: {
id: import_data.id, id: import_data.id,
auth_method: 'ssh_public_key', auth_method: 'ssh_public_key',
...@@ -75,6 +79,8 @@ describe ProjectMirrorEntity do ...@@ -75,6 +79,8 @@ describe ProjectMirrorEntity do
username_only_import_url: nil, username_only_import_url: nil,
mirror_user_id: nil, mirror_user_id: nil,
mirror_trigger_builds: false, mirror_trigger_builds: false,
only_mirror_protected_branches: true,
mirror_overwrites_diverged_branches: nil,
import_data_attributes: nil, import_data_attributes: nil,
remote_mirrors_attributes: [ remote_mirrors_attributes: [
{ {
......
...@@ -55,6 +55,8 @@ describe Projects::UpdateMirrorService do ...@@ -55,6 +55,8 @@ describe Projects::UpdateMirrorService do
end end
describe "updating branches" do describe "updating branches" do
subject { described_class.new(project, project.owner) }
context 'when mirror only protected branches option is set' do context 'when mirror only protected branches option is set' do
let(:new_protected_branch_name) { 'new-branch' } let(:new_protected_branch_name) { 'new-branch' }
let(:protected_branch_name) { 'existing-branch' } let(:protected_branch_name) { 'existing-branch' }
...@@ -69,7 +71,7 @@ describe Projects::UpdateMirrorService do ...@@ -69,7 +71,7 @@ describe Projects::UpdateMirrorService do
stub_fetch_mirror(project) stub_fetch_mirror(project)
described_class.new(project, project.owner).execute subject.execute
expect(project.repository.branch_names).to include(new_protected_branch_name) expect(project.repository.branch_names).to include(new_protected_branch_name)
end end
...@@ -88,7 +90,7 @@ describe Projects::UpdateMirrorService do ...@@ -88,7 +90,7 @@ describe Projects::UpdateMirrorService do
stub_fetch_mirror(project) stub_fetch_mirror(project)
described_class.new(project, project.owner).execute subject.execute
expect(project.repository.find_branch(protected_branch_name).dereferenced_target) expect(project.repository.find_branch(protected_branch_name).dereferenced_target)
.to eq(project.repository.find_branch('master').dereferenced_target) .to eq(project.repository.find_branch('master').dereferenced_target)
...@@ -97,7 +99,7 @@ describe Projects::UpdateMirrorService do ...@@ -97,7 +99,7 @@ describe Projects::UpdateMirrorService do
it "does not update unprotected branches" do it "does not update unprotected branches" do
stub_fetch_mirror(project) stub_fetch_mirror(project)
described_class.new(project, project.owner).execute subject.execute
expect(project.repository.find_branch(protected_branch_name).dereferenced_target) expect(project.repository.find_branch(protected_branch_name).dereferenced_target)
.not_to eq(project.repository.find_branch('master').dereferenced_target) .not_to eq(project.repository.find_branch('master').dereferenced_target)
...@@ -107,7 +109,7 @@ describe Projects::UpdateMirrorService do ...@@ -107,7 +109,7 @@ describe Projects::UpdateMirrorService do
it "creates new branches" do it "creates new branches" do
stub_fetch_mirror(project) stub_fetch_mirror(project)
described_class.new(project, project.owner).execute subject.execute
expect(project.repository.branch_names).to include('new-branch') expect(project.repository.branch_names).to include('new-branch')
end end
...@@ -115,19 +117,49 @@ describe Projects::UpdateMirrorService do ...@@ -115,19 +117,49 @@ describe Projects::UpdateMirrorService do
it "updates existing branches" do it "updates existing branches" do
stub_fetch_mirror(project) stub_fetch_mirror(project)
described_class.new(project, project.owner).execute subject.execute
expect(project.repository.find_branch('existing-branch').dereferenced_target) expect(project.repository.find_branch('existing-branch').dereferenced_target)
.to eq(project.repository.find_branch('master').dereferenced_target) .to eq(project.repository.find_branch('master').dereferenced_target)
end end
it "doesn't update diverged branches" do context 'with diverged branches' do
stub_fetch_mirror(project) 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) subject.execute
.not_to eq(project.repository.find_branch('master').dereferenced_target)
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 end
describe 'when project is empty' do describe 'when project is empty' do
...@@ -139,7 +171,7 @@ describe Projects::UpdateMirrorService do ...@@ -139,7 +171,7 @@ describe Projects::UpdateMirrorService do
allow(project).to receive(:fetch_mirror) { create_file(repository) } allow(project).to receive(:fetch_mirror) { create_file(repository) }
expect(CreateBranchService).not_to receive(:create_master_branch) 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') expect(repository.branch_names).not_to include('master')
end end
......
...@@ -939,6 +939,10 @@ module Gitlab ...@@ -939,6 +939,10 @@ module Gitlab
nil nil
end end
def update_branch(user, branch_name, newrev, oldrev)
Gitlab::Git::OperationService.new(user, self).update_branch(branch_name, newrev, oldrev)
end
AUTOCRLF_VALUES = { AUTOCRLF_VALUES = {
"true" => true, "true" => true,
"false" => false, "false" => false,
......
...@@ -111,6 +111,7 @@ excluded_attributes: ...@@ -111,6 +111,7 @@ excluded_attributes:
- :remote_mirror_available_overridden - :remote_mirror_available_overridden
- :only_mirror_protected_branches - :only_mirror_protected_branches
- :pull_mirror_available_overridden - :pull_mirror_available_overridden
- :mirror_overwrites_diverged_branches
snippets: snippets:
- :expired_at - :expired_at
merge_request_diff: 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