git-ahead-check/src/git-ahead-check.cr

70 lines
1.4 KiB
Crystal

require "colorize"
require "option_parser"
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
short_output = false
begin
OptionParser.parse do |parser|
parser.banner = "The very simple password manager for humans\n"
parser.on "-h", "--help", "Show help" do
puts parser
exit(0)
end
parser.on "-s", "Short output" do
short_output = true
end
parser.on "-v", "Show version" do
puts VERSION
exit(0)
end
end
rescue ex
puts ex.message, ""
end
CONFIG_PATH = "#{ENV["HOME"]}/.config/emilecok/git-ahead-check"
if File.exists?(CONFIG_PATH)
a = File.read_lines(CONFIG_PATH)
repos_dirs = File.read_lines(CONFIG_PATH)
repos_dirs.each { |x| check_repo(x, short_output) }
else
Dir.mkdir_p(File.dirname(CONFIG_PATH))
File.new(CONFIG_PATH, "w")
print File.dirname(CONFIG_PATH)
puts " directory created.\n"
end
def check_repo(repo_path : String, show_path)
if !Dir.exists?(repo_path.sub("$HOME", ENV["HOME"]))
return 0
end
if !Dir.exists?("#{repo_path}/.git")
return 0
end
repo_name = repo_path.split("/")[-1]
ahead_count = `git -C #{repo_path} status | grep -i "Your branch" | grep -Eo "[0-9]"`.chomp
if !ahead_count.empty?
print "* #{ahead_count}\t".colorize(:red).mode(:bold)
print repo_name.colorize(:yellow)
if !show_path
print " ["
print repo_path.colorize(:light_magenta)
puts "]"
else
puts
end
end
end