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

#commit(ref = 'HEAD') ⇒ Object


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

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

#commit_by(oid:) ⇒ Object


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

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

#commits_by(oids:) ⇒ Object


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

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

#default_branchObject


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

def default_branch
  @default_branch ||= repository.root_ref || default_branch_from_preferences
end

#default_branch_from_preferencesObject


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

def default_branch_from_preferences
  return unless empty_repo?

  group_branch_default_name = group&.default_branch_name if respond_to?(:group)

  group_branch_default_name || Gitlab::CurrentSettings.default_branch_name
end

#empty_repo?Boolean

Returns:

  • (Boolean)

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

def empty_repo?
  repository.empty?
end

#full_pathObject

Raises:

  • (NotImplementedError)

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

def full_path
  raise NotImplementedError
end

#http_url_to_repoObject


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

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

#lfs_enabled?Boolean

Returns:

  • (Boolean)

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

def lfs_enabled?
  false
end

#lfs_http_url_to_repo(_operation = nil) ⇒ Object

Is overridden in EE::Project for Geo support


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

def lfs_http_url_to_repo(_operation = nil)
  http_url_to_repo
end

#reload_default_branchObject


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

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

  default_branch
end

#repo_exists?Boolean

Returns:

  • (Boolean)

34
35
36
37
38
39
40
# File 'app/models/concerns/has_repository.rb', line 34

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

#repositoryObject

Raises:

  • (NotImplementedError)

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

def repository
  raise NotImplementedError
end

#repository_exists?Boolean

Returns:

  • (Boolean)

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

def repository_exists?
  !!repository.exists?
end

#repository_size_checkerObject

Raises:

  • (NotImplementedError)

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

def repository_size_checker
  raise NotImplementedError
end

#root_ref?(branch) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#ssh_url_to_repoObject


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

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

#storageObject

Raises:

  • (NotImplementedError)

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

def storage
  raise NotImplementedError
end

#url_to_repoObject


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

def url_to_repo
  ssh_url_to_repo
end

#valid_repo?Boolean

Returns:

  • (Boolean)

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

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

#web_url(only_path: nil) ⇒ Object


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

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