package git import ( "bytes" "io" ) func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error) { var buf []byte var err error if maxBytes < 0 { buf, err = io.ReadAll(out) } else { buf, err = io.ReadAll(io.LimitReader(out, maxBytes)) } if err != nil { return "", false, err } truncated := len(buf) >= int(maxBytes) // Remove the last line if it's truncated if truncated { // Find the index of the last newline character lastNewline := bytes.LastIndexByte(buf, '\n') if lastNewline > 0 { // Trim the data buffer up to the last newline character buf = buf[:lastNewline] } } return string(buf), truncated, nil }