lib/rfix/file/tracked.rb in rfix-2.0.4 vs lib/rfix/file/tracked.rb in rfix-3.0.0

- old
+ new

@@ -1,42 +1,58 @@ # frozen_string_literal: true +require "rainbow/ext/string" + module Rfix module File - class Tracked < Base - attribute :status, Types::Status::Tracked + class Tracked < Dry::Struct + ID = "[T]".color(:lightseagreen).freeze - OPTIONS = { - include_untracked_content: true, - recurse_untracked_dirs: true, - include_untracked: true, - ignore_submodules: true, - include_ignored: false, - context_lines: 0 - }.freeze + attribute :status, Types::Symbol + attribute :path, Types::Path::Relative + attribute :repository, Repository - def include?(line) - diff.each_line.map(&:new_lineno).select(&:positive?).to_set.include?(line) + delegate :include?, to: :lines + + def key + absolute_path.to_s end + def absolute_path + repository.path.join(path) + end + + def exists? + true + end + def tracked? true end - private + def to_s + "%s:%s" % [path, to_str_range] + end - def options - OPTIONS.dup.merge(paths: [basename]) + def to_str_range + lines + .to_a + .sort + .chunk_while { |i, j| i + 1 == j } + .map { |a| a.length < 3 ? a : "#{a.first}-#{a.last}" } + .join(",") + .then { |res| res.empty? ? "-" : res } end - def diff - repository.origin.diff_workdir(**options).tap do |diff| - diff.find_similar!( - renames_from_rewrites: true, - renames: true, - copies: true - ) - end + def to_table + [path, to_str_range] + end + + def lines + Diff.new(repository: repository, options: { + paths: [path.to_path], + disable_pathspec_match: true, + }).lines.lazy.map(&:new_lineno).select(&:positive?) end end end end