Module: Rusky

Defined in:
lib/rusky.rb,
lib/rusky/task.rb,
lib/rusky/version.rb

Defined Under Namespace

Classes: Task

Constant Summary collapse

HOOKS =
%w[
  applypatch-msg
  pre-applypatch
  post-applypatch
  pre-commit
  prepare-commit-msg
  commit-msg
  post-commit
  pre-rebase
  post-checkout
  post-merge
  pre-push
  pre-receive
  update
  post-receive
  post-update
  push-to-checkout
  pre-auto-gc
  post-rewrite
  sendemail-validate
].freeze
VERSION =
"0.1.8"

Class Method Summary collapse

Class Method Details

.create_hook(hook_name, hook_path, cwd) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rusky.rb', line 54

def self.create_hook(hook_name, hook_path, cwd)
  script = get_hook_script(hook_name, cwd)
  filename = File.join(hook_path, hook_name)

  if File.exists? filename
    if File.read(filename).include? 'rusky'
      # Overwrite
      write(filename, script)
    else
      # Keep user original Git hook
    end
  else
    write(filename, script)
  end
end

.get_hook_script(hook_name, cwd) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rusky.rb', line 75

def self.get_hook_script(hook_name, cwd)
  no_verify_message = if hook_name == 'prepare-commit-msg'
                        '(cannot be bypassed with --no-verify due to Git specs)'
                      else
                        '(add --no-verify to bypass)'
                      end

  rake_task_name = "rusky:#{hook_name.gsub('-', '_')}"

  "    #!/bin/sh\n    #rusky \#{Rusky::VERSION}\n    has_hook_script () {\n      [ -f .rusky ] && cat .rusky | grep -q \"$1:\"\n    }\n    cd \"\#{cwd}\"\n    # Check if \#{hook_name} script is defined, skip if not\n    has_hook_script \#{hook_name} || exit 0\n\n    # Export Git hook params\n    export GIT_PARAMS=\"$*\"\n    # Run command\n    echo \"rusky > \#{hook_name} Git hook is running\"\n    echo \"rusky > bundle exec rake \#{rake_task_name}\"\n    echo\n    bundle exec rake \#{rake_task_name} || {\n      echo\n      echo \"rusky > \#{hook_name} Git hook failed \#{no_verify_message}\"\n      exit 1\n    }\n  EOS\nend\n"

.installObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rusky.rb', line 28

def self.install
  cwd = `lsof -p #{Process.ppid} | grep cwd`.split(" ").last

  git_path = File.join(cwd, '.git')
  if !File.exists? git_path
    puts "can't find .git directory, skipping Git hooks installation"
    return
  end

  hook_path = File.join(git_path, 'hooks')
  if !File.exists? hook_path
    FileUtils.mkdir_p hook_path
  end

  HOOKS.each do |hook_name|
    create_hook(hook_name, hook_path, cwd)
  end

  rusky_setting_file_path = File.join(cwd, '.rusky')
  if !File.exists? rusky_setting_file_path
    File.write(rusky_setting_file_path, '')
  end
rescue => e
  puts "unexpected error happened: #{e.inspect}"
end

.remove_hook(cwd, hook_name) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/rusky.rb', line 126

def self.remove_hook(cwd, hook_name)
  filename = File.join(cwd, '.git', 'hooks', hook_name)
  if File.exists?(filename) && File.read(filename).include?('rusky')
    puts "rusky > removing #{hook_name} hook script..."
    File.delete(filename)
  end
end

.uninstallObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rusky.rb', line 109

def self.uninstall
  cwd = `lsof -p #{Process.ppid} | grep cwd`.split(" ").last

  HOOKS.each do |hook_name|
    remove_hook(cwd, hook_name)
  end

  rusky_setting_file_path = File.join(cwd, '.rusky')
  if File.exists? rusky_setting_file_path
    File.delete(rusky_setting_file_path)
    puts "rusky > removing .rusky file..."
  end

  puts "rusky > uninstall is done. please remove rake tasks for rusky if you have them"
  puts "rusky > Thank you for using rusky!"
end

.write(filename, script) ⇒ Object



70
71
72
73
# File 'lib/rusky.rb', line 70

def self.write(filename, script)
  File.write filename, script
  FileUtils.chmod(0755, filename)
end