rev_list.rb 2.2 KB
Newer Older
1 2
# Gitaly note: JV: will probably be migrated indirectly by migrating the call sites.

3 4 5
module Gitlab
  module Git
    class RevList
6 7
      include Gitlab::Git::Popen

8
      attr_reader :oldrev, :newrev, :repository
9

10
      def initialize(repository, newrev:, oldrev: nil)
11 12
        @oldrev = oldrev
        @newrev = newrev
13
        @repository = repository
14 15
      end

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

21 22 23 24 25 26 27
      # 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
      #
28 29 30
      # 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(require_path: nil, not_in: nil, &lazy_block)
31 32 33 34 35
        opts = {
          including: newrev,
          excluding: not_in.nil? ? :all : not_in,
          require_path: require_path
        }
36

37
        get_objects(opts, &lazy_block)
38 39
      end

40
      def all_objects(require_path: nil, &lazy_block)
41
        get_objects(including: :all, require_path: require_path, &lazy_block)
42 43
      end

44
      # This methods returns an array of missed references
45 46
      #
      # Should become obsolete after https://gitlab.com/gitlab-org/gitaly/issues/348.
47
      def missed_ref
48
        repository.missed_ref(oldrev, newrev).split("\n")
49 50 51 52
      end

      private

53
      def execute(args)
54
        repository.rev_list(args).split("\n")
55
      end
56

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

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

63
          yield(objects)
64 65 66 67 68
        end
      end

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

71
          next if require_path && path.to_s.empty?
72 73 74 75

          sha
        end.reject(&:nil?)
      end
76 77 78
    end
  end
end