Recognize conflict markers that have no label

Git only writes the space after a marker when there is a label to write
after it, and the label can be empty: `git checkout -m` with the diff3
conflict style, for instance, has no name for the common ancestor, so it
writes a bare "|||||||" line.
This commit is contained in:
Stefan Haller
2026-08-08 12:43:42 +02:00
parent 5481436d8c
commit d0078bf05c
2 changed files with 21 additions and 2 deletions
+4 -2
View File
@@ -108,10 +108,12 @@ func hasMarkerPrefix[T string | []byte](line T, markerChar byte, markerSize int)
}
// A start, ancestor or end marker is followed by a space and a label, e.g.
// "<<<<<<< HEAD".
// "<<<<<<< HEAD". The label can be missing though, in which case git doesn't
// write the space either; `git checkout -m` with the diff3 conflict style does
// that for the ancestor marker, for example.
func isConflictMarker[T string | []byte](line T, markerChar byte, markerSize int) bool {
return hasMarkerPrefix(line, markerChar, markerSize) &&
len(line) > markerSize && line[markerSize] == ' '
(len(line) == markerSize || line[markerSize] == ' ')
}
// The marker separating the two sides of a conflict never has a label after it.
@@ -61,6 +61,19 @@ func TestDetermineLineType(t *testing.T) {
line: "<<<<<<<<",
expected: NOT_A_MARKER,
},
// Markers without a label
{
line: "<<<<<<<",
expected: START,
},
{
line: "|||||||",
expected: ANCESTOR,
},
{
line: ">>>>>>>",
expected: END,
},
{
line: strings.Repeat("<", 32) + " HEAD",
markerSize: 32,
@@ -136,6 +149,10 @@ func TestFindConflictsAux(t *testing.T) {
content: " <<<<<<< ",
expected: false,
},
{
content: ">>>>>>>",
expected: true,
},
{
content: "a\nb\nc\n<<<<<<< ",
expected: true,