Something went wrong. Try again.
A code review TUI that tries to reduce how many red and green squiggles you have to squint at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139package git
import ( "bytes" "context" "errors" "fmt" "io" "os" "os/exec" "strings" "time")
const ( commandErrorBytesMax = 64 * 1024 commandWaitDelay = time.Second)
// CommandOutputExtentError reports that a Git command produced more stdout// than its caller can safely consume.type CommandOutputExtentError struct { observed int limit int}
// Error implements error.func (extentError CommandOutputExtentError) Error() string { return fmt.Sprintf( "git: command output is too large: observed %d bytes, limit %d", extentError.observed, extentError.limit, )}
// CommandOutputBytes returns the stdout extent observed before aborting.func (extentError CommandOutputExtentError) CommandOutputBytes() int { return extentError.observed}
// CommandOutputLimitBytes returns the caller's fixed stdout bound.func (extentError CommandOutputExtentError) CommandOutputLimitBytes() int { return extentError.limit}
func (repository Repository) run(ctx context.Context, limit int, arguments ...string) ([]byte, error) { output := newBoundedBuffer(limit) if err := repository.write(ctx, nil, nil, &output, arguments...); err != nil { return nil, err } return output.buffer.Bytes(), nil}
func (repository Repository) write( ctx context.Context, environment []string, input io.Reader, output io.Writer, arguments ...string,) error { if ctx == nil || output == nil || len(arguments) == 0 { return errors.New("git: context, output, and command arguments are required") } commandArguments := []string{"-C", repository.Root, "--no-pager"} commandArguments = append(commandArguments, arguments...) command := exec.CommandContext(ctx, "git", commandArguments...) command.Env = append(os.Environ(), "LC_ALL=C", "GIT_OPTIONAL_LOCKS=0") command.Env = append(command.Env, repository.environment...) command.Env = append(command.Env, environment...) command.Stdin = input stdout := recordingWriter{destination: output} command.Stdout = &stdout command.WaitDelay = commandWaitDelay stderr := boundedCommandError{remaining: commandErrorBytesMax} command.Stderr = &stderr if operationErr := errors.Join(command.Run(), stdout.err, ctx.Err()); operationErr != nil { message := strings.TrimSpace(stderr.buffer.String()) if message != "" { return fmt.Errorf("%s: %w", message, operationErr) } return operationErr } return nil}
type recordingWriter struct { destination io.Writer err error}
func (writer *recordingWriter) Write(data []byte) (int, error) { written, err := writer.destination.Write(data) if err == nil && written != len(data) { err = io.ErrShortWrite } if writer.err == nil { writer.err = err } return written, err}
type boundedBuffer struct { buffer bytes.Buffer limit int remaining int}
func newBoundedBuffer(limit int) boundedBuffer { return boundedBuffer{limit: limit, remaining: limit}}
func (writer *boundedBuffer) Write(data []byte) (int, error) { if len(data) > writer.remaining { return 0, CommandOutputExtentError{ observed: writer.limit - writer.remaining + len(data), limit: writer.limit, } } written, err := writer.buffer.Write(data) writer.remaining -= written return written, err}
type boundedCommandError struct { buffer bytes.Buffer remaining int}
func (writer *boundedCommandError) Write(data []byte) (int, error) { originalLength := len(data) data = data[:min(len(data), writer.remaining)] written, err := writer.buffer.Write(data) writer.remaining -= written if err != nil { return written, err } return originalLength, nil}