2023-03-18 15:18:24 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error) {
|
2023-03-18 17:22:27 +00:00
|
|
|
var buf []byte
|
|
|
|
var err error
|
2023-03-18 15:18:24 +00:00
|
|
|
|
|
|
|
if maxBytes < 0 {
|
|
|
|
buf, err = io.ReadAll(out)
|
2023-03-18 17:22:27 +00:00
|
|
|
} else {
|
|
|
|
buf, err = io.ReadAll(io.LimitReader(out, maxBytes))
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
2023-03-18 17:22:27 +00:00
|
|
|
if err != nil {
|
2023-03-18 15:18:24 +00:00
|
|
|
return "", false, err
|
|
|
|
}
|
2023-03-18 17:22:27 +00:00
|
|
|
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')
|
2023-03-18 15:18:24 +00:00
|
|
|
|
2023-03-18 17:22:27 +00:00
|
|
|
if lastNewline > 0 {
|
|
|
|
// Trim the data buffer up to the last newline character
|
|
|
|
buf = buf[:lastNewline]
|
|
|
|
}
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:22:27 +00:00
|
|
|
return string(buf), truncated, nil
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|