This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.rb
More file actions
133 lines (107 loc) · 3.63 KB
/
hooks.rb
File metadata and controls
133 lines (107 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Hooks
include Output
HOOK_SCRIPT = <<~SCRIPT
#!/bin/sh
# git-pkgs auto-update hook
git pkgs update 2>/dev/null || true
SCRIPT
HOOKS = %w[post-commit post-merge].freeze
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
if @options[:install]
install_hooks(repo)
elsif @options[:uninstall]
uninstall_hooks(repo)
else
show_status(repo)
end
end
def install_hooks(repo, quiet: false)
hooks_dir = File.join(repo.git_dir, "hooks")
installed = []
HOOKS.each do |hook_name|
hook_path = File.join(hooks_dir, hook_name)
if File.exist?(hook_path)
content = File.read(hook_path)
next if content.include?("git-pkgs")
File.open(hook_path, "a") do |f|
f.puts "\n# git-pkgs auto-update"
f.puts "git pkgs update 2>/dev/null || true"
end
installed << hook_name
else
File.write(hook_path, HOOK_SCRIPT)
File.chmod(0o755, hook_path)
installed << hook_name
end
end
return if quiet || installed.empty?
info "Installed hooks: #{installed.join(', ')}"
end
def uninstall_hooks(repo)
hooks_dir = File.join(repo.git_dir, "hooks")
HOOKS.each do |hook_name|
hook_path = File.join(hooks_dir, hook_name)
next unless File.exist?(hook_path)
content = File.read(hook_path)
if content.strip == HOOK_SCRIPT.strip
File.delete(hook_path)
info "Removed #{hook_name} hook"
elsif content.include?("git-pkgs")
new_content = content.lines.reject { |line|
line.include?("git-pkgs") || line.include?("git pkgs")
}.join
new_content = new_content.gsub(/\n# git-pkgs auto-update\n/, "\n")
if new_content.strip.empty? || new_content.strip == "#!/bin/sh"
File.delete(hook_path)
info "Removed #{hook_name} hook"
else
File.write(hook_path, new_content)
info "Removed git-pkgs from #{hook_name} hook"
end
end
end
info "Hooks uninstalled successfully"
end
def show_status(repo)
hooks_dir = File.join(repo.git_dir, "hooks")
puts "Hook status:"
HOOKS.each do |hook_name|
hook_path = File.join(hooks_dir, hook_name)
if File.exist?(hook_path) && File.read(hook_path).include?("git-pkgs")
puts " #{hook_name}: installed"
else
puts " #{hook_name}: not installed"
end
end
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs hooks [options]"
opts.on("-i", "--install", "Install git hooks for auto-updating") do
options[:install] = true
end
opts.on("-u", "--uninstall", "Remove git hooks") do
options[:uninstall] = true
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end