summaryrefslogtreecommitdiff
path: root/lib/system_check/orphans/repository_check.rb
blob: 1de6bec165b5c30a628ee00d11d548392941756e (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
module SystemCheck
  module Orphans
    class RepositoryCheck < SystemCheck::BaseCheck
      set_name 'Orphaned repositories:'

      def multi_check
        Gitlab.config.repositories.storages.each do |name, repository_storage|
          $stdout.puts
          $stdout.puts "* Storage: #{name} (#{repository_storage['path']})".color(:yellow)

          repositories = toplevel_namespace_dirs(repository_storage['path']).map do |path|
            namespace = File.basename(path)
            Dir.glob(File.join(path, '*')).map {|repo| "#{namespace}/#{File.basename(repo)}"}
          end.try(:flatten!)

          orphans = (repositories - list_repositories(name))
          if orphans.empty?
            $stdout.puts "* No orphaned repositories for #{name} storage".color(:green)
            next
          end

          orphans.each do |orphan|
            $stdout.puts " - #{orphan}".color(:red)
          end
        end
      end

      private

      def list_repositories(storage_name)
        sql = "
          SELECT
            CONCAT(n.path, '/', p.path, '.git') repo,
            CONCAT(n.path, '/', p.path, '.wiki.git') wiki
          FROM projects p
            JOIN namespaces n
              ON (p.namespace_id = n.id)
          WHERE (p.repository_storage LIKE ?)
        "

        query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, storage_name])
        ActiveRecord::Base.connection.select_all(query).rows.try(:flatten!)
      end

      def toplevel_namespace_dirs(storage_path)
        Dir.glob(File.join(storage_path, '*'))
      end
    end
  end
end