Skip to content

Commit e151924

Browse files
authored
Merge pull request #223 from sysprog21/prevent-merge-conflicts
Prevent commit of conflict markers
2 parents 6743ed9 + 3fdd3d2 commit e151924

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)