class Git::Status
@api public
The Status object is an Enumerable that contains StatusFile objects.
worktree. Untracked files are also identified.
This identifies which files have been modified, added, or deleted from the
The status class gets the status of a git repository
def [](file)
def [](file) @files[file] end
def added
-
(Enumerable)
-
def added @_added ||= @files.select { |_k, f| f.type == 'A' } end
def added?(file)
-
(Boolean)
-
Other tags:
- Example: Check if lib/git.rb is added. -
Parameters:
-
file
(String
) -- The name of the file.
def added?(file) case_aware_include?(:added, :lc_added, file) end
def case_aware_include?(cased_hash, downcased_hash, file)
def case_aware_include?(cased_hash, downcased_hash, file) if ignore_case? send(downcased_hash).include?(file.downcase) else send(cased_hash).include?(file) end end
def changed
-
(Enumerable)
-
def changed @_changed ||= @files.select { |_k, f| f.type == 'M' } end
def changed?(file)
-
(Boolean)
-
Other tags:
- Example: Check if lib/git.rb has changed. -
Parameters:
-
file
(String
) -- The name of the file.
def changed?(file) case_aware_include?(:changed, :lc_changed, file) end
def construct_status
def construct_status # Lists all files in the index and the worktree # git ls-files --stage # { file => { path: file, mode_index: '100644', sha_index: 'dd4fc23', stage: '0' } } @files = @base.lib.ls_files # Lists files in the worktree that are not in the index # Add untracked files to @files fetch_untracked # Lists files that are different between the index vs. the worktree fetch_modified # Lists files that are different between the repo HEAD vs. the worktree fetch_added @files.each do |k, file_hash| @files[k] = StatusFile.new(@base, file_hash) end end
def deleted
-
(Enumerable)
-
def deleted @_deleted ||= @files.select { |_k, f| f.type == 'D' } end
def deleted?(file)
-
(Boolean)
-
Other tags:
- Example: Check if lib/git.rb is deleted. -
Parameters:
-
file
(String
) -- The name of the file.
def deleted?(file) case_aware_include?(:deleted, :lc_deleted, file) end
def downcase_keys(hash)
def downcase_keys(hash) hash.map { |k, v| [k.downcase, v] }.to_h end
def each(&block)
def each(&block) @files.values.each(&block) end
def fetch_added
def fetch_added unless @base.lib.empty? # Files changed between the repo HEAD vs. the worktree # git diff-index HEAD # { file => { path: file, type: 'M', mode_index: '100644', mode_repo: '100644', sha_index: '0000000', :sha_repo: '52c6c4e' } } @base.lib.diff_index('HEAD').each do |path, data| @files[path] ? @files[path].merge!(data) : @files[path] = data end end end
def fetch_modified
def fetch_modified # Files changed between the index vs. the worktree # git diff-files # { file => { path: file, type: 'M', mode_index: '100644', mode_repo: '100644', sha_index: '0000000', :sha_repo: '52c6c4e' } } @base.lib.diff_files.each do |path, data| @files[path] ? @files[path].merge!(data) : @files[path] = data end end
def fetch_untracked
def fetch_untracked # git ls-files --others --exclude-standard, chdir: @git_work_dir) # { file => { path: file, untracked: true } } @base.lib.untracked_files.each do |file| @files[file] = { path: file, untracked: true } end end
def ignore_case?
ignoreCase is set inconsistently with the file-system itself. For details:
It's worth noting that (like git itself) this gem will not behave well if
def ignore_case? return @_ignore_case if defined?(@_ignore_case) @_ignore_case = @base.config('core.ignoreCase') == 'true' rescue Git::FailedError @_ignore_case = false end
def initialize(base)
def initialize(base) @base = base construct_status end
def lc_added
def lc_added @_lc_added ||= added.transform_keys(&:downcase) end
def lc_changed
def lc_changed @_lc_changed ||= changed.transform_keys(&:downcase) end
def lc_deleted
def lc_deleted @_lc_deleted ||= deleted.transform_keys(&:downcase) end
def lc_untracked
def lc_untracked @_lc_untracked ||= untracked.transform_keys(&:downcase) end
def pretty
def pretty out = +'' each do |file| out << pretty_file(file) end out << "\n" out end
def pretty_file(file)
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
def untracked
-
(Enumerable)
-
def untracked @_untracked ||= @files.select { |_k, f| f.untracked } end
def untracked?(file)
-
(Boolean)
-
Other tags:
- Example: Check if lib/git.rb is an untracked file. -
Parameters:
-
file
(String
) -- The name of the file.
def untracked?(file) case_aware_include?(:untracked, :lc_untracked, file) end