commit.rb 17.8 KB
Newer Older
R
Robert Speicher 已提交
1 2 3 4
# Gitlab::Git::Commit is a wrapper around native Rugged::Commit object
module Gitlab
  module Git
    class Commit
5
      include Gitlab::EncodingHelper
R
Robert Speicher 已提交
6

7
      attr_accessor :raw_commit, :head
R
Robert Speicher 已提交
8

9
      MIN_SHA_LENGTH = 7
R
Robert Speicher 已提交
10 11 12 13 14 15 16 17 18 19 20
      SERIALIZE_KEYS = [
        :id, :message, :parent_ids,
        :authored_date, :author_name, :author_email,
        :committed_date, :committer_name, :committer_email
      ].freeze

      attr_accessor *SERIALIZE_KEYS # rubocop:disable Lint/AmbiguousOperator

      def ==(other)
        return false unless other.is_a?(Gitlab::Git::Commit)

21
        id && id == other.id
R
Robert Speicher 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
      end

      class << self
        # Get commits collection
        #
        # Ex.
        #   Commit.where(
        #     repo: repo,
        #     ref: 'master',
        #     path: 'app/models',
        #     limit: 10,
        #     offset: 5,
        #   )
        #
        def where(options)
          repo = options.delete(:repo)
          raise 'Gitlab::Git::Repository is required' unless repo.respond_to?(:log)

40
          repo.log(options)
R
Robert Speicher 已提交
41 42 43 44 45 46 47 48 49
        end

        # Get single commit
        #
        # Ex.
        #   Commit.find(repo, '29eda46b')
        #
        #   Commit.find(repo, 'master')
        #
50
        # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/321
R
Robert Speicher 已提交
51
        def find(repo, commit_id = "HEAD")
52
          # Already a commit?
53
          return commit_id if commit_id.is_a?(Gitlab::Git::Commit)
54 55 56

          # A rugged reference?
          commit_id = Gitlab::Git::Ref.dereference_object(commit_id)
57
          return decorate(repo, commit_id) if commit_id.is_a?(Rugged::Commit)
R
Robert Speicher 已提交
58

59 60 61 62 63 64 65 66
          # Some weird thing?
          return nil unless commit_id.is_a?(String)

          commit = repo.gitaly_migrate(:find_commit) do |is_enabled|
            if is_enabled
              repo.gitaly_commit_client.find_commit(commit_id)
            else
              obj = repo.rev_parse_target(commit_id)
R
Robert Speicher 已提交
67

68 69 70
              obj.is_a?(Rugged::Commit) ? obj : nil
            end
          end
R
Robert Speicher 已提交
71

72 73
          decorate(repo, commit) if commit
        rescue Rugged::ReferenceError, Rugged::InvalidError, Rugged::ObjectError,
74
               Gitlab::Git::CommandError, Gitlab::Git::Repository::NoRepository,
75
               Rugged::OdbError, Rugged::TreeError, ArgumentError
R
Robert Speicher 已提交
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
          nil
        end

        # Get last commit for HEAD
        #
        # Ex.
        #   Commit.last(repo)
        #
        def last(repo)
          find(repo)
        end

        # Get last commit for specified path and ref
        #
        # Ex.
        #   Commit.last_for_path(repo, '29eda46b', 'app/models')
        #
        #   Commit.last_for_path(repo, 'master', 'Gemfile')
        #
        def last_for_path(repo, ref, path = nil)
          where(
            repo: repo,
            ref: ref,
            path: path,
            limit: 1
          ).first
        end

        # Get commits between two revspecs
        # See also #repository.commits_between
        #
        # Ex.
        #   Commit.between(repo, '29eda46b', 'master')
        #
        def between(repo, base, head)
111
          Gitlab::GitalyClient.migrate(:commits_between) do |is_enabled|
112 113 114
            if is_enabled
              repo.gitaly_commit_client.between(base, head)
            else
115
              repo.rugged_commits_between(base, head).map { |c| decorate(repo, c) }
116 117
            end
          end
R
Robert Speicher 已提交
118 119 120 121
        rescue Rugged::ReferenceError
          []
        end

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        # Returns commits collection
        #
        # Ex.
        #   Commit.find_all(
        #     repo,
        #     ref: 'master',
        #     max_count: 10,
        #     skip: 5,
        #     order: :date
        #   )
        #
        #   +options+ is a Hash of optional arguments to git
        #     :ref is the ref from which to begin (SHA1 or name)
        #     :max_count is the maximum number of commits to fetch
        #     :skip is the number of commits to skip
        #     :order is the commits order and allowed value is :none (default), :date,
        #        :topo, or any combination of them (in an array). Commit ordering types
        #        are documented here:
        #        http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant)
        #
142
        # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/326
R
Robert Speicher 已提交
143
        def find_all(repo, options = {})
144 145 146 147 148 149 150 151 152 153
          Gitlab::GitalyClient.migrate(:find_all_commits) do |is_enabled|
            if is_enabled
              find_all_by_gitaly(repo, options)
            else
              find_all_by_rugged(repo, options)
            end
          end
        end

        def find_all_by_rugged(repo, options = {})
154 155
          actual_options = options.dup

156
          allowed_options = [:ref, :max_count, :skip, :order]
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

          actual_options.keep_if do |key|
            allowed_options.include?(key)
          end

          default_options = { skip: 0 }
          actual_options = default_options.merge(actual_options)

          rugged = repo.rugged
          walker = Rugged::Walker.new(rugged)

          if actual_options[:ref]
            walker.push(rugged.rev_parse_oid(actual_options[:ref]))
          else
            rugged.references.each("refs/heads/*") do |ref|
              walker.push(ref.target_id)
            end
          end

          walker.sorting(rugged_sort_type(actual_options[:order]))

          commits = []
          offset = actual_options[:skip]
          limit = actual_options[:max_count]
          walker.each(offset: offset, limit: limit) do |commit|
182
            commits.push(decorate(repo, commit))
183 184 185 186 187 188 189
          end

          walker.reset

          commits
        rescue Rugged::OdbError
          []
R
Robert Speicher 已提交
190 191
        end

192 193 194 195
        def find_all_by_gitaly(repo, options = {})
          Gitlab::GitalyClient::CommitService.new(repo).find_all_commits(options)
        end

196 197
        def decorate(repository, commit, ref = nil)
          Gitlab::Git::Commit.new(repository, commit, ref)
R
Robert Speicher 已提交
198 199
        end

200 201 202 203 204 205 206 207 208 209 210 211 212
        # Returns the `Rugged` sorting type constant for one or more given
        # sort types. Valid keys are `:none`, `:topo`, and `:date`, or an array
        # containing more than one of them. `:date` uses a combination of date and
        # topological sorting to closer mimic git's native ordering.
        def rugged_sort_type(sort_type)
          @rugged_sort_types ||= {
            none: Rugged::SORT_NONE,
            topo: Rugged::SORT_TOPO,
            date: Rugged::SORT_DATE | Rugged::SORT_TOPO
          }

          @rugged_sort_types.fetch(sort_type, Rugged::SORT_NONE)
        end
213 214

        def shas_with_signatures(repository, shas)
215 216 217 218 219 220 221 222 223 224 225
          GitalyClient.migrate(:filter_shas_with_signatures) do |is_enabled|
            if is_enabled
              Gitlab::GitalyClient::CommitService.new(repository).filter_shas_with_signatures(shas)
            else
              shas.select do |sha|
                begin
                  Rugged::Commit.extract_signature(repository.rugged, sha)
                rescue Rugged::OdbError
                  false
                end
              end
226 227 228
            end
          end
        end
229 230 231 232 233

        # Only to be used when the object ids will not necessarily have a
        # relation to each other. The last 10 commits for a branch for example,
        # should go through .where
        def batch_by_oid(repo, oids)
234 235
          repo.gitaly_migrate(:list_commits_by_oid,
                              status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) do |is_enabled|
236 237 238 239 240 241 242
            if is_enabled
              repo.gitaly_commit_client.list_commits_by_oid(oids)
            else
              oids.map { |oid| find(repo, oid) }.compact
            end
          end
        end
243 244 245 246 247 248 249 250 251 252 253

        def extract_signature(repository, commit_id)
          repository.gitaly_migrate(:extract_commit_signature) do |is_enabled|
            if is_enabled
              repository.gitaly_commit_client.extract_signature(commit_id)
            else
              rugged_extract_signature(repository, commit_id)
            end
          end
        end

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        def extract_signature_lazily(repository, commit_id)
          BatchLoader.for({ repository: repository, commit_id: commit_id }).batch do |items, loader|
            items_by_repo = items.group_by { |i| i[:repository] }

            items_by_repo.each do |repo, items|
              commit_ids = items.map { |i| i[:commit_id] }

              signatures = batch_signature_extraction(repository, commit_ids)

              signatures.each do |commit_sha, signature_data|
                loader.call({ repository: repository, commit_id: commit_sha }, signature_data)
              end
            end
          end
        end

        def batch_signature_extraction(repository, commit_ids)
          repository.gitaly_migrate(:extract_commit_signature_in_batch) do |is_enabled|
            if is_enabled
              gitaly_batch_signature_extraction(repository, commit_ids)
            else
              rugged_batch_signature_extraction(repository, commit_ids)
            end
          end
        end

        def gitaly_batch_signature_extraction(repository, commit_ids)
          repository.gitaly_commit_client.get_commit_signatures(commit_ids)
        end

        def rugged_batch_signature_extraction(repository, commit_ids)
          commit_ids.each_with_object({}) do |commit_id, signatures|
            signature_data = rugged_extract_signature(repository, commit_id)
            next unless signature_data

            signatures[commit_id] = signature_data
          end
        end

293 294 295 296 297 298 299
        def rugged_extract_signature(repository, commit_id)
          begin
            Rugged::Commit.extract_signature(repository.rugged, commit_id)
          rescue Rugged::OdbError
            nil
          end
        end
R
Robert Speicher 已提交
300 301
      end

302
      def initialize(repository, raw_commit, head = nil)
R
Robert Speicher 已提交
303 304
        raise "Nil as raw commit passed" unless raw_commit

305 306
        case raw_commit
        when Hash
R
Robert Speicher 已提交
307
          init_from_hash(raw_commit)
308
        when Rugged::Commit
R
Robert Speicher 已提交
309
          init_from_rugged(raw_commit)
310
        when Gitaly::GitCommit
311
          init_from_gitaly(raw_commit)
R
Robert Speicher 已提交
312 313 314 315
        else
          raise "Invalid raw commit type: #{raw_commit.class}"
        end

316
        @repository = repository
R
Robert Speicher 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        @head = head
      end

      def sha
        id
      end

      def short_id(length = 10)
        id.to_s[0..length]
      end

      def safe_message
        @safe_message ||= message
      end

      def created_at
        committed_date
      end

      # Was this commit committed by a different person than the original author?
      def different_committer?
        author_name != committer_name || author_email != committer_email
      end

      def parent_id
        parent_ids.first
      end

      # Shows the diff between the commit's parent and the commit.
      #
      # Cuts out the header and stats from #to_patch and returns only the diff.
348 349
      #
      # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/324
350
      def to_diff
351
        Gitlab::GitalyClient.migrate(:commit_patch, status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) do |is_enabled|
352 353 354 355 356 357
          if is_enabled
            @repository.gitaly_commit_client.patch(id)
          else
            rugged_diff_from_parent.patch
          end
        end
R
Robert Speicher 已提交
358 359 360 361
      end

      # Returns a diff object for the changes from this commit's first parent.
      # If there is no parent, then the diff is between this commit and an
362
      # empty repo. See Repository#diff for keys allowed in the +options+
R
Robert Speicher 已提交
363 364
      # hash.
      def diff_from_parent(options = {})
365 366 367 368 369 370 371 372 373 374 375 376 377 378
        Gitlab::GitalyClient.migrate(:commit_raw_diffs) do |is_enabled|
          if is_enabled
            @repository.gitaly_commit_client.diff_from_parent(self, options)
          else
            rugged_diff_from_parent(options)
          end
        end
      end

      def rugged_diff_from_parent(options = {})
        options ||= {}
        break_rewrites = options[:break_rewrites]
        actual_options = Gitlab::Git::Diff.filter_diff_options(options)

379 380 381 382 383
        diff = if rugged_commit.parents.empty?
                 rugged_commit.diff(actual_options.merge(reverse: true))
               else
                 rugged_commit.parents[0].diff(rugged_commit, actual_options)
               end
384 385 386

        diff.find_similar!(break_rewrites: break_rewrites)
        diff
R
Robert Speicher 已提交
387 388
      end

389
      def deltas
390 391 392 393 394 395 396 397 398 399 400
        @deltas ||= begin
          deltas = Gitlab::GitalyClient.migrate(:commit_deltas) do |is_enabled|
            if is_enabled
              @repository.gitaly_commit_client.commit_deltas(self)
            else
              rugged_diff_from_parent.each_delta
            end
          end

          deltas.map { |delta| Gitlab::Git::Diff.new(delta) }
        end
401 402
      end

R
Robert Speicher 已提交
403 404 405 406 407 408 409 410 411 412 413 414
      def has_zero_stats?
        stats.total.zero?
      rescue
        true
      end

      def no_commit_message
        "--no commit message"
      end

      def to_hash
        serialize_keys.map.with_object({}) do |key, hash|
415
          hash[key] = send(key) # rubocop:disable GitlabSecurity/PublicSend
R
Robert Speicher 已提交
416 417 418 419 420 421 422 423
        end
      end

      def date
        committed_date
      end

      def diffs(options = {})
424
        Gitlab::Git::DiffCollection.new(diff_from_parent(options), options)
R
Robert Speicher 已提交
425 426 427
      end

      def parents
428
        parent_ids.map { |oid| self.class.find(@repository, oid) }.compact
R
Robert Speicher 已提交
429 430 431
      end

      def stats
432
        Gitlab::Git::CommitStats.new(@repository, self)
R
Robert Speicher 已提交
433 434 435 436
      end

      def to_patch(options = {})
        begin
437
          rugged_commit.to_mbox(options)
R
Robert Speicher 已提交
438
        rescue Rugged::InvalidError => ex
A
Alexis Reigel 已提交
439
          if ex.message =~ /commit \w+ is a merge commit/i
R
Robert Speicher 已提交
440 441 442 443 444 445 446 447 448 449 450 451
            'Patch format is not currently supported for merge commits.'
          end
        end
      end

      # Get ref names collection
      #
      # Ex.
      #   commit.ref_names(repo)
      #
      def ref_names(repo)
        refs(repo).map do |ref|
452
          ref.sub(%r{^refs/(heads|remotes|tags)/}, "")
R
Robert Speicher 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        end
      end

      def message
        encode! @message
      end

      def author_name
        encode! @author_name
      end

      def author_email
        encode! @author_email
      end

      def committer_name
        encode! @committer_name
      end

      def committer_email
        encode! @committer_email
      end

476 477 478 479 480 481 482 483
      def rugged_commit
        @rugged_commit ||= if raw_commit.is_a?(Rugged::Commit)
                             raw_commit
                           else
                             @repository.rev_parse_target(id)
                           end
      end

484 485 486 487
      def merge_commit?
        parent_ids.size > 1
      end

A
Ahmad Sherif 已提交
488
      def tree_entry(path)
Z
Zeger-Jan van de Weg 已提交
489 490
        return unless path.present?

A
Ahmad Sherif 已提交
491 492 493 494 495 496 497 498 499
        @repository.gitaly_migrate(:commit_tree_entry) do |is_migrated|
          if is_migrated
            gitaly_tree_entry(path)
          else
            rugged_tree_entry(path)
          end
        end
      end

500 501 502 503 504 505 506 507 508 509 510 511 512 513
      def to_gitaly_commit
        return raw_commit if raw_commit.is_a?(Gitaly::GitCommit)

        message_split = raw_commit.message.split("\n", 2)
        Gitaly::GitCommit.new(
          id: raw_commit.oid,
          subject: message_split[0] ? message_split[0].chomp.b : "",
          body: raw_commit.message.b,
          parent_ids: raw_commit.parent_ids,
          author: gitaly_commit_author_from_rugged(raw_commit.author),
          committer: gitaly_commit_author_from_rugged(raw_commit.committer)
        )
      end

R
Robert Speicher 已提交
514 515 516 517 518 519
      private

      def init_from_hash(hash)
        raw_commit = hash.symbolize_keys

        serialize_keys.each do |key|
520
          send("#{key}=", raw_commit[key]) # rubocop:disable GitlabSecurity/PublicSend
R
Robert Speicher 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        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

540 541 542 543 544 545
      def init_from_gitaly(commit)
        @raw_commit = commit
        @id = commit.id
        # TODO: Once gitaly "takes over" Rugged consider separating the
        # subject from the message to make it clearer when there's one
        # available but not the other.
546
        @message = (commit.body.presence || commit.subject).dup
547
        @authored_date = Time.at(commit.author.date.seconds).utc
548 549
        @author_name = commit.author.name.dup
        @author_email = commit.author.email.dup
550
        @committed_date = Time.at(commit.committer.date.seconds).utc
551 552
        @committer_name = commit.committer.name.dup
        @committer_email = commit.committer.email.dup
553
        @parent_ids = Array(commit.parent_ids)
554 555
      end

R
Robert Speicher 已提交
556 557 558
      def serialize_keys
        SERIALIZE_KEYS
      end
559

A
Ahmad Sherif 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
      def gitaly_tree_entry(path)
        # We're only interested in metadata, so limit actual data to 1 byte
        # since Gitaly doesn't support "send no data" option.
        entry = @repository.gitaly_commit_client.tree_entry(id, path, 1)
        return unless entry

        # To be compatible with the rugged format
        entry = entry.to_h
        entry.delete(:data)
        entry[:name] = File.basename(path)
        entry[:type] = entry[:type].downcase

        entry
      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

582 583 584 585 586 587 588
      def gitaly_commit_author_from_rugged(author_or_committer)
        Gitaly::CommitAuthor.new(
          name: author_or_committer[:name].b,
          email: author_or_committer[:email].b,
          date: Google::Protobuf::Timestamp.new(seconds: author_or_committer[:time].to_i)
        )
      end
589 590 591 592 593 594 595 596 597

      # Get a collection of Gitlab::Git::Ref objects for this commit.
      #
      # Ex.
      #   commit.ref(repo)
      #
      def refs(repo)
        repo.refs_hash[id]
      end
R
Robert Speicher 已提交
598 599 600
    end
  end
end