summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-07-18 21:35:24 -0700
committerStan Hu <stanhu@gmail.com>2018-07-18 21:35:24 -0700
commit930ca2d599802ae0983770311b641080f23f056d (patch)
treee6793d1b859747f1f26899067e92a6c94ebf16a9 /lib
parenta6dfed3a587feb45a793911f3d750665be92b315 (diff)
parent98eccfc44c597ba14939659ca3b9150197129961 (diff)
downloadgitlab-ce-930ca2d599802ae0983770311b641080f23f056d.tar.gz
Merge branch 'master' into sh-support-bitbucket-server-import
Diffstat (limited to 'lib')
-rw-r--r--lib/api/entities.rb2
-rw-r--r--lib/gitlab/checks/change_access.rb25
-rw-r--r--lib/gitlab/git/commit.rb64
-rw-r--r--lib/gitlab/git/repository.rb104
-rw-r--r--lib/gitlab/git/repository_mirroring.rb2
-rw-r--r--lib/gitlab/git/rev_list.rb50
-rw-r--r--lib/gitlab/gitaly_client/ref_service.rb19
-rw-r--r--lib/gitlab/import_export/file_importer.rb3
-rw-r--r--lib/uploaded_file.rb12
9 files changed, 75 insertions, 206 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index b256c33c631..3f3a95ea8e6 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -701,7 +701,7 @@ module API
expose :system?, as: :system
expose :noteable_id, :noteable_type
- expose :position, if: ->(note, options) { note.diff_note? } do |note|
+ expose :position, if: ->(note, options) { note.is_a?(DiffNote) } do |note|
note.position.to_h
end
diff --git a/lib/gitlab/checks/change_access.rb b/lib/gitlab/checks/change_access.rb
index f76a6fb5f17..7a4224e5bbe 100644
--- a/lib/gitlab/checks/change_access.rb
+++ b/lib/gitlab/checks/change_access.rb
@@ -93,7 +93,7 @@ module Gitlab
end
else
unless user_access.can_push_to_branch?(branch_name)
- raise GitAccess::UnauthorizedError, ERROR_MESSAGES[:push_protected_branch]
+ raise GitAccess::UnauthorizedError, push_to_protected_branch_rejected_message
end
end
end
@@ -140,6 +140,29 @@ module Gitlab
private
+ def push_to_protected_branch_rejected_message
+ if project.empty_repo?
+ empty_project_push_message
+ else
+ ERROR_MESSAGES[:push_protected_branch]
+ end
+ end
+
+ def empty_project_push_message
+ <<~MESSAGE
+
+ A default branch (e.g. master) does not yet exist for #{project.full_path}
+ Ask a project Owner or Maintainer to create a default branch:
+
+ #{project_members_url}
+
+ MESSAGE
+ end
+
+ def project_members_url
+ Gitlab::Routing.url_helpers.project_project_members_url(project)
+ end
+
def should_run_commit_validations?
commit_check.validate_lfs_file_locks?
end
diff --git a/lib/gitlab/git/commit.rb b/lib/gitlab/git/commit.rb
index 4e2d817d12c..5b264868af0 100644
--- a/lib/gitlab/git/commit.rb
+++ b/lib/gitlab/git/commit.rb
@@ -1,4 +1,4 @@
-# Gitlab::Git::Commit is a wrapper around native Rugged::Commit object
+# Gitlab::Git::Commit is a wrapper around Gitaly::GitCommit
module Gitlab
module Git
class Commit
@@ -55,7 +55,6 @@ module Gitlab
# A rugged reference?
commit_id = Gitlab::Git::Ref.dereference_object(commit_id)
- return decorate(repo, commit_id) if commit_id.is_a?(Rugged::Commit)
# Some weird thing?
return nil unless commit_id.is_a?(String)
@@ -68,9 +67,7 @@ module Gitlab
end
decorate(repo, commit) if commit
- rescue Rugged::ReferenceError, Rugged::InvalidError, Rugged::ObjectError,
- Gitlab::Git::CommandError, Gitlab::Git::Repository::NoRepository,
- Rugged::OdbError, Rugged::TreeError, ArgumentError
+ rescue Gitlab::Git::CommandError, Gitlab::Git::Repository::NoRepository, ArgumentError
nil
end
@@ -142,20 +139,6 @@ module Gitlab
Gitlab::Git::Commit.new(repository, commit, ref)
end
- # Returns the `Rugged` sorting type constant for one or more given
- # sort types. Valid keys are `:none`, `:topo`, and `:date`, or an array
- # containing more than one of them. `:date` uses a combination of date and
- # topological sorting to closer mimic git's native ordering.
- def rugged_sort_type(sort_type)
- @rugged_sort_types ||= {
- none: Rugged::SORT_NONE,
- topo: Rugged::SORT_TOPO,
- date: Rugged::SORT_DATE | Rugged::SORT_TOPO
- }
-
- @rugged_sort_types.fetch(sort_type, Rugged::SORT_NONE)
- end
-
def shas_with_signatures(repository, shas)
Gitlab::GitalyClient::CommitService.new(repository).filter_shas_with_signatures(shas)
end
@@ -223,8 +206,6 @@ module Gitlab
case raw_commit
when Hash
init_from_hash(raw_commit)
- when Rugged::Commit
- init_from_rugged(raw_commit)
when Gitaly::GitCommit
init_from_gitaly(raw_commit)
else
@@ -265,23 +246,6 @@ module Gitlab
@repository.gitaly_commit_client.diff_from_parent(self, options)
end
- # Not to be called directly, but right now its used for tests and in old
- # migrations
- def rugged_diff_from_parent(options = {})
- options ||= {}
- break_rewrites = options[:break_rewrites]
- actual_options = Gitlab::Git::Diff.filter_diff_options(options)
-
- diff = if rugged_commit.parents.empty?
- rugged_commit.diff(actual_options.merge(reverse: true))
- else
- rugged_commit.parents[0].diff(rugged_commit, actual_options)
- end
-
- diff.find_similar!(break_rewrites: break_rewrites)
- diff
- end
-
def deltas
@deltas ||= begin
deltas = @repository.gitaly_commit_client.commit_deltas(self)
@@ -352,14 +316,6 @@ module Gitlab
encode! @committer_email
end
- def rugged_commit
- @rugged_commit ||= if raw_commit.is_a?(Rugged::Commit)
- raw_commit
- else
- @repository.rev_parse_target(id)
- end
- end
-
def merge_commit?
parent_ids.size > 1
end
@@ -405,22 +361,6 @@ module Gitlab
end
end
- def init_from_rugged(commit)
- author = commit.author
- committer = commit.committer
-
- @raw_commit = commit
- @id = commit.oid
- @message = commit.message
- @authored_date = author[:time]
- @committed_date = committer[:time]
- @author_name = author[:name]
- @author_email = author[:email]
- @committer_name = committer[:name]
- @committer_email = committer[:email]
- @parent_ids = commit.parents.map(&:oid)
- end
-
def init_from_gitaly(commit)
@raw_commit = commit
@id = commit.id
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 2cbd9c218d4..39fbf6e2526 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -252,14 +252,6 @@ module Gitlab
end
end
- def batch_existence(object_ids, existing: true)
- filter_method = existing ? :select : :reject
-
- object_ids.public_send(filter_method) do |oid| # rubocop:disable GitlabSecurity/PublicSend
- rugged.exists?(oid)
- end
- end
-
# Returns an Array of branch and tag names
def ref_names
branch_names + tag_names
@@ -389,6 +381,21 @@ module Gitlab
end
end
+ # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/1233
+ def new_commits(newrev)
+ gitaly_migrate(:new_commits) do |is_enabled|
+ if is_enabled
+ gitaly_ref_client.list_new_commits(newrev)
+ else
+ refs = Gitlab::GitalyClient::StorageSettings.allow_disk_access do
+ rev_list(including: newrev, excluding: :all).split("\n").map(&:strip)
+ end
+
+ Gitlab::Git::Commit.batch_by_oid(self, refs)
+ end
+ end
+ end
+
def count_commits(options)
options = process_count_commits_options(options.dup)
@@ -409,13 +416,6 @@ module Gitlab
end
end
- # Return the object that +revspec+ points to. If +revspec+ is an
- # annotated tag, then return the tag's target instead.
- def rev_parse_target(revspec)
- obj = rugged.rev_parse(revspec)
- Ref.dereference_object(obj)
- end
-
# Counts the amount of commits between `from` and `to`.
def count_commits_between(from, to, options = {})
count_commits(from: from, to: to, **options)
@@ -1132,33 +1132,6 @@ module Gitlab
run_git!(args, lazy_block: block)
end
- def with_worktree(worktree_path, branch, sparse_checkout_files: nil, env:)
- base_args = %w(worktree add --detach)
-
- # Note that we _don't_ want to test for `.present?` here: If the caller
- # passes an non nil empty value it means it still wants sparse checkout
- # but just isn't interested in any file, perhaps because it wants to
- # checkout files in by a changeset but that changeset only adds files.
- if sparse_checkout_files
- # Create worktree without checking out
- run_git!(base_args + ['--no-checkout', worktree_path], env: env)
- worktree_git_path = run_git!(%w(rev-parse --git-dir), chdir: worktree_path).chomp
-
- configure_sparse_checkout(worktree_git_path, sparse_checkout_files)
-
- # After sparse checkout configuration, checkout `branch` in worktree
- run_git!(%W(checkout --detach #{branch}), chdir: worktree_path, env: env)
- else
- # Create worktree and checkout `branch` in it
- run_git!(base_args + [worktree_path, branch], env: env)
- end
-
- yield
- ensure
- FileUtils.rm_rf(worktree_path) if File.exist?(worktree_path)
- FileUtils.rm_rf(worktree_git_path) if worktree_git_path && File.exist?(worktree_git_path)
- end
-
def checksum
# The exists? RPC is much cheaper, so we perform this request first
raise NoRepository, "Repository does not exists" unless exists?
@@ -1204,38 +1177,6 @@ module Gitlab
end
end
- # Adding a worktree means checking out the repository. For large repos,
- # this can be very expensive, so set up sparse checkout for the worktree
- # to only check out the files we're interested in.
- def configure_sparse_checkout(worktree_git_path, files)
- run_git!(%w(config core.sparseCheckout true))
-
- return if files.empty?
-
- worktree_info_path = File.join(worktree_git_path, 'info')
- FileUtils.mkdir_p(worktree_info_path)
- File.write(File.join(worktree_info_path, 'sparse-checkout'), files)
- end
-
- def rugged_fetch_source_branch(source_repository, source_branch, local_ref)
- with_repo_branch_commit(source_repository, source_branch) do |commit|
- if commit
- write_ref(local_ref, commit.sha)
- true
- else
- false
- end
- end
- end
-
- def worktree_path(prefix, id)
- id = id.to_s
- raise ArgumentError, "worktree id can't be empty" unless id.present?
- raise ArgumentError, "worktree id can't contain slashes " if id.include?("/")
-
- File.join(path, 'gitlab-worktree', "#{prefix}-#{id}")
- end
-
def git_env_for_user(user)
{
'GIT_COMMITTER_NAME' => user.name,
@@ -1296,17 +1237,6 @@ module Gitlab
Gitlab::Git::HookEnv.all(gl_repository).values_at(*ALLOWED_OBJECT_RELATIVE_DIRECTORIES_VARIABLES).flatten.compact
end
- # Return the Rugged patches for the diff between +from+ and +to+.
- def diff_patches(from, to, options = {}, *paths)
- options ||= {}
- break_rewrites = options[:break_rewrites]
- actual_options = Gitlab::Git::Diff.filter_diff_options(options.merge(paths: paths))
-
- diff = rugged.diff(from, to, actual_options)
- diff.find_similar!(break_rewrites: break_rewrites)
- diff.each_patch
- end
-
def sort_branches(branches, sort_by)
case sort_by
when 'name'
@@ -1394,10 +1324,6 @@ module Gitlab
def rev_list_param(spec)
spec == :all ? ['--all'] : spec
end
-
- def sha_from_ref(ref)
- rev_parse_target(ref).oid
- end
end
end
end
diff --git a/lib/gitlab/git/repository_mirroring.rb b/lib/gitlab/git/repository_mirroring.rb
index e35ea5762eb..9faa62be28e 100644
--- a/lib/gitlab/git/repository_mirroring.rb
+++ b/lib/gitlab/git/repository_mirroring.rb
@@ -50,7 +50,7 @@ module Gitlab
name = ref.name.sub(%r{\Arefs/remotes/#{remote_name}/}, '')
begin
- target_commit = Gitlab::Git::Commit.find(self, ref.target)
+ target_commit = Gitlab::Git::Commit.find(self, ref.target.oid)
branches << Gitlab::Git::Branch.new(self, name, ref.target, target_commit)
rescue Rugged::ReferenceError
# Omit invalid branch
diff --git a/lib/gitlab/git/rev_list.rb b/lib/gitlab/git/rev_list.rb
deleted file mode 100644
index 2ba68343aa5..00000000000
--- a/lib/gitlab/git/rev_list.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-module Gitlab
- module Git
- class RevList
- include Gitlab::Git::Popen
-
- attr_reader :oldrev, :newrev, :repository
-
- def initialize(repository, newrev:, oldrev: nil)
- @oldrev = oldrev
- @newrev = newrev
- @repository = repository
- end
-
- # This method returns an array of new commit references
- # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/1233
- #
- def new_refs
- Gitlab::GitalyClient::StorageSettings.allow_disk_access do
- repository.rev_list(including: newrev, excluding: :all).split("\n")
- end
- end
-
- private
-
- def execute(args)
- repository.rev_list(args).split("\n")
- end
-
- def get_objects(including: [], excluding: [], options: [], require_path: nil)
- opts = { including: including, excluding: excluding, options: options, objects: true }
-
- repository.rev_list(opts) do |lazy_output|
- objects = objects_from_output(lazy_output, require_path: require_path)
-
- yield(objects)
- end
- end
-
- def objects_from_output(object_output, require_path: nil)
- object_output.map do |output_line|
- sha, path = output_line.split(' ', 2)
-
- next if require_path && path.to_s.empty?
-
- sha
- end.reject(&:nil?)
- end
- end
- end
-end
diff --git a/lib/gitlab/gitaly_client/ref_service.rb b/lib/gitlab/gitaly_client/ref_service.rb
index 7f4eed9222a..1ad6376dac7 100644
--- a/lib/gitlab/gitaly_client/ref_service.rb
+++ b/lib/gitlab/gitaly_client/ref_service.rb
@@ -56,6 +56,25 @@ module Gitlab
encode!(response.name.dup)
end
+ def list_new_commits(newrev)
+ request = Gitaly::ListNewCommitsRequest.new(
+ repository: @gitaly_repo,
+ commit_id: newrev
+ )
+
+ response = GitalyClient
+ .call(@storage, :ref_service, :list_new_commits, request, timeout: GitalyClient.medium_timeout)
+
+ commits = []
+ response.each do |msg|
+ msg.commits.each do |c|
+ commits << Gitlab::Git::Commit.new(@repository, c)
+ end
+ end
+
+ commits
+ end
+
def count_tag_names
tag_names.count
end
diff --git a/lib/gitlab/import_export/file_importer.rb b/lib/gitlab/import_export/file_importer.rb
index 0f4c3498036..4c411f4847e 100644
--- a/lib/gitlab/import_export/file_importer.rb
+++ b/lib/gitlab/import_export/file_importer.rb
@@ -4,6 +4,7 @@ module Gitlab
include Gitlab::ImportExport::CommandLineUtil
MAX_RETRIES = 8
+ IGNORED_FILENAMES = %w(. ..).freeze
def self.import(*args)
new(*args).import
@@ -59,7 +60,7 @@ module Gitlab
end
def extracted_files
- Dir.glob("#{@shared.export_path}/**/*", File::FNM_DOTMATCH).reject { |f| f =~ %r{.*/\.{1,2}$} }
+ Dir.glob("#{@shared.export_path}/**/*", File::FNM_DOTMATCH).reject { |f| IGNORED_FILENAMES.include?(File.basename(f)) }
end
end
end
diff --git a/lib/uploaded_file.rb b/lib/uploaded_file.rb
index 4b9cb59eab5..53e5ac02e42 100644
--- a/lib/uploaded_file.rb
+++ b/lib/uploaded_file.rb
@@ -21,7 +21,7 @@ class UploadedFile
raise InvalidPathError, "#{path} file does not exist" unless ::File.exist?(path)
@content_type = content_type
- @original_filename = filename || ::File.basename(path)
+ @original_filename = sanitize_filename(filename || path)
@content_type = content_type
@sha256 = sha256
@remote_id = remote_id
@@ -55,6 +55,16 @@ class UploadedFile
end
end
+ # copy-pasted from CarrierWave::SanitizedFile
+ def sanitize_filename(name)
+ name = name.tr("\\", "/") # work-around for IE
+ name = ::File.basename(name)
+ name = name.gsub(CarrierWave::SanitizedFile.sanitize_regexp, "_")
+ name = "_#{name}" if name =~ /\A\.+\z/
+ name = "unnamed" if name.empty?
+ name.mb_chars.to_s
+ end
+
def path
@tempfile.path
end