|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyGit |
| 4 | + module Status |
| 5 | + # Base class for git status entries |
| 6 | + # |
| 7 | + # @api public |
| 8 | + class Entry |
| 9 | + # Status code mapping to symbols |
| 10 | + STATUS_CODES = { |
| 11 | + '.': :unmodified, |
| 12 | + M: :modified, |
| 13 | + T: :type_changed, |
| 14 | + A: :added, |
| 15 | + D: :deleted, |
| 16 | + R: :renamed, |
| 17 | + C: :copied, |
| 18 | + U: :updated_but_unmerged, |
| 19 | + '?': :untracked, |
| 20 | + '!': :ignored |
| 21 | + }.freeze |
| 22 | + |
| 23 | + # Rename operation mapping to symbols |
| 24 | + RENAME_OPERATIONS = { |
| 25 | + 'R' => :rename |
| 26 | + # git status doesn't actually try to detect copies |
| 27 | + # 'C' => :copy |
| 28 | + }.freeze |
| 29 | + |
| 30 | + # @attribute [r] path |
| 31 | + # |
| 32 | + # The path of the file |
| 33 | + # |
| 34 | + # @example |
| 35 | + # entry.path #=> 'lib/example.rb' |
| 36 | + # |
| 37 | + # @return [String] file path |
| 38 | + # |
| 39 | + attr_reader :path |
| 40 | + |
| 41 | + # Initialize a new entry |
| 42 | + # |
| 43 | + # @example |
| 44 | + # Entry.new('lib/example.rb') |
| 45 | + # |
| 46 | + # @param path [String] file path |
| 47 | + # |
| 48 | + def initialize(path) |
| 49 | + @path = path |
| 50 | + end |
| 51 | + |
| 52 | + # Convert a status code to a symbol |
| 53 | + # |
| 54 | + # @example |
| 55 | + # Entry.status_to_symbol('M') #=> :modified |
| 56 | + # |
| 57 | + # @param code [String] status code |
| 58 | + # @return [Symbol] status as symbol |
| 59 | + # |
| 60 | + def self.status_to_symbol(code) |
| 61 | + STATUS_CODES[code.to_sym] || :unknown |
| 62 | + end |
| 63 | + |
| 64 | + # Convert a rename operation to a symbol |
| 65 | + # |
| 66 | + # @example |
| 67 | + # Entry.rename_operation_to_symbol('R') #=> :rename |
| 68 | + # |
| 69 | + # @param code [String] the operation code |
| 70 | + # @return [Symbol] operation as symbol |
| 71 | + # |
| 72 | + def self.rename_operation_to_symbol(code) |
| 73 | + RENAME_OPERATIONS[code] || :unknown |
| 74 | + end |
| 75 | + |
| 76 | + # Get the staging status |
| 77 | + # |
| 78 | + # @example |
| 79 | + # entry.staging_status #=> :modified |
| 80 | + # |
| 81 | + # @return [Symbol, nil] staging status symbol or nil if not applicable |
| 82 | + # |
| 83 | + def index_status = nil |
| 84 | + |
| 85 | + # Get the worktree status |
| 86 | + # |
| 87 | + # @example |
| 88 | + # entry.worktree_status #=> :unchanged |
| 89 | + # |
| 90 | + # @return [Symbol, nil] worktree status symbol or nil if not applicable |
| 91 | + # |
| 92 | + def worktree_status = nil |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments