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

module Gitlab
  module Git
    class Push
      include Gitlab::Utils::StrongMemoize

      attr_reader :ref, :oldrev, :newrev

      def initialize(project, oldrev, newrev, ref)
        @project = project
        @oldrev = oldrev.presence || Gitlab::Git::BLANK_SHA
        @newrev = newrev.presence || Gitlab::Git::BLANK_SHA
        @ref = ref
      end

      def branch_name
        strong_memoize(:branch_name) do
          Gitlab::Git.branch_name(@ref)
        end
      end

      def branch_added?
        Gitlab::Git.blank_ref?(@oldrev)
      end

      def branch_removed?
        Gitlab::Git.blank_ref?(@newrev)
      end

      def branch_updated?
        branch_push? && !branch_added? && !branch_removed?
      end

      def force_push?
        Gitlab::Checks::ForcePush.force_push?(@project, @oldrev, @newrev)
      end

      def branch_push?
        strong_memoize(:branch_push) do
          Gitlab::Git.branch_ref?(@ref)
        end
      end

      def modified_paths
        unless branch_updated?
          raise ArgumentError, 'Unable to calculate modified paths!'
        end

        strong_memoize(:modified_paths) do
          @project.repository.diff_stats(@oldrev, @newrev).paths
        end
      end
    end
  end
end