Skip to content

Commit ce338ca

Browse files
author
Shane da Silva
committed
Improve regex for MergeConflicts hook
The previous regex could report a false positive if the literal string of seven '<' characters appeared in the middle of a line. Even if this is unlikely, we can easily guard against this by anchoring the regex at the beginning of the line. Change-Id: I79ea46fbf80c75f7e7b4672ef457e6f52c6cd120 Reviewed-on: http://gerrit.causes.com/37944 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane@causes.com>
1 parent 83d8c4c commit ce338ca

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/overcommit/hook/pre_commit/merge_conflicts.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Overcommit::Hook::PreCommit
22
# Checks for unresolved merge conflicts
33
class MergeConflicts < Base
44
def run
5-
result = execute(%w[grep -IHn <<<<<<<] + applicable_files)
5+
result = execute(%w[grep -IHn ^<<<<<<<\s] + applicable_files)
66

77
unless result.stdout.empty?
88
return :bad, "Merge conflict markers detected:\n#{result.stdout}"

spec/overcommit/hook/pre_commit/merge_conflicts_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121

2222
context 'when file contains a merge conflict marker' do
23-
let(:contents) { "Just\n<<<<<<<some\nconflicting text" }
23+
let(:contents) { "Just\n<<<<<<< HEAD:filename.txt\nconflicting text" }
2424

2525
it { should fail_hook }
2626
end
@@ -30,4 +30,10 @@
3030

3131
it { should pass }
3232
end
33+
34+
context "when file contains characters that aren't conflict markers" do
35+
let(:contents) { 'Just some <<<<<<< arrows' }
36+
37+
it { should pass }
38+
end
3339
end

0 commit comments

Comments
 (0)