From 8bdd4a9b49441418aad1d7ad019d3d5f152af04a Mon Sep 17 00:00:00 2001 From: "George L. Yermulnik" Date: Thu, 21 Nov 2024 19:06:42 +0200 Subject: [PATCH] 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 --- scripts/git-merge-conflict-markers.sh | 18 ++++++++++++--- .../git_merge_conflict_markers_good_02.txt | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/scripts/git-merge-conflict-markers.sh b/scripts/git-merge-conflict-markers.sh index c6f72824..0142c667 100755 --- a/scripts/git-merge-conflict-markers.sh +++ b/scripts/git-merge-conflict-markers.sh @@ -4,15 +4,27 @@ set -o errexit set -o nounset 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 echo "1.0.0" exit 0 fi -if grep -l -E "${GIT_MERGE_CONFLICT_EXPRESSION}" "$@"; then - echo "Found Git merge conflict markers" +declare -i errors=0 + +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 else echo "No merge conflicts found in $*" diff --git a/test/linters/git_merge_conflict_markers/git_merge_conflict_markers_good_02.txt b/test/linters/git_merge_conflict_markers/git_merge_conflict_markers_good_02.txt index b64ccf45..0ec5bd5f 100644 --- a/test/linters/git_merge_conflict_markers/git_merge_conflict_markers_good_02.txt +++ b/test/linters/git_merge_conflict_markers/git_merge_conflict_markers_good_02.txt @@ -1 +1,24 @@ +<<<<<<<<<<<<< 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