class Grit::Status

Attributes

files[R]

Public Class Methods

new(base) click to toggle source
# File lib/grit_ext/status.rb, line 10
def initialize(base)
  @base = base
  construct_status
end

Public Instance Methods

[](file) click to toggle source

enumerable method

# File lib/grit_ext/status.rb, line 48
def [](file)
  @files[file]
end
added() click to toggle source
# File lib/grit_ext/status.rb, line 19
def added
  @files.select{|k, f| f.type == 'A'}
end
changed() click to toggle source
# File lib/grit_ext/status.rb, line 15
def changed
  @files.select{|k, f| f.type == 'M'}
end
deleted() click to toggle source
# File lib/grit_ext/status.rb, line 23
def deleted
  @files.select{|k, f| f.type == 'D'}
end
each() { |file| ... } click to toggle source
# File lib/grit_ext/status.rb, line 52
def each
  @files.each do |k, file|
    yield file
  end
end
pretty() click to toggle source
# File lib/grit_ext/status.rb, line 31
def pretty
  out = ''
  each do |file|
    out << file.path
    out << "\n\tsha(r) " + file.sha_repo.to_s + ' ' + file.mode_repo.to_s
    out << "\n\tsha(i) " + file.sha_index.to_s + ' ' + file.mode_index.to_s
    out << "\n\ttype   " + file.type.to_s
    out << "\n\tstage  " + file.stage.to_s
    out << "\n\tuntrac " + file.untracked.to_s
    out << "\n"
  end
  out << "\n"
  out
end
untracked() click to toggle source
# File lib/grit_ext/status.rb, line 27
def untracked
  @files.select{|k, f| f.untracked && !f.ignored}
end

Private Instance Methods

construct_status() click to toggle source
# File lib/grit_ext/status.rb, line 89
def construct_status
  @files = ls_files

  Dir.chdir(@base.working_dir) do
    # find untracked in working dir
    ls_untracked.each do |path, data|
      @files[path] = data unless @files[path]
    end

    # find modified in tree
    diff_files.each do |path, data|
      @files[path] ? @files[path].merge!(data) : @files[path] = data
    end

    # find added but not committed - new files
    diff_index('HEAD').each do |path, data|
      @files[path] ? @files[path].merge!(data) : @files[path] = data
    end

    @files.each do |k, file_hash|
      @files[k] = StatusFile.new(@base, file_hash)
    end
  end
end
diff_files() click to toggle source

compares the index and the working directory

# File lib/grit_ext/status.rb, line 115
def diff_files
  hsh = {}
  @base.git.diff_files.split("\n").each do |line|
    (info, file) = line.split("\t")
    (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
    hsh[file] = {path: file, mode_file: mode_src.to_s[1, 7], mode_index: mode_dest,
                 sha_file: sha_src, sha_index: sha_dest, type: type}
  end
  hsh
end
diff_index(treeish) click to toggle source

compares the index and the repository

# File lib/grit_ext/status.rb, line 127
def diff_index(treeish)
  hsh = {}
  @base.git.diff_index({}, treeish).split("\n").each do |line|
    (info, file) = line.split("\t")
    (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
    hsh[file] = {path: file, mode_repo: mode_src.to_s[1, 7], mode_index: mode_dest,
                 sha_repo: sha_src, sha_index: sha_dest, type: type}
  end
  hsh
end
ls_files() click to toggle source
# File lib/grit_ext/status.rb, line 138
def ls_files
  hsh = {}
  lines = @base.git.ls_files(stage: true)
  lines.split("\n").each do |line|
    (info, file) = line.split("\t")
    (mode, sha, stage) = info.split
    hsh[file] = {path: file, mode_index: mode, sha_index: sha, stage: stage}
  end
  hsh
end
ls_untracked() click to toggle source
# File lib/grit_ext/status.rb, line 149
def ls_untracked
  hsh, wdir = {}, @base.working_dir

  # directories and hidden files are skiped so as to preserve
  # backward compatibility with 2.4.1
  skip = lambda{|f|
    File.directory?(File.join(wdir, f)) || f =~ /^\./
  }

  # `git ls-files --others --ignore --exclude-standard --directory`
  # returns untracked and ignored files as well as ignored
  # directories
  ignored_dirs = []
  @base.git.ls_files(:others => true,
                     :ignored => true, :directory => true,
                     :"exclude-standard" => true).
            split("\n").each do |file|
    if File.directory?(File.join(wdir, file))
      ignored_dirs << file
    elsif !skip[file]
      hsh[file] = {path: file,
                   untracked: true,
                   ignored: true}
    end
  end

  # `git ls-files --others` is used for remaining files
  @base.git.ls_files(others: true).
            split("\n").each do |file|
    if ignored_dirs.any?{|d| file.index(d) == 0}
      hsh[file] = {path: file,
                   untracked: true,
                   ignored: true}
    elsif !(skip[file] || hsh[file])
      hsh[file] = {path: file,
                   untracked: true}
    end
  end

  hsh
end