Class: Committer::Commands::SetupGitHook

Inherits:
Object
  • Object
show all
Defined in:
lib/committer/commands/setup_git_hook.rb

Constant Summary collapse

HOOK_PATH =
'.git/hooks/prepare-commit-msg'

Class Method Summary collapse

Class Method Details

.display_success_messageObject



57
58
59
60
# File 'lib/committer/commands/setup_git_hook.rb', line 57

def self.display_success_message
  puts 'Git hook successfully installed!'
  puts 'Now your commit messages will be automatically generated.'
end

.execute(_args) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/committer/commands/setup_git_hook.rb', line 10

def self.execute(_args)
  validate_git_repository
  install_git_hook
  display_success_message
  exit 0
rescue StandardError => e
  puts "Error: #{e.message}"
  exit 1
end

.install_git_hookObject



50
51
52
53
54
55
# File 'lib/committer/commands/setup_git_hook.rb', line 50

def self.install_git_hook
  template_path = File.expand_path('../prepare-commit-msg', __dir__)
  hook_content = File.read(template_path)
  File.write(HOOK_PATH, hook_content)
  File.chmod(0o755, HOOK_PATH)
end

.validate_git_directory_existsObject



26
27
28
29
30
31
# File 'lib/committer/commands/setup_git_hook.rb', line 26

def self.validate_git_directory_exists
  return if Dir.exist?('.git')

  puts 'Error: Current directory is not a git repository.'
  exit 1
end

.validate_git_repositoryObject



20
21
22
23
24
# File 'lib/committer/commands/setup_git_hook.rb', line 20

def self.validate_git_repository
  validate_git_root
  validate_git_directory_exists
  validate_hook_doesnt_exist
end

.validate_git_rootObject



33
34
35
36
37
38
39
40
# File 'lib/committer/commands/setup_git_hook.rb', line 33

def self.validate_git_root
  git_toplevel = Committer::GitHelper.repo_root
  current_dir = Dir.pwd
  return if git_toplevel == current_dir

  puts 'Error: Please run this command from the root of your git repository.'
  exit 1
end

.validate_hook_doesnt_existObject



42
43
44
45
46
47
48
# File 'lib/committer/commands/setup_git_hook.rb', line 42

def self.validate_hook_doesnt_exist
  return unless File.exist?(HOOK_PATH)

  puts 'Error: prepare-commit-msg hook already exists.'
  puts 'Please remove or rename the existing hook and try again.'
  exit 1
end