summaryrefslogtreecommitdiff
path: root/app/services/repositories/base_service.rb
blob: a99a65b7edb67b44c7a6cb56cc4e10f3b2ea2a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

class Repositories::BaseService < BaseService
  include Gitlab::ShellAdapter

  DELETED_FLAG = '+deleted'

  attr_reader :repository

  delegate :container, :disk_path, :full_path, to: :repository
  delegate :repository_storage, to: :container

  def initialize(repository)
    @repository = repository
  end

  def repo_exists?(path)
    gitlab_shell.repository_exists?(repository_storage, path + '.git')
  end

  def mv_repository(from_path, to_path)
    return true unless repo_exists?(from_path)

    gitlab_shell.mv_repository(repository_storage, from_path, to_path)
  end

  # Build a path for removing repositories
  # We use `+` because its not allowed by GitLab so user can not create
  # project with name cookies+119+deleted and capture someone stalled repository
  #
  # gitlab/cookies.git -> gitlab/cookies+119+deleted.git
  #
  def removal_path
    "#{disk_path}+#{container.id}#{DELETED_FLAG}"
  end

  # If we get a Gitaly error, the repository may be corrupted. We can
  # ignore these errors since we're going to trash the repositories
  # anyway.
  def ignore_git_errors(&block)
    yield
  rescue Gitlab::Git::CommandError => e
    Gitlab::GitLogger.warn(class: self.class.name, container_id: container.id, disk_path: disk_path, message: e.to_s)
  end

  def move_error(path)
    error = %Q{Repository "#{path}" could not be moved}

    log_error(error)
    error(error)
  end
end