Class: Git::DiffPathStatus

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

Overview

The files and their status (e.g., added, modified, deleted) between two commits

Examples:

Iterate over path statuses

git = Git.open('/path/to/repo')
git.diff_path_status.each { |path, status| puts "#{path}: #{status}" }

Instance Method Summary collapse

Constructor Details

#initialize(base_or_hash, from = nil, to = nil, path_limiter = nil) ⇒ DiffPathStatus

Returns a new instance of DiffPathStatus.



16
17
18
19
20
21
22
23
24
25
# File 'lib/git/diff_path_status.rb', line 16

def initialize(base_or_hash, from = nil, to = nil, path_limiter = nil)
  if base_or_hash.is_a?(Hash)
    # New form: pre-fetched hash passed directly from Git::Repository::Diffing
    @fetch_path_status = base_or_hash
  else
    # Legacy form: (base, from, to, path_limiter)
    # Used by Git::Diff#path_status_provider and direct instantiation in tests.
    initialize_legacy(base_or_hash, from, to, path_limiter)
  end
end

Instance Method Details

#each {|path, status| ... } ⇒ Enumerator, Hash{String => String}

Iterates over each file path and its status code

Examples:

Print each path and status

diff_path_status.each { |path, status| puts "#{path}: #{status}" }

Yields:

  • (path, status)

    each file path with its git status code

Yield Parameters:

  • path (String)

    the file path relative to the repository root

  • status (String)

    the git status code (e.g. "M", "A", "D")

Yield Returns:

  • (void)

Returns:

  • (Enumerator, Hash{String => String})

    an Enumerator when no block is given; the name-status hash when a block is given



43
44
45
46
47
# File 'lib/git/diff_path_status.rb', line 43

def each(&)
  return to_enum(__method__) unless block_given?

  fetch_path_status.each(&)
end

#to_hHash{String => String}

Returns the name-status report as a hash

Examples:

Basic usage

diff_path_status.to_h #=> { "README.md" => "M", "lib/foo.rb" => "A" }

Returns:

  • (Hash{String => String})

    a mapping of file paths to their status codes



56
57
58
# File 'lib/git/diff_path_status.rb', line 56

def to_h
  fetch_path_status
end