Module: HasRepository

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::ShellAdapter, Gitlab::Utils::StrongMemoize, Referable
Included in:
Project, Snippet, Wiki
Defined in:
app/models/concerns/has_repository.rb

Overview

This concern is created to handle repository actions. It should be include inside any object capable of directly having a repository, like project or snippet.

It also includes `Referable`, therefore the method `to_reference` should be overridden in case the object needs any special behavior.

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Methods included from Referable

#referable_inspect, #reference_link_text, #to_reference, #to_reference_base

Instance Method Details

#after_change_head_branch_does_not_exist(branch) ⇒ Object


124
125
126
# File 'app/models/concerns/has_repository.rb', line 124

def after_change_head_branch_does_not_exist(branch)
  # No-op (by default)
end

#after_repository_change_headObject


120
121
122
# File 'app/models/concerns/has_repository.rb', line 120

def after_repository_change_head
  reload_default_branch
end

#commit(ref = 'HEAD') ⇒ Object


42
43
44
# File 'app/models/concerns/has_repository.rb', line 42

def commit(ref = 'HEAD')
  repository.commit(ref)
end

#commit_by(oid:) ⇒ Object


46
47
48
# File 'app/models/concerns/has_repository.rb', line 46

def commit_by(oid:)
  repository.commit_by(oid: oid)
end

#commits_by(oids:) ⇒ Object


50
51
52
# File 'app/models/concerns/has_repository.rb', line 50

def commits_by(oids:)
  repository.commits_by(oids: oids)
end

#default_branchObject


74
75
76
# File 'app/models/concerns/has_repository.rb', line 74

def default_branch
  @default_branch ||= repository.empty? ? default_branch_from_preferences : repository.root_ref
end

#default_branch_from_group_preferencesObject


82
83
84
85
86
87
# File 'app/models/concerns/has_repository.rb', line 82

def default_branch_from_group_preferences
  return unless respond_to?(:group)
  return unless group

  group.default_branch_name || group.root_ancestor.default_branch_name
end

#default_branch_from_preferencesObject


78
79
80
# File 'app/models/concerns/has_repository.rb', line 78

def default_branch_from_preferences
  (default_branch_from_group_preferences || Gitlab::CurrentSettings.default_branch_name).presence
end

#empty_repo?Boolean

Returns:

  • (Boolean)

70
71
72
# File 'app/models/concerns/has_repository.rb', line 70

def empty_repo?
  repository.empty?
end

#full_pathObject

Raises:

  • (NotImplementedError)

62
63
64
# File 'app/models/concerns/has_repository.rb', line 62

def full_path
  raise NotImplementedError
end

#http_url_to_repoObject


103
104
105
# File 'app/models/concerns/has_repository.rb', line 103

def http_url_to_repo
  Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :http)
end

#lfs_enabled?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'app/models/concerns/has_repository.rb', line 66

def lfs_enabled?
  false
end

#lfs_http_url_to_repo(_operation = nil) ⇒ Object

Is overridden in EE::Project for Geo support


108
109
110
# File 'app/models/concerns/has_repository.rb', line 108

def lfs_http_url_to_repo(_operation = nil)
  http_url_to_repo
end

#reload_default_branchObject


89
90
91
92
93
# File 'app/models/concerns/has_repository.rb', line 89

def reload_default_branch
  @default_branch = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables

  default_branch
end

#repo_exists?Boolean

Returns:

  • (Boolean)

26
27
28
29
30
31
32
# File 'app/models/concerns/has_repository.rb', line 26

def repo_exists?
  strong_memoize(:repo_exists) do
    repository.exists?
  rescue StandardError
    false
  end
end

#repositoryObject

Raises:

  • (NotImplementedError)

54
55
56
# File 'app/models/concerns/has_repository.rb', line 54

def repository
  raise NotImplementedError
end

#repository_exists?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'app/models/concerns/has_repository.rb', line 34

def repository_exists?
  !!repository.exists?
end

#repository_size_checkerObject

Raises:

  • (NotImplementedError)

116
117
118
# File 'app/models/concerns/has_repository.rb', line 116

def repository_size_checker
  raise NotImplementedError
end

#root_ref?(branch) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'app/models/concerns/has_repository.rb', line 38

def root_ref?(branch)
  repository.root_ref == branch
end

#ssh_url_to_repoObject


99
100
101
# File 'app/models/concerns/has_repository.rb', line 99

def ssh_url_to_repo
  Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :ssh)
end

#storageObject

Raises:

  • (NotImplementedError)

58
59
60
# File 'app/models/concerns/has_repository.rb', line 58

def storage
  raise NotImplementedError
end

#url_to_repoObject


95
96
97
# File 'app/models/concerns/has_repository.rb', line 95

def url_to_repo
  ssh_url_to_repo
end

#valid_repo?Boolean

Returns:

  • (Boolean)

19
20
21
22
23
24
# File 'app/models/concerns/has_repository.rb', line 19

def valid_repo?
  repository.exists?
rescue StandardError
  errors.add(:base, _('Invalid repository path'))
  false
end

#web_url(only_path: nil) ⇒ Object


112
113
114
# File 'app/models/concerns/has_repository.rb', line 112

def web_url(only_path: nil)
  Gitlab::UrlBuilder.build(self, only_path: only_path)
end