From 7a1e93d35b7280db8bc4128862c86223d76a8d6d Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 23 Nov 2017 16:51:55 +0100 Subject: [PATCH] Rename fetch_refs to refmap --- app/models/repository.rb | 4 +-- app/services/projects/import_service.rb | 6 ++-- lib/gitlab/git/repository_mirroring.rb | 35 ++++++++++--------- lib/gitlab/github_import.rb | 2 +- .../importer/repository_importer.rb | 4 +-- lib/gitlab/legacy_github_import/importer.rb | 4 +-- .../importer/repository_importer_spec.rb | 2 +- 7 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index 1b5eb8e2cea..c4784fd9235 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -972,14 +972,14 @@ class Repository run_git(args).first.lines.map(&:strip) end - def fetch_as_mirror(url, forced: false, fetch_refs: :all, remote_name: nil) + def fetch_as_mirror(url, forced: false, refmap: :all_refs, remote_name: nil) unless remote_name remote_name = "tmp-#{SecureRandom.hex}" tmp_remote_name = true end add_remote(remote_name, url) - set_remote_as_mirror(remote_name, fetch_refs: fetch_refs) + set_remote_as_mirror(remote_name, refmap: refmap) fetch_remote(remote_name, forced: forced) ensure remove_remote(remote_name) if tmp_remote_name diff --git a/app/services/projects/import_service.rb b/app/services/projects/import_service.rb index 26248eb8030..f2d676af5c3 100644 --- a/app/services/projects/import_service.rb +++ b/app/services/projects/import_service.rb @@ -51,11 +51,11 @@ module Projects def import_repository begin - fetch_refs = importer_class.try(:fetch_refs) if has_importer? + refmap = importer_class.try(:refmap) if has_importer? - if fetch_refs + if refmap project.ensure_repository - project.repository.fetch_as_mirror(project.import_url, fetch_refs: fetch_refs) + project.repository.fetch_as_mirror(project.import_url, refmap: refmap) else gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, project.import_url) end diff --git a/lib/gitlab/git/repository_mirroring.rb b/lib/gitlab/git/repository_mirroring.rb index 49e2b833fa7..392bef69e80 100644 --- a/lib/gitlab/git/repository_mirroring.rb +++ b/lib/gitlab/git/repository_mirroring.rb @@ -1,36 +1,37 @@ module Gitlab module Git module RepositoryMirroring - FETCH_REFS = { - # `:all` is used to define repository as equivalent as "git clone --mirror" - all: '+refs/*:refs/*', + REFMAPS = { + # With `:all_refs`, the repository is equivalent to the result of `git clone --mirror` + all_refs: '+refs/*:refs/*', heads: '+refs/heads/*:refs/heads/*', tags: '+refs/tags/*:refs/tags/*' }.freeze RemoteError = Class.new(StandardError) - def set_remote_as_mirror(remote_name, fetch_refs: :all) - Array(fetch_refs).each_with_index do |fetch_ref, i| - fetch_ref = FETCH_REFS[fetch_ref] || fetch_ref - - # Add first fetch with Rugged so it does not create its own. - if i == 0 - rugged.config["remote.#{remote_name}.fetch"] = fetch_ref - else - add_remote_fetch_config(remote_name, fetch_ref) - end - end + def set_remote_as_mirror(remote_name, refmap: :all_refs) + set_remote_refmap(remote_name, refmap) rugged.config["remote.#{remote_name}.mirror"] = true rugged.config["remote.#{remote_name}.prune"] = true end - def add_remote_fetch_config(remote_name, refspec) - run_git(%W[config --add remote.#{remote_name}.fetch #{refspec}]) + def set_remote_refmap(remote_name, refmap) + Array(refmap).each_with_index do |refspec, i| + refspec = REFMAPS[refspec] || refspec + + # We need multiple `fetch` entries, but Rugged only allows replacing a config, not adding to it. + # To make sure we start from scratch, we set the first using rugged, and use `git` for any others + if i == 0 + rugged.config["remote.#{remote_name}.fetch"] = refspec + else + run_git(%W[config --add remote.#{remote_name}.fetch #{refspec}]) + end + end end - # Like all public `Gitlab::Git::Repository` methods, this method is part + # Like all_refs public `Gitlab::Git::Repository` methods, this method is part # of `Repository`'s interface through `method_missing`. # `Repository` has its own `fetch_as_mirror` which uses `gitlab-shell` and # takes some extra attributes, so we qualify this method name to prevent confusion. diff --git a/lib/gitlab/github_import.rb b/lib/gitlab/github_import.rb index 427dab82e19..65b5e30c70f 100644 --- a/lib/gitlab/github_import.rb +++ b/lib/gitlab/github_import.rb @@ -1,6 +1,6 @@ module Gitlab module GithubImport - def self.fetch_refs + def self.refmap [:heads, :tags, '+refs/pull/*/head:refs/merge-requests/*/head'] end diff --git a/lib/gitlab/github_import/importer/repository_importer.rb b/lib/gitlab/github_import/importer/repository_importer.rb index 7b1994de012..9cf2e7fd871 100644 --- a/lib/gitlab/github_import/importer/repository_importer.rb +++ b/lib/gitlab/github_import/importer/repository_importer.rb @@ -45,8 +45,8 @@ module Gitlab def import_repository project.ensure_repository - fetch_refs = Gitlab::GithubImport.fetch_refs - project.repository.fetch_as_mirror(project.import_url, fetch_refs: fetch_refs, forced: true, remote_name: 'github') + refmap = Gitlab::GithubImport.refmap + project.repository.fetch_as_mirror(project.import_url, refmap: refmap, forced: true, remote_name: 'github') true rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e diff --git a/lib/gitlab/legacy_github_import/importer.rb b/lib/gitlab/legacy_github_import/importer.rb index 0e5425d01fd..0526ef9eb13 100644 --- a/lib/gitlab/legacy_github_import/importer.rb +++ b/lib/gitlab/legacy_github_import/importer.rb @@ -3,8 +3,8 @@ module Gitlab class Importer include Gitlab::ShellAdapter - def self.fetch_refs - Gitlab::GithubImport.fetch_refs + def self.refmap + Gitlab::GithubImport.refmap end attr_reader :errors, :project, :repo, :repo_url diff --git a/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb b/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb index f827902e7b8..168e5d07504 100644 --- a/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb +++ b/spec/lib/gitlab/github_import/importer/repository_importer_spec.rb @@ -166,7 +166,7 @@ describe Gitlab::GithubImport::Importer::RepositoryImporter do expect(repository) .to receive(:fetch_as_mirror) - .with(project.import_url, fetch_refs: Gitlab::GithubImport.fetch_refs, forced: true, remote_name: 'github') + .with(project.import_url, refmap: Gitlab::GithubImport.refmap, forced: true, remote_name: 'github') expect(importer.import_repository).to eq(true) end