class Kamaze::Project::Tools::Git::Status::File

Status file

Describe a file as seen in status, file is described by path and flags, and is immutable by design.

@see www.rubydoc.info/github/libgit2/rugged/Rugged%2FRepository%3Astatus

Attributes

base_dir[R]

@return [Pathname]

flags[R]

@return [String]

path[R]

@return [Pathname]

Public Class Methods

new(path, flags, base_dir = Dir.pwd) click to toggle source

@param [Pathname|String] path @param [Hash] flags @param [Pathname|String] base_dir

# File lib/kamaze/project/tools/git/status/file.rb, line 31
def initialize(path, flags, base_dir = Dir.pwd)
  @base_dir = ::Pathname.new(base_dir).freeze
  @path = ::Pathname.new(path).freeze
  @flags = flags.freeze
end

Public Instance Methods

==(other) click to toggle source

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 63
def ==(other)
  return false unless comparable_with?(other)

  [[self.path.to_s, other.path.to_s],
   [self.base_dir.to_s, other.base_dir.to_s],
   [self.flags.sort, other.flags.sort]].each do |c|
    return false unless c[0] == c[1]
  end

  true
end
absolute_path() click to toggle source

Get absolute path

@return [Pathname]

# File lib/kamaze/project/tools/git/status/file.rb, line 87
def absolute_path
  base_dir.join(path)
end
comparable_to?(other) click to toggle source

Denote instance is comparable with another object

@param [Object] other @return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 79
def comparable_to?(other)
  [:flags, :path, :base_dir]
    .map { |m| other.respond_to?(m) }.uniq == [true]
end
deleted?() click to toggle source

Denote deleted

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 129
def deleted?
  flags.values.include?(:deleted)
end
ignored?() click to toggle source

Denote ignored

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 94
def ignored?
  flags.keys.include?(:ignored)
end
index?() click to toggle source

Denote index

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 108
def index?
  flags.keys.include?(:index)
end
index_deleted?() click to toggle source

Denote deleted in index

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 157
def index_deleted?
  index? and deleted?
end
index_modified?() click to toggle source

Denote modified in index

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 150
def index_modified?
  index? and modified?
end
index_new?() click to toggle source

Denote new in index

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 143
def index_new?
  index? and new?
end
modified?() click to toggle source

Denote modified

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 122
def modified?
  flags.values.include?(:modified)
end
new?() click to toggle source

Denote new

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 115
def new?
  flags.values.include?(:new)
end
status() click to toggle source

Get a status string, composed of two chars

@see git-scm.com/docs/git-status @return [String]

# File lib/kamaze/project/tools/git/status/file.rb, line 46
def status
  return '??' if untracked?

  states = [' ', ' ']
  mapping = { new: 'A', modified: 'M', deleted: 'D' }
  { index: 0, worktree: 1 }.each do |from, index|
    next unless self.public_send("#{from}?")

    mapping.each do |flag, s|
      states[index] = s if self.public_send("#{from}_#{flag}?")
    end
  end

  states.join
end
to_s() click to toggle source

@return [String]

# File lib/kamaze/project/tools/git/status/file.rb, line 38
def to_s
  path.to_s
end
untracked?() click to toggle source

Denote untracked

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 136
def untracked?
  worktree? and new? and !index?
end
worktree?() click to toggle source

Denote worktree

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 101
def worktree?
  flags.keys.include?(:worktree)
end
worktree_deleted?() click to toggle source

Denote deleted in worktree

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 178
def worktree_deleted?
  worktree? and deleted?
end
worktree_modified?() click to toggle source

Denote modified in worktree

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 171
def worktree_modified?
  worktree? and modified?
end
worktree_new?() click to toggle source

Denote new in worktree

@return [Boolean]

# File lib/kamaze/project/tools/git/status/file.rb, line 164
def worktree_new?
  worktree? and new?
end