Skip to content

Commit e0d5e44

Browse files
committed
Add support for post-rewrite hooks
Add the PostRewrite hook context and base hook so that post-rewrite hooks can be written. We've purposely not implemented the reading of the lines passed to STDIN, as it's not clear what kind of API would be useful here. Since the only use of this right now is as a trigger for re-indexing tags, we'll pass on implementing reading STDIN until a use case arises. Change-Id: I6047c820e5f792d1bab5ef56971f9403e3cf022e Reviewed-on: http://gerrit.causes.com/46988 Tested-by: jenkins <jenkins@brigade.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 205729f commit e0d5e44

File tree

7 files changed

+77
-3
lines changed

7 files changed

+77
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
`required_executable` option on the JsHint and Jscs pre-commit hooks
1111
* Add pre-commit hooks for [standard](https://github.com/feross/standard) and
1212
[semistandard](https://github.com/Flet/semistandard) JavaScript linters
13-
* Add support for `post-commit` and `post-merge` hooks
13+
* Add support for `post-commit`, `post-merge`, and `post-rewrite` hooks
1414
* Add `GitGuilt` `post-commit` hook to display changes in blame ownership for
1515
modified files
1616
* Change behavior of configuration options containing array values to always

config/default.yml

+6
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,9 @@ PostCommit:
306306
required_executable: 'git-guilt'
307307
flags: ['HEAD~', 'HEAD']
308308
install_command: 'npm install -g git-guilt'
309+
310+
# Hooks that run every time a commit is modified by an amend or rebase.
311+
PostRewrite:
312+
ALL:
313+
requires_files: false
314+
quiet: false
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'forwardable'
2+
3+
module Overcommit::Hook::PostRewrite
4+
# Functionality common to all post-rewrite hooks.
5+
class Base < Overcommit::Hook::Base
6+
extend Forwardable
7+
8+
def_delegators :@context, :amend?, :rebase?
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Overcommit::HookContext
2+
# Contains helpers for contextual information used by post-rewrite hooks.
3+
class PostRewrite < Base
4+
# Returns whether this post-rewrite was triggered by `git commit --amend`.
5+
#
6+
# @return [true,false]
7+
def amend?
8+
@args[0] == 'amend'
9+
end
10+
11+
# Returns whether this post-rewrite was triggered by `git rebase`.
12+
#
13+
# @return [true,false]
14+
def rebase?
15+
@args[0] == 'rebase'
16+
end
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
require 'overcommit/hook_context/post_rewrite'
3+
4+
describe Overcommit::HookContext::PostRewrite do
5+
let(:config) { double('config') }
6+
let(:context) { described_class.new(config, args) }
7+
8+
describe '#amend?' do
9+
subject { context.amend? }
10+
11+
context 'when rewrite was triggered by amend' do
12+
let(:args) { ['amend'] }
13+
14+
it { should == true }
15+
end
16+
17+
context 'when rewrite was triggered by rebase' do
18+
let(:args) { ['rebase'] }
19+
20+
it { should == false }
21+
end
22+
end
23+
24+
describe '#rebase?' do
25+
subject { context.rebase? }
26+
27+
context 'when rewrite was triggered by amend' do
28+
let(:args) { ['amend'] }
29+
30+
it { should == false }
31+
end
32+
33+
context 'when rewrite was triggered by rebase' do
34+
let(:args) { ['rebase'] }
35+
36+
it { should == true }
37+
end
38+
end
39+
end

spec/overcommit/utils_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@
108108
describe '.supported_hook_types' do
109109
subject { described_class.supported_hook_types }
110110

111-
it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge] }
111+
it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite] }
112112
end
113113

114114
describe '.supported_hook_type_classes' do
115115
subject { described_class.supported_hook_type_classes }
116116

117-
it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge] }
117+
it { should =~ %w[CommitMsg PreCommit PostCheckout PostCommit PostMerge PostRewrite] }
118118
end
119119

120120
describe '.execute' do

template-dir/hooks/post-rewrite

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
overcommit-hook

0 commit comments

Comments
 (0)