mirror of
https://github.com/gogs/gogs.git
synced 2026-11-29 21:14:36 +01:00
116 lines
3.0 KiB
Go
116 lines
3.0 KiB
Go
package git
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// tracer creates the spans of the functions and methods of the package, and of
|
|
// the Git processes they run.
|
|
var tracer = otel.Tracer("gogs.io/gogs/internal/git")
|
|
|
|
var (
|
|
// logOutput is the writer to write logs. When not set, no log will be produced.
|
|
logOutput io.Writer
|
|
// logPrefix is the prefix prepended to each log entry.
|
|
logPrefix = "[git] "
|
|
)
|
|
|
|
// SetOutput sets the output writer for logs.
|
|
func SetOutput(output io.Writer) {
|
|
logOutput = output
|
|
}
|
|
|
|
func logf(format string, args ...any) {
|
|
if logOutput == nil {
|
|
return
|
|
}
|
|
|
|
_, _ = fmt.Fprint(logOutput, logPrefix)
|
|
_, _ = fmt.Fprintf(logOutput, format, args...)
|
|
_, _ = fmt.Fprintln(logOutput)
|
|
}
|
|
|
|
var (
|
|
// gitVersion stores the Git binary version.
|
|
// NOTE: Call BinVersion to check the Git version instead of reading this
|
|
// variable.
|
|
gitVersion string
|
|
gitVersionMu sync.Mutex
|
|
gitVersionSet bool
|
|
)
|
|
|
|
// BinVersion returns current Git binary version that is used by this package.
|
|
// Successful results are cached. Failed attempts are not cached, allowing
|
|
// retries with a fresh context.
|
|
func BinVersion(ctx context.Context) (_ string, err error) {
|
|
ctx, span := tracer.Start(ctx, "git.BinVersion")
|
|
defer func() { endSpan(span, err) }()
|
|
|
|
gitVersionMu.Lock()
|
|
defer gitVersionMu.Unlock()
|
|
|
|
if gitVersionSet {
|
|
return gitVersion, nil
|
|
}
|
|
|
|
stdout, err := exec(ctx, "", []string{"version"}, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fields := strings.Fields(string(stdout))
|
|
if len(fields) < 3 {
|
|
return "", errors.Newf("not enough output: %s", stdout)
|
|
}
|
|
|
|
// Handle special case on Windows.
|
|
i := strings.Index(fields[2], "windows")
|
|
if i >= 1 {
|
|
gitVersion = fields[2][:i-1]
|
|
} else {
|
|
gitVersion = fields[2]
|
|
}
|
|
|
|
gitVersionSet = true
|
|
return gitVersion, nil
|
|
}
|
|
|
|
// ConfigGet returns the value of given key in Git configuration. It returns an
|
|
// empty string when the key is not set.
|
|
//
|
|
// Docs: https://git-scm.com/docs/git-config
|
|
func ConfigGet(ctx context.Context, key string) (_ string, err error) {
|
|
ctx, span := tracer.Start(ctx, "git.ConfigGet", trace.WithAttributes(attribute.String("git.config.key", key)))
|
|
defer func() { endSpan(span, err) }()
|
|
|
|
stdout, err := exec(ctx, "", []string{"config", "--get", "--end-of-options", key}, nil)
|
|
if err != nil {
|
|
if isExitStatus(err, 1) {
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(stdout)), nil
|
|
}
|
|
|
|
// SetGlobalConfig sets given key to the value in the global Git configuration
|
|
// of the current user.
|
|
//
|
|
// Docs: https://git-scm.com/docs/git-config
|
|
func SetGlobalConfig(ctx context.Context, key, value string) (err error) {
|
|
ctx, span := tracer.Start(ctx, "git.SetGlobalConfig", trace.WithAttributes(attribute.String("git.config.key", key)))
|
|
defer func() { endSpan(span, err) }()
|
|
|
|
_, err = exec(ctx, "", []string{"config", "--global", "--end-of-options", key, value}, nil)
|
|
return err
|
|
}
|