From 6cae65fd6d144e221446e27d7596184deb1e3d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 17 Jul 2024 10:00:53 +0200 Subject: [PATCH] Only read Git log output from stdout Also rework the GetGitCommandFunc a little to make it work with Hugo. See https://github.com/gohugoio/hugo/issues/8627 --- gitmap.go | 39 ++++++++++++++++++++++++++------------- gitmap_test.go | 2 +- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/gitmap.go b/gitmap.go index b33e324..72ffada 100644 --- a/gitmap.go +++ b/gitmap.go @@ -1,4 +1,4 @@ -// Copyright © 2016-present Bjørn Erik Pedersen . +// Copyright 2024 Bjørn Erik Pedersen . // // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. @@ -9,6 +9,7 @@ import ( "bytes" "errors" "fmt" + "io" "os/exec" "path/filepath" "strings" @@ -49,18 +50,27 @@ type GitInfo struct { Body string `json:"body"` // The commit message body } +// Runner is an interface for running Git commands, +// as implemented buy *exec.Cmd. +type Runner interface { + Run() error +} + // Options for the Map function type Options struct { Repository string // Path to the repository to map Revision string // Use blank or HEAD for the currently active revision - GetGitCommandFunc func(args ...string) *exec.Cmd + GetGitCommandFunc func(stdout, stderr io.Writer, args ...string) (Runner, error) } // Map creates a GitRepo with a file map from the given options. func Map(opts Options) (*GitRepo, error) { if opts.GetGitCommandFunc == nil { - opts.GetGitCommandFunc = func(args ...string) *exec.Cmd { - return exec.Command(gitExec, args...) + opts.GetGitCommandFunc = func(stdout, stderr io.Writer, args ...string) (Runner, error) { + cmd := exec.Command(gitExec, args...) + cmd.Stdout = stdout + cmd.Stderr = stderr + return cmd, nil } } @@ -91,8 +101,7 @@ func Map(opts Options) (*GitRepo, error) { return nil, err } - entriesStr := string(out) - entriesStr = strings.Trim(entriesStr, "\n\x1e'") + entriesStr := strings.Trim(out, "\n\x1e'") entries := strings.Split(entriesStr, "\x1e") for _, e := range entries { @@ -116,19 +125,23 @@ func Map(opts Options) (*GitRepo, error) { return &GitRepo{Files: m, TopLevelAbsPath: topLevelPath}, nil } -func git(opts Options, args ...string) ([]byte, error) { - out, err := opts.GetGitCommandFunc(args...).CombinedOutput() +func git(opts Options, args ...string) (string, error) { + var outBuff bytes.Buffer + var errBuff bytes.Buffer + cmd, err := opts.GetGitCommandFunc(&outBuff, &errBuff, args...) + if err != nil { + return "", err + } + err = cmd.Run() if err != nil { if ee, ok := err.(*exec.Error); ok { if ee.Err == exec.ErrNotFound { - return nil, ErrGitNotFound + return "", ErrGitNotFound } } - - return nil, errors.New(string(bytes.TrimSpace(out))) + return "", errors.New(strings.TrimSpace(errBuff.String())) } - - return out, nil + return outBuff.String(), nil } func toGitInfo(entry string) (*GitInfo, error) { diff --git a/gitmap_test.go b/gitmap_test.go index 5b4d0ef..976101d 100644 --- a/gitmap_test.go +++ b/gitmap_test.go @@ -1,4 +1,4 @@ -// Copyright © 2016-present Bjørn Erik Pedersen . +// Copyright 2024 Bjørn Erik Pedersen . // // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file.