summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/rugged_impl/commit.rb
blob: 971a33b2e990131872657245f9a5ad3ae786d77c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

# NOTE: This code is legacy. Do not add/modify code here unless you have
# discussed with the Gitaly team.  See
# https://docs.gitlab.com/ee/development/gitaly.html#legacy-rugged-code
# for more details.

# rubocop:disable Gitlab/ModuleWithInstanceVariables
module Gitlab
  module Git
    module RuggedImpl
      module Commit
        module ClassMethods
          extend ::Gitlab::Utils::Override
          include Gitlab::Git::RuggedImpl::UseRugged

          def rugged_find(repo, commit_id)
            obj = repo.rev_parse_target(commit_id)

            obj.is_a?(::Rugged::Commit) ? obj : nil
          rescue ::Rugged::Error
            nil
          end

          # This needs to return an array of Gitlab::Git:Commit objects
          # instead of Rugged::Commit objects to ensure upstream models
          # operate on a consistent interface. Unlike
          # Gitlab::Git::Commit.find, Gitlab::Git::Commit.batch_by_oid
          # doesn't attempt to decorate the result.
          def rugged_batch_by_oid(repo, oids)
            oids.map { |oid| rugged_find(repo, oid) }
              .compact
              .map { |commit| decorate(repo, commit) }
          end

          override :find_commit
          def find_commit(repo, commit_id)
            if use_rugged?(repo, :rugged_find_commit)
              rugged_find(repo, commit_id)
            else
              super
            end
          end

          override :batch_by_oid
          def batch_by_oid(repo, oids)
            if use_rugged?(repo, :rugged_list_commits_by_oid)
              rugged_batch_by_oid(repo, oids)
            else
              super
            end
          end
        end

        extend ::Gitlab::Utils::Override
        include Gitlab::Git::RuggedImpl::UseRugged

        override :init_commit
        def init_commit(raw_commit)
          case raw_commit
          when ::Rugged::Commit
            init_from_rugged(raw_commit)
          else
            super
          end
        end

        override :commit_tree_entry
        def commit_tree_entry(path)
          if use_rugged?(@repository, :rugged_commit_tree_entry)
            rugged_tree_entry(path)
          else
            super
          end
        end

        # Is this the same as Blob.find_entry_by_path ?
        def rugged_tree_entry(path)
          rugged_commit.tree.path(path)
        rescue Rugged::TreeError
          nil
        end

        def rugged_commit
          @rugged_commit ||= if raw_commit.is_a?(Rugged::Commit)
                               raw_commit
                             else
                               @repository.rev_parse_target(id)
                             end
        end

        def init_from_rugged(commit)
          author = commit.author
          committer = commit.committer

          @raw_commit = commit
          @id = commit.oid
          @message = commit.message
          @authored_date = author[:time]
          @committed_date = committer[:time]
          @author_name = author[:name]
          @author_email = author[:email]
          @committer_name = committer[:name]
          @committer_email = committer[:email]
          @parent_ids = commit.parents.map(&:oid)
        end
      end
    end
  end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables