rails_git_hooks 0.5.0 → 0.6.0

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: ec622102778f161f06cc57a88c9a749044554a83a7a8c4abdbcc1e423eb287f3
4
+ data.tar.gz: e03e9d4ca6b32fbb1c29fb40422096bdd013712e564df3b8bbefb6c358703d93
5
5
  SHA512:
6
- metadata.gz: 0d924d665a075d1ab0d4918ed4b9b270df1341acf66b0d2beea8f6763d2d6483e128d1ad59c2896f72381d6bad587beac8a51258043d88286a9120631f083bfa
7
- data.tar.gz: 4e00fdde7a52b52734f2ee919c8f651f2a1740f156ec8c491d4d0081720a4058f6989f46eeb8bd8f4d1fa658e3efd45f1b45e42ac5bfc36cf34f0352aa550a1b
6
+ metadata.gz: 15701ccb101a859a300bec674cf5290fdaa9f86f6fa08555d07d64622b3536cc99b60085b057095753d3ac7d7ae5bbec46f650510b7ccfea7fbcb47b87a40170
7
+ data.tar.gz: c5f315f7fd2ae74107c85f18dab6f344010394a3263fe3e8ab1dcdb4e5ae12f2332a34f15687ba07b08f0fe5e27ef37efa1e750743855d4e90b2a3d3d2b26ec1
data/CHANGELOG.md CHANGED
@@ -4,9 +4,13 @@ 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.0] (latest)
8
8
 
9
- ## [0.5.0] (latest)
9
+ ### Added
10
+
11
+ - **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`.
12
+
13
+ ## [0.5.0]
10
14
 
11
15
  ### Added
12
16
 
data/README.md CHANGED
@@ -64,8 +64,8 @@ Run from your project root (inside a git repo).
64
64
  |---------|-------------|
65
65
  | `rails_git_hooks install [HOOK...] [--jira PROJECT]` | Install hooks. No args = install all. |
66
66
  | `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. |
67
+ | `rails_git_hooks disable HOOK [HOOK...] [whitespace-check]` | Disable hooks (use `*` for all) or the whitespace-check. |
68
+ | `rails_git_hooks enable HOOK [HOOK...] [whitespace-check]` | Re-enable hooks or enable whitespace-check. |
69
69
  | `rails_git_hooks disabled` | Show which hooks are currently disabled. |
70
70
 
71
71
  **Examples**
@@ -85,6 +85,9 @@ rails_git_hooks disable *
85
85
 
86
86
  # Turn them back on
87
87
  rails_git_hooks enable pre-commit
88
+
89
+ # Enable rejection of trailing whitespace and conflict markers in staged files (off by default)
90
+ rails_git_hooks enable whitespace-check
88
91
  ```
89
92
 
90
93
  Disabled state is stored in `.git/rails_git_hooks_disabled` and persists until you run `enable`.
@@ -111,6 +114,7 @@ Set the Jira project key at install time with `--jira PROJECT` or `GIT_HOOKS_JIR
111
114
 
112
115
  1. **Blocks commits on `master` / `main`** — You must commit from a feature branch; direct commits to the default branch are rejected.
113
116
  2. **Runs RuboCop** on staged `.rb` files. If there are offenses, the commit is aborted.
117
+ 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
118
 
115
119
  Requires the `rubocop` gem in your project. If the hook doesn’t run, ensure it’s executable: `chmod +x .git/hooks/pre-commit`.
116
120
 
@@ -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
@@ -68,6 +68,22 @@ module GitHooks
68
68
  hook_names
69
69
  end
70
70
 
71
+ WHITESPACE_CHECK_FILE = 'rails_git_hooks_whitespace_check'
72
+
73
+ def enable_whitespace_check
74
+ path = File.join(@git_dir, WHITESPACE_CHECK_FILE)
75
+ File.write(path, '')
76
+ end
77
+
78
+ def disable_whitespace_check
79
+ path = File.join(@git_dir, WHITESPACE_CHECK_FILE)
80
+ FileUtils.rm_f(path)
81
+ end
82
+
83
+ def whitespace_check_enabled?
84
+ File.exist?(File.join(@git_dir, WHITESPACE_CHECK_FILE))
85
+ end
86
+
71
87
  private
72
88
 
73
89
  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.0'
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Nazarov