Skip to content

Commit 3fdd3d2

Browse files
committed
Prevent commit of conflict markers
This commit introduces a pre-commit hook that checks for merge conflict markers (e.g. "<<<<<<<", "=======", ">>>>>>>") in staged changes. If any are detected, the hook warns the user and aborts the commit, ensuring that unresolved conflict markers are not inadvertently committed. Reference: https://blog.meain.io/2019/making-sure-you-wont-commit-conflict-markers/ Change-Id: Ia3b058298f9e38227bdbd7a3634ed9b0b855e374
1 parent 6743ed9 commit 3fdd3d2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

scripts/pre-commit.hook

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ if echo "$workspace" | grep -q "[一-龥]"; then
8282
throw "The workspace path '$workspace' contains non-ASCII characters."
8383
fi
8484

85+
# Check for merge conflict markers in staged changes.
86+
# Assemble the conflict marker regex without embedding it directly.
87+
CONFLICT_MARKERS=$(printf '%s|%s|%s' "<<<<<<<" "=======" ">>>>>>>")
88+
# Get staged files that contain conflict markers, but exclude hook files.
89+
CONFLICT_FILES=$(git diff --cached --name-only -G "${CONFLICT_MARKERS}" | \
90+
grep -vE '(^|/)\.git/hooks/|(^|/)(pre-commit|commit-msg|prepare-commit-msg|pre-push)\.hook$')
91+
if [ -n "${CONFLICT_FILES}" ]; then
92+
throw "Conflict markers are still present in the following files:\n%s" ${CONFLICT_FILES}
93+
fi
94+
8595
CLANG_FORMAT=$(which clang-format)
8696
if [ $? -ne 0 ]; then
8797
throw "clang-format not installed. Unable to check source file format policy."

0 commit comments

Comments
 (0)