rails_git_hooks 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ad75b3fc1e2570a89d2feec9dd121d561531ffd323bd039d8376d1aec7250c6
4
- data.tar.gz: 9e4ad0399f71ce3e8bedc120413b4fcbd4a23f5c8bbc0c3fe104c47165728a79
3
+ metadata.gz: '039dc0fb243e8ce670d6cadeb3c3e398cc6441276953c9a0c1e8f8541864256e'
4
+ data.tar.gz: 26a46c6776054ec41554c60ad4ea06f48c14f703cd9c396735f2888b6b0f329b
5
5
  SHA512:
6
- metadata.gz: 0d924d665a075d1ab0d4918ed4b9b270df1341acf66b0d2beea8f6763d2d6483e128d1ad59c2896f72381d6bad587beac8a51258043d88286a9120631f083bfa
7
- data.tar.gz: 4e00fdde7a52b52734f2ee919c8f651f2a1740f156ec8c491d4d0081720a4058f6989f46eeb8bd8f4d1fa658e3efd45f1b45e42ac5bfc36cf34f0352aa550a1b
6
+ metadata.gz: 589952d05b4bbc277efc18f59a02bdd51ba2edeae20b26fb1b093186bdd57413a4949e25987b291b546b06f29b2c81cff394901cd6c6dd9527a1fd1c0f3f0101
7
+ data.tar.gz: d79a92159cd66cf640b1fc80a29ade822fd6e3215be50915aec2609301f7edd71a13b1b83252e4b94adbb95feed759f1dfb78eb80e1f23abd800419992a6a24f
data/CHANGELOG.md CHANGED
@@ -4,9 +4,20 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
 
7
- ## [Unreleased]
7
+ ## [0.6.1] (latest)
8
8
 
9
- ## [0.5.0] (latest)
9
+ ### Changed
10
+
11
+ - **Default install** now installs **commit-msg** and **pre-commit** only (Jira ticket prefix + default-branch protection + RuboCop). Pre-push remains opt-in: `rails_git_hooks install pre-push`.
12
+ - README: quick start and commands table updated for default (commit-msg + pre-commit); Jira project key / `JIRA_PROJECT_KEY` for manual install; pre-push install instruction.
13
+
14
+ ## [0.6.0]
15
+
16
+ ### Added
17
+
18
+ - **Trailing whitespace / conflict markers** check in pre-commit (disabled by default): rejects commits that add trailing spaces/tabs or `<<<<<<<` / `=======` / `>>>>>>>` in staged files. Enable with `rails_git_hooks enable whitespace-check`; disable with `rails_git_hooks disable whitespace-check`.
19
+
20
+ ## [0.5.0]
10
21
 
11
22
  ### Added
12
23
 
data/README.md CHANGED
@@ -41,7 +41,11 @@ bundle install
41
41
  bundle exec rails_git_hooks install
42
42
  ```
43
43
 
44
- **2. (Optional) Set your Jira project key**
44
+ This installs **commit-msg** (Jira ticket prefix) and **pre-commit** (blocks commits on `master`/`main` + RuboCop on staged `.rb` files) by default.
45
+
46
+ **2. Set your Jira project key**
47
+
48
+ Replace the default by passing your project key at install time or via env:
45
49
 
46
50
  ```bash
47
51
  rails_git_hooks install --jira MYPROJ
@@ -50,7 +54,10 @@ export GIT_HOOKS_JIRA_PROJECT=MYPROJ
50
54
  rails_git_hooks install
51
55
  ```
52
56
 
53
- Default is `APD` if not set.
57
+ Default is `APD` if not set. For manual install: replace `JIRA_PROJECT_KEY` in the commit-msg script with your key (e.g. `APD`).
58
+
59
+ To also run the full test suite before push:
60
+ `rails_git_hooks install pre-push`
54
61
 
55
62
  > **Tip:** If the pre-commit hook doesn’t run, make it executable: `chmod +x .git/hooks/pre-commit`
56
63
 
@@ -62,10 +69,10 @@ Run from your project root (inside a git repo).
62
69
 
63
70
  | Command | Description |
64
71
  |---------|-------------|
65
- | `rails_git_hooks install [HOOK...] [--jira PROJECT]` | Install hooks. No args = install all. |
72
+ | `rails_git_hooks install [HOOK...] [--jira PROJECT]` | Install hooks. No args = install default (commit-msg + pre-commit). |
66
73
  | `rails_git_hooks list` | List available hook names. |
67
- | `rails_git_hooks disable HOOK [HOOK...]` | Disable hooks (use `*` for all). |
68
- | `rails_git_hooks enable HOOK [HOOK...]` | Re-enable disabled hooks. |
74
+ | `rails_git_hooks disable HOOK [HOOK...] [whitespace-check]` | Disable hooks (use `*` for all) or the whitespace-check. |
75
+ | `rails_git_hooks enable HOOK [HOOK...] [whitespace-check]` | Re-enable hooks or enable whitespace-check. |
69
76
  | `rails_git_hooks disabled` | Show which hooks are currently disabled. |
70
77
 
71
78
  **Examples**
@@ -85,6 +92,9 @@ rails_git_hooks disable *
85
92
 
86
93
  # Turn them back on
87
94
  rails_git_hooks enable pre-commit
95
+
96
+ # Enable rejection of trailing whitespace and conflict markers in staged files (off by default)
97
+ rails_git_hooks enable whitespace-check
88
98
  ```
89
99
 
90
100
  Disabled state is stored in `.git/rails_git_hooks_disabled` and persists until you run `enable`.
@@ -111,6 +121,7 @@ Set the Jira project key at install time with `--jira PROJECT` or `GIT_HOOKS_JIR
111
121
 
112
122
  1. **Blocks commits on `master` / `main`** — You must commit from a feature branch; direct commits to the default branch are rejected.
113
123
  2. **Runs RuboCop** on staged `.rb` files. If there are offenses, the commit is aborted.
124
+ 3. **Trailing whitespace / conflict markers** (off by default) — When enabled, rejects commits that add trailing spaces/tabs or `<<<<<<<` / `=======` / `>>>>>>>` in staged files. Enable with: `rails_git_hooks enable whitespace-check`. Disable with: `rails_git_hooks disable whitespace-check`.
114
125
 
115
126
  Requires the `rubocop` gem in your project. If the hook doesn’t run, ensure it’s executable: `chmod +x .git/hooks/pre-commit`.
116
127
 
@@ -53,30 +53,36 @@ module GitHooks
53
53
  puts "Available hooks: #{hooks.join(', ')}"
54
54
  end
55
55
 
56
- def run_disable(args)
57
- hooks = args.reject { |a| a.start_with?('-') }
58
- if hooks.empty?
59
- warn 'Usage: rails_git_hooks disable HOOK [HOOK...]'
56
+ def run_disable(args) # rubocop:disable Metrics/AbcSize
57
+ tokens = args.reject { |a| a.start_with?('-') }
58
+ if tokens.empty?
59
+ warn 'Usage: rails_git_hooks disable HOOK [HOOK...] [whitespace-check]'
60
60
  warn "Use '*' to disable all hooks."
61
61
  exit 1
62
62
  end
63
63
  installer = Installer.new
64
- installer.disable(*hooks)
65
- puts "Disabled: #{hooks.join(', ')}"
64
+ hook_names = tokens - ['whitespace-check']
65
+ installer.disable_whitespace_check if tokens.include?('whitespace-check')
66
+ installer.disable(*hook_names) if hook_names.any?
67
+ disabled = hook_names + (tokens.include?('whitespace-check') ? ['whitespace-check'] : [])
68
+ puts "Disabled: #{disabled.join(', ')}"
66
69
  rescue GitHooks::Error => e
67
70
  warn "Error: #{e.message}"
68
71
  exit 1
69
72
  end
70
73
 
71
74
  def run_enable(args)
72
- hooks = args.reject { |a| a.start_with?('-') }
73
- if hooks.empty?
74
- warn 'Usage: rails_git_hooks enable HOOK [HOOK...]'
75
+ tokens = args.reject { |a| a.start_with?('-') }
76
+ if tokens.empty?
77
+ warn 'Usage: rails_git_hooks enable HOOK [HOOK...] [whitespace-check]'
75
78
  exit 1
76
79
  end
77
80
  installer = Installer.new
78
- installer.enable(*hooks)
79
- puts "Enabled: #{hooks.join(', ')}"
81
+ hook_names = tokens - ['whitespace-check']
82
+ installer.enable_whitespace_check if tokens.include?('whitespace-check')
83
+ installer.enable(*hook_names) if hook_names.any?
84
+ enabled = hook_names + (tokens.include?('whitespace-check') ? ['whitespace-check'] : [])
85
+ puts "Enabled: #{enabled.join(', ')}"
80
86
  rescue GitHooks::Error => e
81
87
  warn "Error: #{e.message}"
82
88
  exit 1
@@ -101,24 +107,25 @@ module GitHooks
101
107
 
102
108
  Usage:
103
109
  rails_git_hooks install [HOOK...] [--jira PROJECT_KEY]
104
- rails_git_hooks disable HOOK [HOOK...] (use * for all)
105
- rails_git_hooks enable HOOK [HOOK...]
110
+ rails_git_hooks disable HOOK [HOOK...] [whitespace-check] (use * for all hooks)
111
+ rails_git_hooks enable HOOK [HOOK...] [whitespace-check]
106
112
  rails_git_hooks disabled
107
113
  rails_git_hooks list
108
114
  rails_git_hooks --help
109
115
 
110
116
  Commands:
111
- install Install hooks into current repo's .git/hooks.
112
- disable Disable hooks (they no-op until enabled).
113
- enable Re-enable disabled hooks.
114
- disabled List currently disabled hooks.
115
- list List available hook names.
117
+ install Install hooks into current repo's .git/hooks.
118
+ disable Disable hooks or whitespace-check (trailing ws/conflict markers in pre-commit).
119
+ enable Re-enable disabled hooks or enable whitespace-check.
120
+ disabled List currently disabled hooks.
121
+ list List available hook names.
116
122
 
117
123
  Examples:
118
124
  rails_git_hooks install
119
125
  rails_git_hooks disable pre-commit
120
- rails_git_hooks disable * # disable all
126
+ rails_git_hooks disable * # disable all hooks
121
127
  rails_git_hooks enable pre-commit
128
+ rails_git_hooks enable whitespace-check # reject trailing ws/conflict markers (off by default)
122
129
  rails_git_hooks install commit-msg pre-commit --jira MYPROJ
123
130
  HELP
124
131
  end
@@ -3,6 +3,9 @@
3
3
  module GitHooks
4
4
  class Installer
5
5
  HOOKS_DIR = File.expand_path('templates', __dir__).freeze
6
+ # Default: Jira commit-msg + pre-commit (default-branch protection + RuboCop).
7
+ # Use install(*available_hooks) or pass names for more.
8
+ DEFAULT_HOOKS = %w[commit-msg pre-commit].freeze
6
9
 
7
10
  def initialize(git_dir: nil, jira_project: nil)
8
11
  @git_dir = git_dir || find_git_dir
@@ -13,7 +16,7 @@ module GitHooks
13
16
  target_dir = File.join(@git_dir, 'hooks')
14
17
  raise GitHooks::Error, "Not a git repository or .git/hooks not found: #{@git_dir}" unless Dir.exist?(target_dir)
15
18
 
16
- hooks = hook_names.empty? ? available_hooks : hook_names
19
+ hooks = hook_names.empty? ? DEFAULT_HOOKS : hook_names
17
20
  installed = []
18
21
 
19
22
  hooks.each do |name|
@@ -68,6 +71,22 @@ module GitHooks
68
71
  hook_names
69
72
  end
70
73
 
74
+ WHITESPACE_CHECK_FILE = 'rails_git_hooks_whitespace_check'
75
+
76
+ def enable_whitespace_check
77
+ path = File.join(@git_dir, WHITESPACE_CHECK_FILE)
78
+ File.write(path, '')
79
+ end
80
+
81
+ def disable_whitespace_check
82
+ path = File.join(@git_dir, WHITESPACE_CHECK_FILE)
83
+ FileUtils.rm_f(path)
84
+ end
85
+
86
+ def whitespace_check_enabled?
87
+ File.exist?(File.join(@git_dir, WHITESPACE_CHECK_FILE))
88
+ end
89
+
71
90
  private
72
91
 
73
92
  def find_git_dir
@@ -14,6 +14,27 @@ if File.exist?(disabled_file)
14
14
  exit 0 if disabled.include?('*') || disabled.include?('pre-commit')
15
15
  end
16
16
 
17
+ # Trailing whitespace / conflict markers check (disabled by default; enable with .git/rails_git_hooks_whitespace_check)
18
+ whitespace_check_file = File.join(git_dir, 'rails_git_hooks_whitespace_check')
19
+ if File.exist?(whitespace_check_file)
20
+ staged = `git diff --cached --name-only`.split("\n").map(&:strip).reject(&:empty?)
21
+ errors = []
22
+ staged.each do |path|
23
+ next unless File.file?(path)
24
+
25
+ File.read(path).lines.each_with_index do |line, i|
26
+ errors << "#{path}:#{i + 1}: trailing whitespace" if line.match?(/[ \t]\z/)
27
+ stripped = line.strip
28
+ errors << "#{path}:#{i + 1}: conflict marker" if stripped.start_with?('<<<<<<<', '=======', '>>>>>>>')
29
+ end
30
+ end
31
+ unless errors.empty?
32
+ warn 'Commit rejected (whitespace/conflict check):'
33
+ errors.uniq.each { |e| warn " #{e}" }
34
+ exit 1
35
+ end
36
+ end
37
+
17
38
  # Prevent commits on default branch (master/main)
18
39
  branch = `git rev-parse --abbrev-ref HEAD`.strip
19
40
  if %w[master main].include?(branch)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GitHooks
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_git_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Nazarov