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.



16
17
18
19
# File 'lib/git/status.rb', line 16

def initialize(base)
  @base = base
  construct_status
end

Instance Method Details

#[](file)

enumerable method



126
127
128
# File 'lib/git/status.rb', line 126

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

#addedEnumerable

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

Returns:

  • (Enumerable)


46
47
48
# File 'lib/git/status.rb', line 46

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)


58
59
60
# File 'lib/git/status.rb', line 58

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)


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

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)


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

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)


67
68
69
# File 'lib/git/status.rb', line 67

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)


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

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

#each(&block)



130
131
132
# File 'lib/git/status.rb', line 130

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

#pretty



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

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

#pretty_file(file)



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

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)


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

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)


100
101
102
# File 'lib/git/status.rb', line 100

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