summaryrefslogtreecommitdiff
path: root/lib/gitlab/project_transfer.rb
blob: f5a8aeff73ccec48957b94abbfcf9b43211cc2f2 (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
# frozen_string_literal: true

module Gitlab
  # This class is used to move local, unhashed files owned by projects to their new location
  class ProjectTransfer
    # nil parent_path (or parent_path_was) represents a root namespace
    def move_namespace(path, parent_path_was, parent_path)
      parent_path_was ||= ''
      parent_path ||= ''
      new_parent_folder = File.join(root_dir, parent_path)
      FileUtils.mkdir_p(new_parent_folder)
      from = File.join(root_dir, parent_path_was, path)
      to = File.join(root_dir, parent_path, path)
      move(from, to, "")
    end

    alias_method :move_project, :move_namespace

    def rename_project(path_was, path, namespace_path)
      base_dir = File.join(root_dir, namespace_path)
      move(path_was, path, base_dir)
    end

    def rename_namespace(path_was, path)
      move(path_was, path)
    end

    def root_dir
      raise NotImplementedError
    end

    private

    def move(path_was, path, base_dir = nil)
      base_dir ||= root_dir
      from = File.join(base_dir, path_was)
      to = File.join(base_dir, path)
      FileUtils.mv(from, to)
    rescue Errno::ENOENT
      false
    end
  end
end