Class: Zwischen::Hooks
- Inherits:
-
Object
- Object
- Zwischen::Hooks
- Defined in:
- lib/zwischen/hooks.rb
Constant Summary collapse
- HOOK_MARKER =
"Zwischen pre-push hook"
Class Method Summary collapse
- .handle_existing_hook(hook_path, shell) ⇒ Object
-
.hook_path(project_root = Dir.pwd) ⇒ Object
Resolve the hooks directory through git itself so the hook lands where git will actually execute it — this respects core.hooksPath (husky, pre-commit framework, etc.) and linked worktrees, where .git is a file.
- .install(project_root = Dir.pwd) ⇒ Object
- .installed?(project_root = Dir.pwd) ⇒ Boolean
- .uninstall(project_root = Dir.pwd) ⇒ Object
- .zwischen_hook?(hook_path) ⇒ Boolean
Class Method Details
.handle_existing_hook(hook_path, shell) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/zwischen/hooks.rb', line 62 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
Resolve the hooks directory through git itself so the hook lands where git will actually execute it — this respects core.hooksPath (husky, pre-commit framework, etc.) and linked worktrees, where .git is a file.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zwischen/hooks.rb', line 13 def self.hook_path(project_root = Dir.pwd) stdout, _stderr, status = Open3.capture3( "git", "rev-parse", "--git-path", "hooks", chdir: project_root ) hooks_dir = status.success? ? stdout.strip : nil hooks_dir = File.join(".git", "hooks") if hooks_dir.nil? || hooks_dir.empty? hooks_dir = File.(hooks_dir, project_root) File.join(hooks_dir, "pre-push") rescue StandardError File.join(project_root, ".git", "hooks", "pre-push") end |
.install(project_root = Dir.pwd) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zwischen/hooks.rb', line 37 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
32 33 34 35 |
# File 'lib/zwischen/hooks.rb', line 32 def self.installed?(project_root = Dir.pwd) path = hook_path(project_root) zwischen_hook?(path) end |
.uninstall(project_root = Dir.pwd) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/zwischen/hooks.rb', line 97 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
26 27 28 29 30 |
# File 'lib/zwischen/hooks.rb', line 26 def self.zwischen_hook?(hook_path) return false unless File.exist?(hook_path) File.read(hook_path).include?(HOOK_MARKER) end |