Skip to content

Commit 1162272

Browse files
committed
DRY up SubmoduleStatus hooks into single shared module
We have a couple of hooks that are duplicated across multiple different hook types. Rather than force this duplication, we extract the shared code into a module and store it under `Overcommit::Hook::Shared` namespace.
1 parent 39b643b commit 1162272

File tree

7 files changed

+44
-92
lines changed

7 files changed

+44
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1+
require 'overcommit/hook/shared/submodule_status'
2+
13
module Overcommit::Hook::PostCheckout
24
# Checks the status of submodules in the current repository and
35
# notifies the user if any are uninitialized, out of date with
46
# the current index, or contain merge conflicts.
57
class SubmoduleStatus < Base
6-
def run
7-
messages = []
8-
submodule_statuses.each do |submodule_status|
9-
path = submodule_status.path
10-
if submodule_status.uninitialized?
11-
messages << "Submodule #{path} is uninitialized."
12-
elsif submodule_status.outdated?
13-
messages << "Submodule #{path} is out of date with the current index."
14-
elsif submodule_status.merge_conflict?
15-
messages << "Submodule #{path} has merge conflicts."
16-
end
17-
end
18-
19-
return :pass if messages.empty?
20-
21-
[:warn, messages.join("\n")]
22-
end
23-
24-
private
25-
26-
def submodule_statuses
27-
Overcommit::GitRepo.submodule_statuses(recursive: config['recursive'])
28-
end
8+
include Overcommit::Hook::Shared::SubmoduleStatus
299
end
3010
end
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1+
require 'overcommit/hook/shared/submodule_status'
2+
13
module Overcommit::Hook::PostCommit
24
# Checks the status of submodules in the current repository and
35
# notifies the user if any are uninitialized, out of date with
46
# the current index, or contain merge conflicts.
57
class SubmoduleStatus < Base
6-
def run
7-
messages = []
8-
submodule_statuses.each do |submodule_status|
9-
path = submodule_status.path
10-
if submodule_status.uninitialized?
11-
messages << "Submodule #{path} is uninitialized."
12-
elsif submodule_status.outdated?
13-
messages << "Submodule #{path} is out of date with the current index."
14-
elsif submodule_status.merge_conflict?
15-
messages << "Submodule #{path} has merge conflicts."
16-
end
17-
end
18-
19-
return :pass if messages.empty?
20-
21-
[:warn, messages.join("\n")]
22-
end
23-
24-
private
25-
26-
def submodule_statuses
27-
Overcommit::GitRepo.submodule_statuses(recursive: config['recursive'])
28-
end
8+
include Overcommit::Hook::Shared::SubmoduleStatus
299
end
3010
end
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1+
require 'overcommit/hook/shared/submodule_status'
2+
13
module Overcommit::Hook::PostMerge
24
# Checks the status of submodules in the current repository and
35
# notifies the user if any are uninitialized, out of date with
46
# the current index, or contain merge conflicts.
57
class SubmoduleStatus < Base
6-
def run
7-
messages = []
8-
submodule_statuses.each do |submodule_status|
9-
path = submodule_status.path
10-
if submodule_status.uninitialized?
11-
messages << "Submodule #{path} is uninitialized."
12-
elsif submodule_status.outdated?
13-
messages << "Submodule #{path} is out of date with the current index."
14-
elsif submodule_status.merge_conflict?
15-
messages << "Submodule #{path} has merge conflicts."
16-
end
17-
end
18-
19-
return :pass if messages.empty?
20-
21-
[:warn, messages.join("\n")]
22-
end
23-
24-
private
25-
26-
def submodule_statuses
27-
Overcommit::GitRepo.submodule_statuses(recursive: config['recursive'])
28-
end
8+
include Overcommit::Hook::Shared::SubmoduleStatus
299
end
3010
end
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1+
require 'overcommit/hook/shared/submodule_status'
2+
13
module Overcommit::Hook::PostRewrite
24
# Checks the status of submodules in the current repository and
35
# notifies the user if any are uninitialized, out of date with
46
# the current index, or contain merge conflicts.
57
class SubmoduleStatus < Base
6-
def run
7-
messages = []
8-
submodule_statuses.each do |submodule_status|
9-
path = submodule_status.path
10-
if submodule_status.uninitialized?
11-
messages << "Submodule #{path} is uninitialized."
12-
elsif submodule_status.outdated?
13-
messages << "Submodule #{path} is out of date with the current index."
14-
elsif submodule_status.merge_conflict?
15-
messages << "Submodule #{path} has merge conflicts."
16-
end
17-
end
18-
19-
return :pass if messages.empty?
20-
21-
[:warn, messages.join("\n")]
22-
end
23-
24-
private
25-
26-
def submodule_statuses
27-
Overcommit::GitRepo.submodule_statuses(recursive: config['recursive'])
28-
end
8+
include Overcommit::Hook::Shared::SubmoduleStatus
299
end
3010
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Overcommit::Hook::Shared
2+
# Shared code used by all `SubmoduleStatus` hooks to notify the user if any
3+
# submodules are uninitialized, out of date with the current index, or contain
4+
# merge conflicts.
5+
module SubmoduleStatus
6+
def run
7+
messages = []
8+
submodule_statuses.each do |submodule_status|
9+
path = submodule_status.path
10+
if submodule_status.uninitialized?
11+
messages << "Submodule #{path} is uninitialized."
12+
elsif submodule_status.outdated?
13+
messages << "Submodule #{path} is out of date with the current index."
14+
elsif submodule_status.merge_conflict?
15+
messages << "Submodule #{path} has merge conflicts."
16+
end
17+
end
18+
19+
return :pass if messages.empty?
20+
21+
[:warn, messages.join("\n")]
22+
end
23+
24+
private
25+
26+
def submodule_statuses
27+
Overcommit::GitRepo.submodule_statuses(recursive: config['recursive'])
28+
end
29+
end
30+
end

lib/overcommit/utils.rb

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def camel_case(str)
9191
def supported_hook_types
9292
Dir[File.join(HOOK_DIRECTORY, '*')].
9393
select { |file| File.directory?(file) }.
94+
reject { |file| File.basename(file) == 'shared' }.
9495
map { |file| File.basename(file).gsub('_', '-') }
9596
end
9697

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
hook_types = Dir[File.join(Overcommit::HOOK_DIRECTORY, '*')].
99
select { |f| File.directory?(f) }.
10+
reject { |f| File.basename(f) == 'shared' }.
1011
sort
1112

1213
hook_types.each do |hook_type|

0 commit comments

Comments
 (0)