summaryrefslogtreecommitdiff
path: root/app/services/files/base_service.rb
blob: 1055f5ff0882e556339d638b71df4f4962911afc (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
# frozen_string_literal: true

module Files
  class BaseService < Commits::CreateService
    FileChangedError = Class.new(StandardError)

    def initialize(*args)
      super

      git_user = Gitlab::Git::User.from_gitlab(current_user) if current_user.present?

      @author_email = params[:author_email] || git_user&.email
      @author_name = params[:author_name] || git_user&.name
      @commit_message = params[:commit_message]
      @last_commit_sha = params[:last_commit_sha]

      @file_path = params[:file_path]
      @previous_path = params[:previous_path]

      @file_content = params[:file_content]
      @file_content = Base64.decode64(@file_content) if params[:file_content_encoding] == 'base64'

      @execute_filemode = params[:execute_filemode]
    end

    def file_has_changed?(path, commit_id)
      return false unless commit_id

      last_commit = Gitlab::Git::Commit
        .last_for_path(@start_project.repository, @start_branch, path, literal_pathspec: true)

      return false unless last_commit

      last_commit.sha != commit_id
    end
  end
end