<commit_before>require 'rubygems/comparator/base'

class Gem::Comparator
  class DependencyComparator
    include Gem::Comparator::Base

    COMPARES = :specs

    ##
    # Compares dependencies in spec

    def compare(specs, report, options = {})
      info 'Checking dependencies...'

      filter_params(DEPENDENCY_PARAMS, options[:param]).each do |param|
        all_same = true
        type = param.gsub('_dependency', '').to_sym

        specs.each_with_index do |s, index|
          next if index == 0

          prev_deps = specs[index-1].dependencies.keep_if { |d| d.type == type }
          curr_deps = specs[index].dependencies.keep_if { |d| d.type == type }
          added, deleted, updated = resolve_dependencies(prev_deps, curr_deps)

          if (!deleted.empty? || !added.empty? || !updated.empty?)
            all_same = false
          end

          ver = "#{specs[index-1].version}->#{specs[index].version}"

          report[param][ver].section do
            set_header "#{Rainbow(specs[index-1].version).blue}->#{Rainbow(s.version).blue}: "

            nest('deleted').section do
              set_header '* Deleted:'
              puts deleted.map { |x| "#{x.name} #{x.requirements_list} (#{x.type})" } unless deleted.empty?
            end

            nest('added').section do
              set_header '* Added:'
              puts added.map { |x| "#{x.name} #{x.requirements_list} (#{x.type})" } unless added.empty?
            end

            nest('updated').section do
              set_header '* Updated:'
              puts updated unless updated.empty?
            end
          end
        end
        if all_same && options[:log_all]
          report[param].set_header "#{same} #{type} dependencies" if options[:log_all]
          report[param] << specs[0].dependencies.keep_if{ |d| d.type == type }.inspect
        elsif !all_same
          report[param].set_header "#{different} #{type} dependencies:"
        end
      end
      report
    end

    private

      def resolve_dependencies(prev_deps, curr_deps)
        added = curr_deps - prev_deps
        deleted = prev_deps - curr_deps
        split_dependencies(added, deleted)
      end

      def split_dependencies(added, deleted)
        # Find updated dependencies
        updated = []
        added.dup.each do |ad|
          deleted.dup.each do |dd|
            if ad.name == dd.name
              unless ad.requirements_list == dd.requirements_list
                updated << "#{ad.name} " +
         "from: #{dd.requirements_list} " +
               "to: #{ad.requirements_list}"
              end
              added.delete ad<commit_msg>Fix indentation<commit_after>require 'rubygems/comparator/base'

class Gem::Comparator
  class DependencyComparator
    include Gem::Comparator::Base

    COMPARES = :specs

    ##
    # Compares dependencies in spec

    def compare(specs, report, options = {})
      info 'Checking dependencies...'

      filter_params(DEPENDENCY_PARAMS, options[:param]).each do |param|
        all_same = true
        type = param.gsub('_dependency', '').to_sym

        specs.each_with_index do |s, index|
          next if index == 0

          prev_deps = specs[index-1].dependencies.keep_if { |d| d.type == type }
          curr_deps = specs[index].dependencies.keep_if { |d| d.type == type }
          added, deleted, updated = resolve_dependencies(prev_deps, curr_deps)

          if (!deleted.empty? || !added.empty? || !updated.empty?)
            all_same = false
          end

          ver = "#{specs[index-1].version}->#{specs[index].version}"

          report[param][ver].section do
            set_header "#{Rainbow(specs[index-1].version).blue}->#{Rainbow(s.version).blue}: "

            nest('deleted').section do
              set_header '* Deleted:'
              puts deleted.map { |x| "#{x.name} #{x.requirements_list} (#{x.type})" } unless deleted.empty?
            end

            nest('added').section do
              set_header '* Added:'
              puts added.map { |x| "#{x.name} #{x.requirements_list} (#{x.type})" } unless added.empty?
            end

            nest('updated').section do
              set_header '* Updated:'
              puts updated unless updated.empty?
            end
          end
        end
        if all_same && options[:log_all]
          report[param].set_header "#{same} #{type} dependencies" if options[:log_all]
          report[param] << specs[0].dependencies.keep_if{ |d| d.type == type }.inspect
        elsif !all_same
          report[param].set_header "#{different} #{type} dependencies:"
        end
      end
      report
    end

    private

      def resolve_dependencies(prev_deps, curr_deps)
        added = curr_deps - prev_deps
        deleted = prev_deps - curr_deps
        split_dependencies(added, deleted)
      end

      def split_dependencies(added, deleted)
        # Find updated dependencies
        updated = []
        added.dup.each do |ad|
          deleted.dup.each do |dd|
            if ad.name == dd.name
              unless ad.requirements_list == dd.requirements_list
                updated << "#{ad.name} " +
                           "from: #{dd.requirements_list} " +
                           "to: #{ad.requirements_list}"
              end
              added.delete ad
{'fraction_non_alphanumeric': 0.12368421052631579, 'fraction_numerical': 0.0018796992481203006, 'mean_word_length': 1.9893258426966292, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5004851', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1546, 'n_words': 338}