fix: Make Git conflict markers check more precise

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.

Repro:
![image](https://github.com/user-attachments/assets/119c9a17-652a-4a6e-88a9-108d347f6f55)
```shell
> grep -E '^(<<<<<<<|=======|>>>>>>>)' README.md
==========================================================
=============   MegaLinter, by OX.security   =============
=========  https://ox.security?ref=megalinter  ===========
==========================================================
```

Replaces https://github.com/super-linter/super-linter/pull/6374
This commit is contained in:
George L. Yermulnik 2024-11-21 19:06:42 +02:00
parent b2d0953bfc
commit 8bdd4a9b49
No known key found for this signature in database
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