Class: Githookify::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/githookify/setup.rb

Constant Summary collapse

HOOKS =
%w[applypatch-msg commit-msg fsmonitor-watchman post-update pre-applypatch pre-commit pre-merge-commit pre-push pre-rebase pre-receive prepare-commit-msg update]

Class Method Summary collapse

Class Method Details

.create_directoriesObject



18
19
20
21
# File 'lib/githookify/setup.rb', line 18

def self.create_directories
  Dir.mkdir('.githooks') unless Dir.exist?('.githooks')
  Dir.mkdir('.githooks/tasks') unless Dir.exist?('.githooks/tasks')
end

.create_rakefileObject



11
12
13
14
15
16
# File 'lib/githookify/setup.rb', line 11

def self.create_rakefile
  unless File.exist?('Rakefile')
    rakefile_content = "task :default\n"
    File.write('Rakefile', rakefile_content)
  end
end

.disable_hooksObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/githookify/setup.rb', line 46

def self.disable_hooks
  unless Dir.exist?('.git')
    puts 'initialise git before enabling git hooks'
  else
    HOOKS.each do |hook|
      hook_path = File.join('.git', 'hooks', hook)
      File.delete(hook_path) if File.exist?(hook_path)
    end
  end
end

.enable_hooksObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/githookify/setup.rb', line 31

def self.enable_hooks
  unless Dir.exist?('.git')
    puts 'initialise git before enabling git hooks'
  else
    HOOKS.each do |hook|
      hook_path = File.join('.git', 'hooks', hook)
      File.open(hook_path, 'w') do |file|
        file.write("#!/bin/bash\n")
        file.write("githookify run #{hook} || exit 1\n")
      end
      File.chmod(0755, hook_path)
    end
  end
end

.installObject



5
6
7
8
9
# File 'lib/githookify/setup.rb', line 5

def self.install
  create_rakefile
  create_directories
  remove_sample_hooks
end

.remove_sample_hooksObject



23
24
25
26
27
28
29
# File 'lib/githookify/setup.rb', line 23

def self.remove_sample_hooks
  dir_path = File.join('.git', 'hooks')
  Dir.foreach(dir_path) do |f|
    fn = File.join(dir_path, f)
    File.delete(fn) if f != '.' && f != '..'
  end
end