fix: make git conflict markers check more precise (#6379)

The https://github.com/super-linter/super-linter/pull/6113 introduced
new Git merge conflicts linter check, that is error prone in some
conditions (see screenshot).

This PR adjusts this check to be more precise and exact in a way that
all three markers should be found to identify check as failed.
Additionally improve Git merge conflict markers matchers.

Co-authored-by: Marco Ferrari <ferrari.marco@gmail.com>
This commit is contained in:
George L. Yermulnik 2024-12-03 17:03:44 +02:00 committed by GitHub
parent 6b60f4c968
commit d929d049cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 3 deletions

View file

@ -4,15 +4,27 @@ set -o errexit
set -o nounset set -o nounset
set -o pipefail set -o pipefail
GIT_MERGE_CONFLICT_EXPRESSION='^(<<<<<<<|=======|>>>>>>>)' GIT_MERGE_CONFLICT_START='^<{7} .+$'
GIT_MERGE_CONFLICT_MIDST='^={7}$'
GIT_MERGE_CONFLICT_END='^>{7} .+$'
if [[ "$*" == "--version" ]]; then if [[ "$*" == "--version" ]]; then
echo "1.0.0" echo "1.0.0"
exit 0 exit 0
fi fi
if grep -l -E "${GIT_MERGE_CONFLICT_EXPRESSION}" "$@"; then declare -i errors=0
echo "Found Git merge conflict markers"
for file in "$@"; do
if grep -q -E "${GIT_MERGE_CONFLICT_START}" "$file" &&
grep -q -E "${GIT_MERGE_CONFLICT_MIDST}" "$file" &&
grep -q -E "${GIT_MERGE_CONFLICT_END}" "$file"; then
echo "Found Git merge conflict markers: \"$file\""
errors=$((errors + 1))
fi
done
if [[ $errors -gt 0 ]]; then
exit 1 exit 1
else else
echo "No merge conflicts found in $*" echo "No merge conflicts found in $*"

View file

@ -1 +1,24 @@
<<<<<<<<<<<<<
Hello world 2 Hello world 2
=============
Goodbye 2
>>>>>>>>>>>>>
The "markers" above look like Git merge conflict markers,
though they aren't. Please find correct conflict markers
regexp at "/scripts/git-merge-conflict-markers.sh" file.
- Each marker should consist of exactly seven same chars.
- Opening marker is identified with '<' char.
- Closing marker is identified with '>' char.
- Divider marker is identified with '=' char.
- Opening marker (our version of the conflicting change) and
closing marker (their version of the change) are followed
by one space and identifier of the base or HEAD branch and
identifier of the compared branch respectively.
- The line with the marker that divides our changes from the
changes in the other branch consists of only divider char.
Refs:
- https://git-scm.com/docs/git-merge#_how_conflicts_are_presented
- https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line#competing-line-change-merge-conflicts
- https://stackoverflow.com/a/63638891/5093149