Files
buildkit/util/gitutil/git_cli_helpers.go
Justin Chadwell d34b2471cd git: centralize git cli operations
Move all of the git command line logic into a single object, inspired by
the object already in buildx.

The basic implemenation allows for configuring a git cli for a specific
repository, along with various authorization settings and custom
binaries. Commands can be run for that repository, and a few helpers are
provided for accessing data on it - more to come in the future
hopefully.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2023-08-24 12:22:15 +01:00

45 lines
854 B
Go

package gitutil
import (
"context"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
func (cli *GitCLI) Dir() string {
if cli.dir != "" {
return cli.dir
}
return cli.workTree
}
func (cli *GitCLI) WorkTree(ctx context.Context) (string, error) {
if cli.workTree != "" {
return cli.workTree, nil
}
return cli.clean(cli.Run(ctx, "rev-parse", "--show-toplevel"))
}
func (cli *GitCLI) GitDir(ctx context.Context) (string, error) {
if cli.gitDir != "" {
return cli.gitDir, nil
}
dir, err := cli.WorkTree(ctx)
if err != nil {
return "", err
}
return filepath.Join(dir, ".git"), nil
}
func (cli *GitCLI) clean(dt []byte, err error) (string, error) {
out := string(dt)
out = strings.ReplaceAll(strings.Split(out, "\n")[0], "'", "")
if err != nil {
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
}
return out, err
}