Class: Zwischen::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/zwischen/hooks.rb

Constant Summary collapse

HOOK_MARKER =
"Zwischen pre-push hook"

Class Method Summary collapse

Class Method Details

.handle_existing_hook(hook_path, shell) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zwischen/hooks.rb', line 49

def self.handle_existing_hook(hook_path, shell)
  return :skip unless File.exist?(hook_path)
  return :install if zwischen_hook?(hook_path) # Already a Zwischen hook, can overwrite

  shell.say("\n⚠️  A pre-push hook already exists at #{hook_path}", :yellow)
  choice = shell.ask("What would you like to do?", limited_to: %w[backup append skip], default: "backup")

  case choice
  when "backup"
    backup_path = "#{hook_path}.zwischen.backup"
    FileUtils.cp(hook_path, backup_path)
    shell.say("  ✓ Backed up to #{backup_path}", :green)
    :install
  when "append"
    existing_content = File.read(hook_path)
    new_content = <<~APPEND
      #{existing_content}

      # #{HOOK_MARKER} - appended by 'zwischen init'
      if [ "$ZWISCHEN_SKIP" = "1" ]; then
        exit 0
      fi

      zwischen scan --pre-push || exit $?
    APPEND
    File.write(hook_path, new_content)
    File.chmod(0o755, hook_path)
    shell.say("  ✓ Appended Zwischen check to existing hook", :green)
    :skip # Don't install new hook, already appended
  when "skip"
    shell.say("  ↳ Skipping hook installation", :yellow)
    :skip
  end
end

.hook_path(project_root = Dir.pwd) ⇒ Object



9
10
11
# File 'lib/zwischen/hooks.rb', line 9

def self.hook_path(project_root = Dir.pwd)
  File.join(project_root, ".git", "hooks", "pre-push")
end

.install(project_root = Dir.pwd) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zwischen/hooks.rb', line 24

def self.install(project_root = Dir.pwd)
  path = hook_path(project_root)
  hooks_dir = File.dirname(path)

  # Ensure hooks directory exists
  FileUtils.mkdir_p(hooks_dir) unless File.directory?(hooks_dir)

  hook_content = <<~HOOK
    #!/usr/bin/env bash
    # #{HOOK_MARKER} - installed by 'zwischen init'

    if [ "$ZWISCHEN_SKIP" = "1" ]; then
      exit 0
    fi

    zwischen scan --pre-push
    exit $?
  HOOK

  File.write(path, hook_content)
  File.chmod(0o755, path)

  true
end

.installed?(project_root = Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/zwischen/hooks.rb', line 19

def self.installed?(project_root = Dir.pwd)
  path = hook_path(project_root)
  zwischen_hook?(path)
end

.uninstall(project_root = Dir.pwd) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/zwischen/hooks.rb', line 84

def self.uninstall(project_root = Dir.pwd)
  path = hook_path(project_root)
  return false unless File.exist?(path)
  return false unless zwischen_hook?(path)

  File.delete(path)
  true
end

.zwischen_hook?(hook_path) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/zwischen/hooks.rb', line 13

def self.zwischen_hook?(hook_path)
  return false unless File.exist?(hook_path)

  File.read(hook_path).include?(HOOK_MARKER)
end