<commit_before>require 'pathname'

module Pronto
  module Git
    class Repository
      def initialize(path)
        @repo = Rugged::Repository.new(path)
      end

      def diff(commit, options = nil)
        target, patches = case commit
                          when :unstaged, :index
                            [head_commit_sha, @repo.index.diff(options)]
                          when :staged
                            [head_commit_sha, head.diff(@repo.index, options)]
                          else
                            merge_base = merge_base(commit)
                            patches = @repo.diff(merge_base, head, options)
                            patches.find_similar!(renames: true)
                            [merge_base, patches]
                          end

        Patches.new(self, target, patches)
      end

      def show_commit(sha)
        return empty_patches(sha) unless sha

        commit = @repo.lookup(sha)
        return empty_patches(sha) if commit.parents.count != 1

        # TODO: Rugged does not seem to support diffing against multiple parents
        diff = commit.diff(reverse: true)
        return empty_patches(sha) if diff.nil?

        Patches.new(self, sha, diff.patches)
      end

      def commits_until(sha)
        result = []
        @repo.walk(head, Rugged::SORT_TOPO).take_while do |commit|
          result << commit.oid
          !commit.oid.start_with?(sha)
        end
        result
      end

      def path<commit_msg>Git/Repository: moved patches.find_similar! so it works for all cases (e.g., unstaged, index, ...)<commit_after>require 'pathname'

module Pronto
  module Git
    class Repository
      def initialize(path)
        @repo = Rugged::Repository.new(path)
      end

      def diff(commit, options = nil)
        target, patches = case commit
                          when :unstaged, :index
                            [head_commit_sha, @repo.index.diff(options)]
                          when :staged
                            [head_commit_sha, head.diff(@repo.index, options)]
                          else
                            merge_base = merge_base(commit)
                            patches = @repo.diff(merge_base, head, options)
                            [merge_base, patches]
                          end

        patches.find_similar!(renames: true)
        Patches.new(self, target, patches)
      end

      def show_commit(sha)
        return empty_patches(sha) unless sha

        commit = @repo.lookup(sha)
        return empty_patches(sha) if commit.parents.count != 1

        # TODO: Rugged does not seem to support diffing against multiple parents
        diff = commit.diff(reverse: true)
        return empty_patches(sha) if diff.nil?

        Patches.new(self, sha, diff.patches)
      end

      def commits_until(sha)
        result = []
        @repo.walk(head, Rugged::SORT_TOPO).take_while do |commit|
          result << commit.oid
          !commit.oid.start_with?(sha)
        end
        result
      end

      def path
{'fraction_non_alphanumeric': 0.08297941849068932, 'fraction_numerical': 0.0006533812479581836, 'mean_word_length': 1.5474209650582362, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2733962', 'n_tokens_mistral': 835, 'n_tokens_neox': 815, 'n_words': 205}