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