func Parse()

in internal/git/conflict/parser.go [88:201]


func Parse(src io.Reader, ancestor, our, their *Entry) (File, error) {
	parentPath := our.Path
	if ancestor != nil {
		parentPath = ancestor.Path
	}

	var (
		// conflict markers
		start  = "<<<<<<< " + our.Path
		middle = "======="
		end    = ">>>>>>> " + their.Path

		f = File{
			path:     parentPath,
			ancestor: ancestor,
			our:      our,
			their:    their,
		}

		objIndex, oldIndex, newIndex uint = 0, 1, 1
		currentSection               section
		bytesRead                    int

		s = bufio.NewScanner(src)
	)

	s.Buffer(make([]byte, 4096), fileLimit) // allow for line scanning up to the file limit

	s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
		defer func() { bytesRead += advance }()

		if bytesRead >= fileLimit {
			return 0, nil, ErrUnmergeableFile
		}

		// The remaining function is a modified version of
		// bufio.ScanLines that does not consume carriage returns
		if atEOF && len(data) == 0 {
			return 0, nil, nil
		}
		if i := bytes.IndexByte(data, '\n'); i >= 0 {
			// We have a full newline-terminated line.
			return i + 1, data[0:i], nil
		}
		if atEOF {
			return len(data), data, nil
		}
		return 0, nil, nil
	})

	for s.Scan() {
		switch l := s.Text(); l {
		case start:
			if currentSection != sectionNone {
				return File{}, ErrUnexpectedDelimiter
			}
			currentSection = sectionNew
		case middle:
			if currentSection != sectionNew {
				return File{}, ErrUnexpectedDelimiter
			}
			currentSection = sectionOld
		case end:
			if currentSection != sectionOld {
				return File{}, ErrUnexpectedDelimiter
			}
			currentSection = sectionNone
		default:
			if len(l) > 0 && l[0] == '\\' {
				currentSection = sectionNoNewline
				f.lines = append(f.lines, line{
					objIndex: objIndex,
					oldIndex: oldIndex,
					newIndex: newIndex,
					payload:  l,
					section:  currentSection,
				})
				continue
			}
			f.lines = append(f.lines, line{
				objIndex: objIndex,
				oldIndex: oldIndex,
				newIndex: newIndex,
				payload:  l,
				section:  currentSection,
			})

			objIndex++
			if currentSection != sectionNew {
				oldIndex++
			}
			if currentSection != sectionOld {
				newIndex++
			}
		}
	}

	if err := s.Err(); err != nil {
		if errors.Is(err, bufio.ErrTooLong) {
			return File{}, ErrUnmergeableFile
		}
		return File{}, err
	}

	if currentSection == sectionOld || currentSection == sectionNew {
		return File{}, ErrMissingEndDelimiter
	}

	if bytesRead == 0 {
		return File{}, ErrUnmergeableFile // typically a binary file
	}

	return f, nil
}