Class: Gitguard::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/gitguard/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_command) ⇒ Runner

Returns a new instance of Runner.



9
10
11
# File 'lib/gitguard/runner.rb', line 9

def initialize(user_command)
  @user_command = user_command
end

Instance Attribute Details

#user_commandObject (readonly)

Returns the value of attribute user_command.



8
9
10
# File 'lib/gitguard/runner.rb', line 8

def user_command
  @user_command
end

Instance Method Details

#at_root(&block) ⇒ Object

Raises:



35
36
37
38
39
# 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?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gitguard/runner.rb', line 48

def changed?
  !unchanged?
end

#clean?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gitguard/runner.rb', line 41

def clean?
  unchanged? && no_untracked_files?
end

#execute(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/gitguard/runner.rb', line 52

def no_untracked_files?
  at_root{ `git ls-files --others --exclude-standard`.empty? }
end

#unchanged?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/gitguard/runner.rb', line 45

def unchanged?
  system("git diff --exit-code > /dev/null")
end

#untracked_files?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gitguard/runner.rb', line 55

def untracked_files?
  !no_untracked_files?
end