summaryrefslogtreecommitdiff
path: root/app/services/files/multi_service.rb
blob: 65af4dd5a281117a67c3b497bf30f2f1d8c6524c (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
68
69
70
71
72
73
# frozen_string_literal: true

module Files
  class MultiService < Files::BaseService
    UPDATE_FILE_ACTIONS = %w(update move delete chmod).freeze

    def create_commit!
      transformer = Lfs::FileTransformer.new(project, repository, @branch_name)

      actions = actions_after_lfs_transformation(transformer, params[:actions])
      actions = transform_move_actions(actions)

      commit_actions!(actions)
    end

    private

    def actions_after_lfs_transformation(transformer, actions)
      actions.map do |action|
        if action[:action] == 'create'
          result = transformer.new_file(action[:file_path], action[:content], encoding: action[:encoding])
          action[:content] = result.content
          action[:encoding] = result.encoding
        end

        action
      end
    end

    # When moving a file, `content: nil` means "use the contents of the previous
    # file", while `content: ''` means "move the file and set it to empty"
    def transform_move_actions(actions)
      actions.map do |action|
        action[:infer_content] = true if action[:content].nil?

        action
      end
    end

    def commit_actions!(actions)
      repository.multi_action(
        current_user,
        message: @commit_message,
        branch_name: @branch_name,
        actions: actions,
        author_email: @author_email,
        author_name: @author_name,
        start_project: @start_project,
        start_branch_name: @start_branch,
        start_sha: @start_sha,
        force: force?
      )
    rescue ArgumentError => e
      raise_error(e)
    end

    def validate!
      super

      params[:actions].each { |action| validate_file_status!(action) }
    end

    def validate_file_status!(action)
      return unless UPDATE_FILE_ACTIONS.include?(action[:action])

      file_path = action[:previous_path] || action[:file_path]

      if file_has_changed?(file_path, action[:last_commit_id])
        raise_error("The file has changed since you started editing it: #{file_path}")
      end
    end
  end
end