Commit 313b1e95 authored by Douwe Maan's avatar Douwe Maan

Rename fetch_refs to refmap

parent 254c5686
...@@ -1020,14 +1020,14 @@ class Repository ...@@ -1020,14 +1020,14 @@ class Repository
run_git(args).first.lines.map(&:strip) run_git(args).first.lines.map(&:strip)
end 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 unless remote_name
remote_name = "tmp-#{SecureRandom.hex}" remote_name = "tmp-#{SecureRandom.hex}"
tmp_remote_name = true tmp_remote_name = true
end end
add_remote(remote_name, url) 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) fetch_remote(remote_name, forced: forced)
ensure ensure
remove_remote(remote_name) if tmp_remote_name remove_remote(remote_name) if tmp_remote_name
......
...@@ -51,11 +51,11 @@ module Projects ...@@ -51,11 +51,11 @@ module Projects
def import_repository def import_repository
begin 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.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 else
gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, project.import_url) gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, project.import_url)
end end
......
module Gitlab module Gitlab
module Git module Git
module RepositoryMirroring module RepositoryMirroring
FETCH_REFS = { REFMAPS = {
# `:all` is used to define repository as equivalent as "git clone --mirror" # With `:all_refs`, the repository is equivalent to the result of `git clone --mirror`
all: '+refs/*:refs/*', all_refs: '+refs/*:refs/*',
heads: '+refs/heads/*:refs/heads/*', heads: '+refs/heads/*:refs/heads/*',
tags: '+refs/tags/*:refs/tags/*' tags: '+refs/tags/*:refs/tags/*'
}.freeze }.freeze
RemoteError = Class.new(StandardError) RemoteError = Class.new(StandardError)
def set_remote_as_mirror(remote_name, fetch_refs: :all) def set_remote_as_mirror(remote_name, refmap: :all_refs)
Array(fetch_refs).each_with_index do |fetch_ref, i| set_remote_refmap(remote_name, refmap)
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
rugged.config["remote.#{remote_name}.mirror"] = true rugged.config["remote.#{remote_name}.mirror"] = true
rugged.config["remote.#{remote_name}.prune"] = true rugged.config["remote.#{remote_name}.prune"] = true
end end
def add_remote_fetch_config(remote_name, refspec) def set_remote_refmap(remote_name, refmap)
run_git(%W[config --add remote.#{remote_name}.fetch #{refspec}]) 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 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`. # of `Repository`'s interface through `method_missing`.
# `Repository` has its own `fetch_as_mirror` which uses `gitlab-shell` and # `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. # takes some extra attributes, so we qualify this method name to prevent confusion.
......
module Gitlab module Gitlab
module GithubImport module GithubImport
def self.fetch_refs def self.refmap
[:heads, :tags, '+refs/pull/*/head:refs/merge-requests/*/head'] [:heads, :tags, '+refs/pull/*/head:refs/merge-requests/*/head']
end end
......
...@@ -45,8 +45,8 @@ module Gitlab ...@@ -45,8 +45,8 @@ module Gitlab
def import_repository def import_repository
project.ensure_repository project.ensure_repository
fetch_refs = Gitlab::GithubImport.fetch_refs refmap = Gitlab::GithubImport.refmap
project.repository.fetch_as_mirror(project.import_url, fetch_refs: fetch_refs, forced: true, remote_name: 'github') project.repository.fetch_as_mirror(project.import_url, refmap: refmap, forced: true, remote_name: 'github')
true true
rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e
......
...@@ -3,8 +3,8 @@ module Gitlab ...@@ -3,8 +3,8 @@ module Gitlab
class Importer class Importer
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
def self.fetch_refs def self.refmap
Gitlab::GithubImport.fetch_refs Gitlab::GithubImport.refmap
end end
attr_reader :errors, :project, :repo, :repo_url attr_reader :errors, :project, :repo, :repo_url
......
...@@ -166,7 +166,7 @@ describe Gitlab::GithubImport::Importer::RepositoryImporter do ...@@ -166,7 +166,7 @@ describe Gitlab::GithubImport::Importer::RepositoryImporter do
expect(repository) expect(repository)
.to receive(:fetch_as_mirror) .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) expect(importer.import_repository).to eq(true)
end end
......
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