Sha256: 76a7192ef13d33f8dd1338d17d2a7a0ae2b9dec3206ef4355cb077c9899efa29

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'ostruct'

module EacGit
  class Local
    module DirtyFiles
      QUOTED_PATH_PATTERN = /\A"(.+)"\z/.freeze
      STATUS_LINE_PATTERN = /\A(.)(.)\s(.+)\z/.freeze

      def dirty?
        dirty_files.any?
      end

      def dirty_file?(path)
        absolute_path = path.to_pathname.expand_path(root_path)
        dirty_files.any? do |df|
          df.absolute_path == absolute_path
        end
      end

      def dirty_files
        command('status', '--porcelain=v1', '--untracked-files', '--no-renames')
          .execute!.each_line.map { |line| parse_status_line(line.gsub(/\n\z/, '')) }
      end

      private

      # @param line [String]
      # @return [Struct]
      def parse_status_line(line)
        STATUS_LINE_PATTERN.if_match(line) do |m|
          path = parse_status_line_path(m[3]).to_pathname
          { index: m[1], worktree: m[2], path: path, absolute_path: path.expand_path(root_path) }
            .to_struct
        end
      end

      # @param path [String]
      # @return [String]
      def parse_status_line_path(path)
        m = QUOTED_PATH_PATTERN.match(path)
        m ? m[1] : path
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
eac_tools-0.86.5 sub/eac_git/lib/eac_git/local/dirty_files.rb
eac_tools-0.86.4 sub/eac_git/lib/eac_git/local/dirty_files.rb
eac_tools-0.86.3 sub/eac_git/lib/eac_git/local/dirty_files.rb
eac_git-0.16.0 lib/eac_git/local/dirty_files.rb
eac_tools-0.86.2 sub/eac_git/lib/eac_git/local/dirty_files.rb
eac_git-0.15.0 lib/eac_git/local/dirty_files.rb
eac_tools-0.84.2 sub/eac_git/lib/eac_git/local/dirty_files.rb