summaryrefslogtreecommitdiff
path: root/lib/gitlab/bare_repository_import/repository.rb
blob: c0c666dfb7b1be0d5d2aeac537f8f6abd61078fb (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Gitlab
  module BareRepositoryImport
    class Repository
      include ::Gitlab::Utils::StrongMemoize

      attr_reader :group_path, :project_name, :repo_path

      def initialize(root_path, repo_path)
        @root_path = root_path
        @repo_path = repo_path
        @root_path << '/' unless root_path.ends_with?('/')

        full_path =
          if hashed? && !wiki?
            repository.config.get('gitlab.fullpath')
          else
            repo_relative_path
          end

        # Split path into 'all/the/namespaces' and 'project_name'
        @group_path, _, @project_name = full_path.to_s.rpartition('/')
      end

      def wiki_exists?
        File.exist?(wiki_path)
      end

      def wiki_path
        @wiki_path ||= repo_path.sub(/\.git$/, '.wiki.git')
      end

      def project_full_path
        @project_full_path ||= "#{group_path}/#{project_name}"
      end

      def processable?
        return false if wiki?
        return false if hashed? && (group_path.blank? || project_name.blank?)

        true
      end

      private

      def wiki?
        strong_memoize(:wiki) do
          repo_path.end_with?('.wiki.git')
        end
      end

      def hashed?
        strong_memoize(:hashed) do
          repo_relative_path.include?('@hashed')
        end
      end

      def repo_relative_path
        # Remove root path and `.git` at the end
        repo_path[@root_path.size...-4]
      end

      def repository
        @repository ||= Rugged::Repository.new(repo_path)
      end
    end
  end
end