class Git::Status

A class for git status

Public Class Methods

new(base) click to toggle source
# File lib/git/status.rb, line 8
def initialize(base)
  @base = base
  construct_status
end

Public Instance Methods

[](file) click to toggle source

enumerable method

# File lib/git/status.rb, line 119
def [](file)
  @files[file]
end
added() click to toggle source

Returns an Enumerable containing files that have been added. File path starts at git base directory

@return [Enumerable]

# File lib/git/status.rb, line 39
def added
  @files.select { |_k, f| f.type == 'A' }
end
added?(file) click to toggle source

Determines whether the given file has been added to the repository File path starts at git base directory

@param file [String] The name of the file. @example Check if lib/git.rb is added.

added?('lib/git.rb')

@return [Boolean]

# File lib/git/status.rb, line 51
def added?(file)
  added.member?(file)
end
changed() click to toggle source

Returns an Enumerable containing files that have changed from the git base directory

@return [Enumerable]

# File lib/git/status.rb, line 18
def changed
  @files.select { |_k, f| f.type == 'M' }
end
changed?(file) click to toggle source

Determines whether the given file has been changed. File path starts at git base directory

@param file [String] The name of the file. @example Check if lib/git.rb has changed.

changed?('lib/git.rb')

@return [Boolean]

# File lib/git/status.rb, line 30
def changed?(file)
  changed.member?(file)
end
deleted() click to toggle source

Returns an Enumerable containing files that have been deleted. File path starts at git base directory

@return [Enumerable]

# File lib/git/status.rb, line 60
def deleted
  @files.select { |_k, f| f.type == 'D' }
end
deleted?(file) click to toggle source

Determines whether the given file has been deleted from the repository File path starts at git base directory

@param file [String] The name of the file. @example Check if lib/git.rb is deleted.

deleted?('lib/git.rb')

@return [Boolean]

# File lib/git/status.rb, line 72
def deleted?(file)
  deleted.member?(file)
end
each(&block) click to toggle source
# File lib/git/status.rb, line 123
def each(&block)
  @files.values.each(&block)
end
pretty() click to toggle source
# File lib/git/status.rb, line 97
def pretty
  out = ''
  each do |file|
    out << pretty_file(file)
  end
  out << "\n"
  out
end
pretty_file(file) click to toggle source
# File lib/git/status.rb, line 106
    def pretty_file(file)
      <<~FILE
        #{file.path}
        \tsha(r) #{file.sha_repo} #{file.mode_repo}
        \tsha(i) #{file.sha_index} #{file.mode_index}
        \ttype   #{file.type}
        \tstage  #{file.stage}
        \tuntrac #{file.untracked}
      FILE
    end
untracked() click to toggle source

Returns an Enumerable containing files that are not tracked in git. File path starts at git base directory

@return [Enumerable]

# File lib/git/status.rb, line 81
def untracked
  @files.select { |_k, f| f.untracked }
end
untracked?(file) click to toggle source

Determines whether the given file has is tracked by git. File path starts at git base directory

@param file [String] The name of the file. @example Check if lib/git.rb is an untracked file.

untracked?('lib/git.rb')

@return [Boolean]

# File lib/git/status.rb, line 93
def untracked?(file)
  untracked.member?(file)
end

Private Instance Methods

construct_status() click to toggle source
# File lib/git/status.rb, line 160
def construct_status
  @files = @base.lib.ls_files

  fetch_untracked
  fetch_modified
  fetch_added

  @files.each do |k, file_hash|
    @files[k] = StatusFile.new(@base, file_hash)
  end
end
fetch_added() click to toggle source
# File lib/git/status.rb, line 192
def fetch_added
  # find added but not committed - new files
  @base.lib.diff_index('HEAD').each do |path, data|
    @files[path] ? @files[path].merge!(data) : @files[path] = data
  end
end
fetch_modified() click to toggle source
# File lib/git/status.rb, line 185
def fetch_modified
  # find modified in tree
  @base.lib.diff_files.each do |path, data|
    @files[path] ? @files[path].merge!(data) : @files[path] = data
  end
end
fetch_untracked() click to toggle source
# File lib/git/status.rb, line 172
def fetch_untracked
  ignore = @base.lib.ignored_files

  Dir.chdir(@base.dir.path) do
    Dir.glob('**/*', File::FNM_DOTMATCH) do |file|
      next if @files[file] || File.directory?(file) ||
              ignore.include?(file) || file =~ %r{^.git\/.+}

      @files[file] = { path: file, untracked: true }
    end
  end
end