Class: Git::Status

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

Overview

The status class gets the status of a git repository

This identifies which files have been modified, added, or deleted from the worktree. Untracked files are also identified.

The Status object is an Enumerable that contains StatusFile objects.

Defined Under Namespace

Classes: StatusFile

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Status

Returns a new instance of Status.



14
15
16
17
# File 'lib/git/status.rb', line 14

def initialize(base)
  @base = base
  construct_status
end

Instance Method Details

#[](file)

enumerable method



124
125
126
# File 'lib/git/status.rb', line 124

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

#addedEnumerable

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

Returns:

  • (Enumerable)


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

def added
  @_added ||= @files.select { |_k, f| f.type == 'A' }
end

#added?(file) ⇒ Boolean

Determines whether the given file has been added to the repository

File path starts at git base directory

Examples:

Check if lib/git.rb is added.

added?('lib/git.rb')

Parameters:

  • file (String)

    The name of the file.

Returns:

  • (Boolean)


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

def added?(file)
  case_aware_include?(:added, :lc_added, file)
end

#changedEnumerable

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

Returns:

  • (Enumerable)


24
25
26
# File 'lib/git/status.rb', line 24

def changed
  @_changed ||= @files.select { |_k, f| f.type == 'M' }
end

#changed?(file) ⇒ Boolean

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

Examples:

Check if lib/git.rb has changed.

changed?('lib/git.rb')

Parameters:

  • file (String)

    The name of the file.

Returns:

  • (Boolean)


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

def changed?(file)
  case_aware_include?(:changed, :lc_changed, file)
end

#deletedEnumerable

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

Returns:

  • (Enumerable)


65
66
67
# File 'lib/git/status.rb', line 65

def deleted
  @_deleted ||= @files.select { |_k, f| f.type == 'D' }
end

#deleted?(file) ⇒ Boolean

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

Examples:

Check if lib/git.rb is deleted.

deleted?('lib/git.rb')

Parameters:

  • file (String)

    The name of the file.

Returns:

  • (Boolean)


77
78
79
# File 'lib/git/status.rb', line 77

def deleted?(file)
  case_aware_include?(:deleted, :lc_deleted, file)
end

#each(&block)



128
129
130
# File 'lib/git/status.rb', line 128

def each(&block)
  @files.values.each(&block)
end

#pretty



102
103
104
105
106
107
108
109
# File 'lib/git/status.rb', line 102

def pretty
  out = ''
  each do |file|
    out << pretty_file(file)
  end
  out << "\n"
  out
end

#pretty_file(file)



111
112
113
114
115
116
117
118
119
120
# File 'lib/git/status.rb', line 111

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

#untrackedEnumerable

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

Returns:

  • (Enumerable)


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

def untracked
  @_untracked ||= @files.select { |_k, f| f.untracked }
end

#untracked?(file) ⇒ Boolean

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

Examples:

Check if lib/git.rb is an untracked file.

untracked?('lib/git.rb')

Parameters:

  • file (String)

    The name of the file.

Returns:

  • (Boolean)


98
99
100
# File 'lib/git/status.rb', line 98

def untracked?(file)
  case_aware_include?(:untracked, :lc_untracked, file)
end