class Gitguard::Runner

Attributes

user_command[R]

Public Class Methods

new(user_command) click to toggle source
# File lib/gitguard/runner.rb, line 9
def initialize(user_command)
  @user_command = user_command
end

Public Instance Methods

at_root(&block) click to toggle source
# File lib/gitguard/runner.rb, line 35
def at_root(&block)
  root_dir = DirSearch.up{|dir| Dir.exist?(File.join(dir, '.git')) }
  raise Error, "Directory not found: .git" unless root_dir
  Dir.chdir(root_dir, &block)
end
changed?() click to toggle source
# File lib/gitguard/runner.rb, line 48
def changed?
  !unchanged?
end
clean?() click to toggle source
# File lib/gitguard/runner.rb, line 41
def clean?
  unchanged? && no_untracked_files?
end
execute() { || ... } click to toggle source
# File lib/gitguard/runner.rb, line 13
def execute(&block)
  unless clean?
    $stderr.puts "\e[31m[gitguard] There are files that need to be committed first.\e[0m"
    $stderr.puts "[gitguard] git status"
    system 'git status'
    exit(1)
  end
  yield
  return if clean?
  at_root do
    cmd = "git add . && git commit -m #{Shellwords.escape(user_command)}"
    puts "\e[34m#{cmd}"
    r = false
    begin
      r = system(cmd)
    ensure
      print "\e[0m"
    end
    raise Error, "Error on #{cmd}" unless r
  end
end
no_untracked_files?() click to toggle source
# File lib/gitguard/runner.rb, line 52
def no_untracked_files?
  at_root{ `git ls-files --others --exclude-standard`.empty? }
end
unchanged?() click to toggle source
# File lib/gitguard/runner.rb, line 45
def unchanged?
  system("git diff --exit-code > /dev/null")
end
untracked_files?() click to toggle source
# File lib/gitguard/runner.rb, line 55
def untracked_files?
  !no_untracked_files?
end