summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/git/repository.rb10
-rw-r--r--lib/gitlab/git/repository_mirroring.rb46
-rw-r--r--lib/gitlab/github_import.rb4
-rw-r--r--lib/gitlab/github_import/importer/repository_importer.rb17
-rw-r--r--lib/gitlab/legacy_github_import/importer.rb4
5 files changed, 43 insertions, 38 deletions
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index dcca20c75ef..69897bf1402 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -1150,10 +1150,12 @@ module Gitlab
@has_visible_content = has_local_branches?
end
- def fetch(remote = 'origin')
- args = %W(#{Gitlab.config.git.bin_path} fetch #{remote})
-
- popen(args, @path).last.zero?
+ # Like all public `Gitlab::Git::Repository` methods, this method is part
+ # of `Repository`'s interface through `method_missing`.
+ # `Repository` has its own `fetch_remote` which uses `gitlab-shell` and
+ # takes some extra attributes, so we qualify this method name to prevent confusion.
+ def fetch_remote_without_shell(remote = 'origin')
+ run_git(['fetch', remote]).last.zero?
end
def blob_at(sha, path)
diff --git a/lib/gitlab/git/repository_mirroring.rb b/lib/gitlab/git/repository_mirroring.rb
index 4500482d68f..49e2b833fa7 100644
--- a/lib/gitlab/git/repository_mirroring.rb
+++ b/lib/gitlab/git/repository_mirroring.rb
@@ -1,24 +1,26 @@
module Gitlab
module Git
module RepositoryMirroring
- IMPORT_HEAD_REFS = '+refs/heads/*:refs/heads/*'.freeze
- IMPORT_TAG_REFS = '+refs/tags/*:refs/tags/*'.freeze
- MIRROR_REMOTE = 'mirror'.freeze
+ FETCH_REFS = {
+ # `:all` is used to define repository as equivalent as "git clone --mirror"
+ all: '+refs/*:refs/*',
+ heads: '+refs/heads/*:refs/heads/*',
+ tags: '+refs/tags/*:refs/tags/*'
+ }.freeze
RemoteError = Class.new(StandardError)
- def set_remote_as_mirror(remote_name)
- # This is used to define repository as equivalent as "git clone --mirror"
- rugged.config["remote.#{remote_name}.fetch"] = 'refs/*:refs/*'
- rugged.config["remote.#{remote_name}.mirror"] = true
- rugged.config["remote.#{remote_name}.prune"] = true
- end
+ 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
- def set_import_remote_as_mirror(remote_name)
- # Add first fetch with Rugged so it does not create its own.
- rugged.config["remote.#{remote_name}.fetch"] = IMPORT_HEAD_REFS
-
- add_remote_fetch_config(remote_name, IMPORT_TAG_REFS)
+ # 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}.prune"] = true
@@ -28,11 +30,17 @@ module Gitlab
run_git(%W[config --add remote.#{remote_name}.fetch #{refspec}])
end
- def fetch_mirror(url)
- add_remote(MIRROR_REMOTE, url)
- set_remote_as_mirror(MIRROR_REMOTE)
- fetch(MIRROR_REMOTE)
- remove_remote(MIRROR_REMOTE)
+ # Like all 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.
+ def fetch_as_mirror_without_shell(url)
+ remote_name = "tmp-#{SecureRandom.hex}"
+ add_remote(remote_name, url)
+ set_remote_as_mirror(remote_name)
+ fetch_remote_without_shell(remote_name)
+ ensure
+ remove_remote(remote_name) if remote_name
end
def remote_tags(remote)
diff --git a/lib/gitlab/github_import.rb b/lib/gitlab/github_import.rb
index d2ae4c1255e..427dab82e19 100644
--- a/lib/gitlab/github_import.rb
+++ b/lib/gitlab/github_import.rb
@@ -1,5 +1,9 @@
module Gitlab
module GithubImport
+ def self.fetch_refs
+ [:heads, :tags, '+refs/pull/*/head:refs/merge-requests/*/head']
+ end
+
def self.new_client_for(project, token: nil, parallel: true)
token_to_use = token || project.import_data&.credentials&.fetch(:user)
diff --git a/lib/gitlab/github_import/importer/repository_importer.rb b/lib/gitlab/github_import/importer/repository_importer.rb
index 0b67fc8db73..7b1994de012 100644
--- a/lib/gitlab/github_import/importer/repository_importer.rb
+++ b/lib/gitlab/github_import/importer/repository_importer.rb
@@ -45,27 +45,14 @@ module Gitlab
def import_repository
project.ensure_repository
- configure_repository_remote
-
- project.repository.fetch_remote('github', forced: true)
+ fetch_refs = Gitlab::GithubImport.fetch_refs
+ project.repository.fetch_as_mirror(project.import_url, fetch_refs: fetch_refs, forced: true, remote_name: 'github')
true
rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e
fail_import("Failed to import the repository: #{e.message}")
end
- def configure_repository_remote
- return if project.repository.remote_exists?('github')
-
- project.repository.add_remote('github', project.import_url)
- project.repository.set_import_remote_as_mirror('github')
-
- project.repository.add_remote_fetch_config(
- 'github',
- '+refs/pull/*/head:refs/merge-requests/*/head'
- )
- end
-
def import_wiki_repository
wiki_path = "#{project.disk_path}.wiki"
wiki_url = project.import_url.sub(/\.git\z/, '.wiki.git')
diff --git a/lib/gitlab/legacy_github_import/importer.rb b/lib/gitlab/legacy_github_import/importer.rb
index 4d096e5a741..0e5425d01fd 100644
--- a/lib/gitlab/legacy_github_import/importer.rb
+++ b/lib/gitlab/legacy_github_import/importer.rb
@@ -3,6 +3,10 @@ module Gitlab
class Importer
include Gitlab::ShellAdapter
+ def self.fetch_refs
+ Gitlab::GithubImport.fetch_refs
+ end
+
attr_reader :errors, :project, :repo, :repo_url
def initialize(project)