delete_file_action.rb 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
require_relative 'file_action'

module Gitlab
  module Satellite
    class DeleteFileAction < FileAction
      # Deletes file and creates a new commit for it
      #
      # Returns false if committing the change fails
      # Returns false if pushing from the satellite to bare repo failed or was rejected
      # Returns true otherwise
      def commit!(content, commit_message)
        in_locked_and_timed_satellite do |repo|
          prepare_satellite!(repo)

          # create target branch in satellite at the corresponding commit from bare repo
16
          repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}")
17 18 19

          # update the file in the satellite's working dir
          file_path_in_satellite = File.join(repo.working_dir, file_path)
20 21 22 23 24 25 26

          # Prevent relative links
          unless safe_path?(file_path_in_satellite)
            Gitlab::GitLogger.error("FileAction: Relative path not allowed")
            return false
          end

27 28 29
          File.delete(file_path_in_satellite)

          # add removed file
30
          repo.remove(file_path_in_satellite)
31 32 33 34 35 36 37 38

          # commit the changes
          # will raise CommandFailed when commit fails
          repo.git.commit(raise: true, timeout: true, a: true, m: commit_message)


          # push commit back to bare repo
          # will raise CommandFailed when push fails
39
          repo.git.push({ raise: true, timeout: true }, :origin, ref)
40 41 42 43 44 45 46 47 48 49 50

          # everything worked
          true
        end
      rescue Grit::Git::CommandFailed => ex
        Gitlab::GitLogger.error(ex.message)
        false
      end
    end
  end
end