Class: Git::Status

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

Overview

A class for git status

Defined Under Namespace

Classes: StatusFile

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Status

Returns a new instance of Status.


8
9
10
11
# File 'lib/git/status.rb', line 8

def initialize(base)
  @base = base
  construct_status
end

Instance Method Details

#[](file)

enumerable method


119
120
121
# File 'lib/git/status.rb', line 119

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

#addedEnumerable

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

Returns:

  • (Enumerable)

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

def 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)

51
52
53
# File 'lib/git/status.rb', line 51

def added?(file)
  added.member?(file)
end

#changedEnumerable

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

Returns:

  • (Enumerable)

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

def 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)

30
31
32
# File 'lib/git/status.rb', line 30

def changed?(file)
  changed.member?(file)
end

#deletedEnumerable

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

Returns:

  • (Enumerable)

60
61
62
# File 'lib/git/status.rb', line 60

def 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)

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

def deleted?(file)
  deleted.member?(file)
end

#each(&block)


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

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

#pretty


97
98
99
100
101
102
103
104
# 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)


106
107
108
109
110
111
112
113
114
115
# 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

#untrackedEnumerable

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

Returns:

  • (Enumerable)

81
82
83
# File 'lib/git/status.rb', line 81

def 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)

93
94
95
# File 'lib/git/status.rb', line 93

def untracked?(file)
  untracked.member?(file)
end