Class: Git::Status

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/status.rb,
lib/git/status.rb,
lib/git/status.rb

Overview

The Status class gets the status of a git repository. It identifies which files have been modified, added, or deleted, including untracked files. The Status object is an Enumerable of StatusFile objects.

Examples:

Inspect repository status

repo = Git.open('/path/to/repo')
status = repo.status
status.changed.each { |path, _file| puts "Modified: #{path}" }
status.added.each { |path, _file| puts "Added: #{path}" }
status.deleted.each { |path, _file| puts "Deleted: #{path}" }
status.untracked.each { |path, _file| puts "Untracked: #{path}" }

Defined Under Namespace

Classes: StatusFile, StatusFileFactory

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Status

Create a new Status for the given repository

Parameters:



25
26
27
28
29
# File 'lib/git/status.rb', line 25

def initialize(base)
  @base = base
  # The factory returns a hash of file paths to StatusFile objects.
  @files = StatusFileFactory.new(base).construct_files
end

Instance Method Details

#[](file) ⇒ Git::Status::StatusFile?

Return the StatusFile for the given path

Parameters:

  • file (String)

    the repository-relative path

Returns:



96
# File 'lib/git/status.rb', line 96

def [](file) = @files[file]

#addedHash{String => Git::Status::StatusFile}

Return files added to the index that are not yet in HEAD

Returns:



44
# File 'lib/git/status.rb', line 44

def added     = @added ||= select_files { |f| f.type == 'A' }

#added?(file) ⇒ Boolean

Return true if file has been added to the index

Parameters:

  • file (String)

    the repository-relative path to check

Returns:

  • (Boolean)

    true if the file has been added



72
# File 'lib/git/status.rb', line 72

def added?(file)     = file_in_collection?(:added, file)

#changedHash{String => Git::Status::StatusFile}

Return files modified in the index and/or working tree

Includes both staged modifications (index vs HEAD) and unstaged modifications (working tree vs index).

Returns:



38
# File 'lib/git/status.rb', line 38

def changed   = @changed ||= select_files { |f| f.type == 'M' }

#changed?(file) ⇒ Boolean

Return true if file has been modified in the index or working tree

Parameters:

  • file (String)

    the repository-relative path to check

Returns:

  • (Boolean)

    true if the file has been modified



64
# File 'lib/git/status.rb', line 64

def changed?(file)   = file_in_collection?(:changed, file)

#deletedHash{String => Git::Status::StatusFile}

Return files deleted from the index

Returns:



50
# File 'lib/git/status.rb', line 50

def deleted   = @deleted ||= select_files { |f| f.type == 'D' }

#deleted?(file) ⇒ Boolean

Return true if file has been deleted from the index

Parameters:

  • file (String)

    the repository-relative path to check

Returns:

  • (Boolean)

    true if the file has been deleted



80
# File 'lib/git/status.rb', line 80

def deleted?(file)   = file_in_collection?(:deleted, file)

#eachEnumerator<Git::Status::StatusFile> #each {|file| ... } ⇒ Array<Git::Status::StatusFile>

Iterate over all status files

Overloads:



114
# File 'lib/git/status.rb', line 114

def each(&) = @files.values.each(&)

#prettyString

Return a formatted multi-line string representation of the status

Returns:

  • (String)

    one indented block per file showing its SHA, mode, type, stage, and untracked flag



121
122
123
# File 'lib/git/status.rb', line 121

def pretty
  map { |file| pretty_file(file) }.join << "\n"
end

#untrackedHash{String => Git::Status::StatusFile}

Return files present in the working tree but not tracked by git

Returns:



56
# File 'lib/git/status.rb', line 56

def untracked = @untracked ||= select_files(&:untracked)

#untracked?(file) ⇒ Boolean

Return true if file is not tracked by git

Parameters:

  • file (String)

    the repository-relative path to check

Returns:

  • (Boolean)

    true if the file is untracked



88
# File 'lib/git/status.rb', line 88

def untracked?(file) = file_in_collection?(:untracked, file)