summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/rev_list.rb
blob: 5fdad077eeafde19f9ed18d48e9b4be0ba285f4d (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
module Gitlab
  module Git
    class RevList
      include Gitlab::Git::Popen

      attr_reader :oldrev, :newrev, :repository

      def initialize(repository, newrev:, oldrev: nil)
        @oldrev = oldrev
        @newrev = newrev
        @repository = repository
      end

      # This method returns an array of new commit references
      def new_refs
        repository.rev_list(including: newrev, excluding: :all).split("\n")
      end

      # Finds newly added objects
      # Returns an array of shas
      #
      # Can skip objects which do not have a path using required_path: true
      # This skips commit objects and root trees, which might not be needed when
      # looking for blobs
      #
      # When given a block it will yield objects as a lazy enumerator so
      # the caller can limit work done instead of processing megabytes of data
      def new_objects(options: [], require_path: nil, not_in: nil, &lazy_block)
        opts = {
          including: newrev,
          options: options,
          excluding: not_in.nil? ? :all : not_in,
          require_path: require_path
        }

        get_objects(opts, &lazy_block)
      end

      def all_objects(options: [], require_path: nil, &lazy_block)
        get_objects(including: :all,
                    options: options,
                    require_path: require_path,
                    &lazy_block)
      end

      private

      def execute(args)
        repository.rev_list(args).split("\n")
      end

      def get_objects(including: [], excluding: [], options: [], require_path: nil)
        opts = { including: including, excluding: excluding, options: options, objects: true }

        repository.rev_list(opts) do |lazy_output|
          objects = objects_from_output(lazy_output, require_path: require_path)

          yield(objects)
        end
      end

      def objects_from_output(object_output, require_path: nil)
        object_output.map do |output_line|
          sha, path = output_line.split(' ', 2)

          next if require_path && path.to_s.empty?

          sha
        end.reject(&:nil?)
      end
    end
  end
end