module Gpr::Actions::Status

Public Instance Methods

git_status(path) click to toggle source
# File lib/gpr/actions/status.rb, line 8
def git_status(path)
  Dir.chdir(path)
  result = {}
  git_status = `git status -sb`.split("\n")
  branch_name = git_status.shift.match(/## (?<branch>.+)/)[:branch]
  unstaged_changes = []
  untracked_files = []

  git_status.each do |file|
    if match_object = file.match(/^\sM (.+)$/)
      unstaged_changes << match_object[1]
    elsif match_object = file.match(/^\?\? (.+)$/)
      untracked_files << match_object[1]
    end
  end

  # Means no branch || ahead || behind
  if branch_name =~ /HEAD|\[.+\]/
    result[:branch] = branch_name.color(:red)
  else
    result[:branch] = branch_name.color(:green)
  end

  if unstaged_changes.empty? && untracked_files.empty?
    result[:directory] = 'Working directory clean'.color(:green)
  else
    result[:directory] = "Modified[#{unstaged_changes.size}] ??[#{untracked_files.size}]".color(:red)
  end

  result
end