rails_git_hooks 0.4.1 → 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: 94f388ff518dfa6ca8a80d6a4cfaac1639682e7a8e2535da52929bc5ea34862b
4
- data.tar.gz: 7bb6f56263591ffc1925fee536341e4b2c7c29e6c0cef865779a0d73ac796b6a
3
+ metadata.gz: ec622102778f161f06cc57a88c9a749044554a83a7a8c4abdbcc1e423eb287f3
4
+ data.tar.gz: e03e9d4ca6b32fbb1c29fb40422096bdd013712e564df3b8bbefb6c358703d93
5
5
  SHA512:
6
- metadata.gz: 4e77a5d7f9b321a34e1cceb269ae590c6ffd8db816794fac5a775090614f46c58b275429a106046459df8407f6344a4dbf4c1421051891ac02ad73981b3177af
7
- data.tar.gz: 1bb5be50035416d37ea8b3a256753c47dd21fd92baadb3e0063c18c22149ee1a94cb0f7945a3075fdc3ba7707da1c3fab4b4108c0bc5d1fbf4c08535eae525cf
6
+ metadata.gz: 15701ccb101a859a300bec674cf5290fdaa9f86f6fa08555d07d64622b3536cc99b60085b057095753d3ac7d7ae5bbec46f650510b7ccfea7fbcb47b87a40170
7
+ data.tar.gz: c5f315f7fd2ae74107c85f18dab6f344010394a3263fe3e8ab1dcdb4e5ae12f2332a34f15687ba07b08f0fe5e27ef37efa1e750743855d4e90b2a3d3d2b26ec1
data/CHANGELOG.md CHANGED
@@ -4,7 +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
- ## [0.4.1] - (latest)
7
+ ## [0.6.0] (latest)
8
+
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]
14
+
15
+ ### Added
16
+
17
+ - **pre-push** hook: runs `bundle exec rspec` before every push; aborts push if tests fail. Respects `rails_git_hooks_disabled`.
18
+ - RuboCop `Lint/ScriptPermission` exclusion for `hooks/*` (same as templates).
19
+
20
+ ## [0.4.1]
8
21
 
9
22
  ### Changed
10
23
 
data/README.md CHANGED
@@ -14,6 +14,7 @@
14
14
  |------|--------------|
15
15
  | **commit-msg** | Prepends `[TICKET-123]` to commit messages when your branch name contains a Jira ticket. |
16
16
  | **pre-commit** | Blocks commits to `master`/`main` and runs RuboCop on staged `.rb` files. |
17
+ | **pre-push** | Runs the full test suite (`bundle exec rspec`) before push; aborts push if tests fail. |
17
18
 
18
19
  Hooks can be disabled temporarily (e.g. for quick WIP commits or CI) without uninstalling.
19
20
 
@@ -63,8 +64,8 @@ Run from your project root (inside a git repo).
63
64
  |---------|-------------|
64
65
  | `rails_git_hooks install [HOOK...] [--jira PROJECT]` | Install hooks. No args = install all. |
65
66
  | `rails_git_hooks list` | List available hook names. |
66
- | `rails_git_hooks disable HOOK [HOOK...]` | Disable hooks (use `*` for all). |
67
- | `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. |
68
69
  | `rails_git_hooks disabled` | Show which hooks are currently disabled. |
69
70
 
70
71
  **Examples**
@@ -84,6 +85,9 @@ rails_git_hooks disable *
84
85
 
85
86
  # Turn them back on
86
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
87
91
  ```
88
92
 
89
93
  Disabled state is stored in `.git/rails_git_hooks_disabled` and persists until you run `enable`.
@@ -110,11 +114,20 @@ Set the Jira project key at install time with `--jira PROJECT` or `GIT_HOOKS_JIR
110
114
 
111
115
  1. **Blocks commits on `master` / `main`** — You must commit from a feature branch; direct commits to the default branch are rejected.
112
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`.
113
118
 
114
119
  Requires the `rubocop` gem in your project. If the hook doesn’t run, ensure it’s executable: `chmod +x .git/hooks/pre-commit`.
115
120
 
116
121
  ---
117
122
 
123
+ ### pre-push — Run tests before push
124
+
125
+ Runs `bundle exec rspec` before every `git push`. If the test suite fails, the push is aborted so you don’t break CI.
126
+
127
+ Requires the `rspec` gem (or for Minitest, edit the hook to use `bundle exec rake test`). If the hook doesn’t run, ensure it’s executable: `chmod +x .git/hooks/pre-push`.
128
+
129
+ ---
130
+
118
131
  ## Manual installation (without the gem)
119
132
 
120
133
  If you don’t want to use the gem, copy the scripts from `hooks/` into `.git/hooks`. The `hooks/` directory is synced from the gem templates via `rake sync_hooks` in development.
@@ -123,6 +136,7 @@ If you don’t want to use the gem, copy the scripts from `hooks/` into `.git/ho
123
136
  |------|--------|
124
137
  | [commit-msg](hooks/commit-msg) | Replace `JIRA_PROJECT_KEY` in the script with your Jira project key (e.g. `APD`). |
125
138
  | [pre-commit](hooks/pre-commit) | Requires the `rubocop` gem in the repo where you use it. |
139
+ | [pre-push](hooks/pre-push) | Runs `bundle exec rspec`; edit to use `bundle exec rake test` for Minitest. |
126
140
 
127
141
  ---
128
142
 
@@ -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)
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # Runs the full test suite before push. Push is aborted if tests fail.
6
+ # Uses `bundle exec rspec`. For Minitest, edit to use `bundle exec rake test`.
7
+
8
+ # Git runs hooks with CWD = repo root; find .git from here
9
+ repo_root = Dir.pwd
10
+ git_dir = File.join(repo_root, '.git')
11
+ git_dir = File.expand_path(File.read(git_dir).strip.sub(/\Agitdir: \s*/, ''), repo_root) if File.file?(git_dir)
12
+ disabled_file = File.join(git_dir, 'rails_git_hooks_disabled')
13
+ if File.exist?(disabled_file)
14
+ disabled = File.read(disabled_file).split("\n").map(&:strip)
15
+ exit 0 if disabled.include?('*') || disabled.include?('pre-push')
16
+ end
17
+
18
+ require 'english'
19
+
20
+ system('bundle exec rspec')
21
+ exit $CHILD_STATUS.to_s[-1].to_i
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GitHooks
4
- VERSION = '0.4.1'
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.4.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Nazarov
@@ -51,8 +51,7 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.0'
54
- description: Installs commit-msg (Jira ticket prefix) and pre-commit (RuboCop) hooks
55
- into your git repository.
54
+ description: Installs most useful git hooks for Rails and Ruby projects
56
55
  email:
57
56
  - nikenor11@gmail.com
58
57
  executables:
@@ -69,6 +68,7 @@ files:
69
68
  - lib/rails_git_hooks/installer.rb
70
69
  - lib/rails_git_hooks/templates/commit-msg
71
70
  - lib/rails_git_hooks/templates/pre-commit
71
+ - lib/rails_git_hooks/templates/pre-push
72
72
  - lib/rails_git_hooks/version.rb
73
73
  homepage: https://github.com/NikitaNazarov1/rails_git_hooks
74
74
  licenses: