From 24f790fb9c671511c48b4fe3e60f23f1b18f8a53 Mon Sep 17 00:00:00 2001
From: Thomas Miceli <tho.miceli@gmail.com>
Date: Sat, 18 Mar 2023 18:22:27 +0100
Subject: [PATCH] Fix truncateCommandOutput function

---
 internal/git/output_parser.go | 36 +++++++++++++++--------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/internal/git/output_parser.go b/internal/git/output_parser.go
index 111ee31..3ba0504 100644
--- a/internal/git/output_parser.go
+++ b/internal/git/output_parser.go
@@ -6,34 +6,28 @@ import (
 )
 
 func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error) {
-	var (
-		buf []byte
-		err error
-	)
+	var buf []byte
+	var err error
 
 	if maxBytes < 0 {
-		// read entire output
 		buf, err = io.ReadAll(out)
-		if err != nil {
-			return "", false, err
-		}
-		return string(buf), false, nil
+	} else {
+		buf, err = io.ReadAll(io.LimitReader(out, maxBytes))
 	}
-
-	// read up to maxBytes bytes
-	buf = make([]byte, maxBytes)
-	n, err := io.ReadFull(out, buf)
-	if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
+	if err != nil {
 		return "", false, err
 	}
-	bytesRead := int64(n)
+	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')
 
-	// find index of last newline character
-	lastNewline := bytes.LastIndexByte(buf, '\n')
-	if lastNewline >= 0 {
-		// truncate buffer to exclude last line
-		buf = buf[:lastNewline]
+		if lastNewline > 0 {
+			// Trim the data buffer up to the last newline character
+			buf = buf[:lastNewline]
+		}
 	}
 
-	return string(buf), bytesRead == maxBytes, nil
+	return string(buf), truncated, nil
 }