summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/rev_list.rb
blob: 79dd0cf7df28f83b89b887bc57fafda3d5345377 (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
module Gitlab
  module Git
    class RevList
      attr_reader :project, :env

      ALLOWED_VARIABLES = %w[GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES].freeze

      def initialize(oldrev, newrev, project:, env: nil)
        @project = project
        @env = env.presence || {}
        @args = [Gitlab.config.git.bin_path,
                 "--git-dir=#{project.repository.path_to_repo}",
                 "rev-list",
                 "--max-count=1",
                 oldrev,
                 "^#{newrev}"]
      end

      def execute
        Gitlab::Popen.popen(@args, nil, parse_environment_variables)
      end

      def valid?
        environment_variables.all? do |(name, value)|
          value.to_s.start_with?(project.repository.path_to_repo)
        end
      end

      private

      def parse_environment_variables
        return {} unless valid?

        environment_variables
      end

      def environment_variables
        @environment_variables ||= env.slice(*ALLOWED_VARIABLES).compact
      end
    end
  end
end