Commit 7a1e93d3 authored by Douwe Maan's avatar Douwe Maan

Rename fetch_refs to refmap

parent 0e6beaf5
......@@ -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
......
......@@ -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
......
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)
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.
......
module Gitlab
module GithubImport
def self.fetch_refs
def self.refmap
[:heads, :tags, '+refs/pull/*/head:refs/merge-requests/*/head']
end
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
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