1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/mergeconflicts/find_conflicts.go

72 lines
1.4 KiB
Go
Raw Normal View History

2021-05-30 15:22:04 +10:00
package mergeconflicts
import (
"strings"
"github.com/jesseduffield/lazygit/pkg/utils"
)
// LineType tells us whether a given line is a start/middle/end marker of a conflict,
// or if it's not a marker at all
type LineType int
const (
START LineType = iota
ANCESTOR
TARGET
2021-05-30 15:22:04 +10:00
END
NOT_A_MARKER
)
func findConflicts(content string) []*mergeConflict {
conflicts := make([]*mergeConflict, 0)
if content == "" {
return conflicts
}
var newConflict *mergeConflict
for i, line := range utils.SplitLines(content) {
switch determineLineType(line) {
case START:
newConflict = &mergeConflict{start: i, ancestor: -1}
case ANCESTOR:
2021-08-21 18:34:30 +09:00
if newConflict != nil {
newConflict.ancestor = i
}
case TARGET:
if newConflict != nil {
newConflict.target = i
2021-08-21 18:34:30 +09:00
}
2021-05-30 15:22:04 +10:00
case END:
2021-08-21 18:34:30 +09:00
if newConflict != nil {
newConflict.end = i
conflicts = append(conflicts, newConflict)
}
2021-06-02 19:53:01 +02:00
// reset value to avoid any possible silent mutations in further iterations
newConflict = nil
2021-05-30 15:22:04 +10:00
default:
// line isn't a merge conflict marker so we just continue
}
}
return conflicts
}
func determineLineType(line string) LineType {
trimmedLine := strings.TrimPrefix(line, "++")
2021-06-05 13:30:55 +10:00
switch {
case strings.HasPrefix(trimmedLine, "<<<<<<< "):
return START
case strings.HasPrefix(trimmedLine, "||||||| "):
return ANCESTOR
2021-06-05 13:30:55 +10:00
case trimmedLine == "=======":
return TARGET
2021-06-05 13:30:55 +10:00
case strings.HasPrefix(trimmedLine, ">>>>>>> "):
return END
default:
return NOT_A_MARKER
2021-05-30 15:22:04 +10:00
}
}