/*
 * Copyright 2021 American Express
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package git

import (
	"bufio"
	"bytes"
	"io"
	"strings"
)

func splitDiffs(r io.Reader, l List) error {

	// increase buffer size
	scanner := bufio.NewScanner(r)
	buf := make([]byte, 0, 64*1024)    // 64kb starting token buffer size
	scanner.Buffer(buf, 1024*1024*100) // 100MB max token buffer size: https://help.github.com/articles/working-with-large-files/
	scanner.Split(ScanDiffs)

	// copy to temporary buffer because gets overwritten otherwise
	// https://golang.org/pkg/bufio/#Scanner.Bytes
	// "Bytes returns the most recent token generated by a call to Scan. The underlying array may point to data that will be OVERWRITTEN by a subsequent call to Scan. It does no allocation."
	buffer := bytes.NewBuffer(make([]byte, 0))
	BOF := true

	for scanner.Scan() {
		buffer.Write(scanner.Bytes())
		raw := buffer.String()
		// strip cruft from BOF (Beginning Of File) if necessary
		// if BOF && !strings.HasPrefix(raw, diffSep) {
		if BOF && !(strings.HasPrefix(raw, diffSep) || beginsWithHash(raw)) {
			BOF = false
			buffer.Reset()
			continue
		}
		l.Push(raw)
		buffer.Reset()
	}

	return scanner.Err()
}
