Add modules/got with a got runner, version check, repository detection, and idempotent bare init. Route bare repository creation from InitRepositoryLocal through got; non-bare temp worktrees keep git. Add [git] GOT_PATH and tests, including an explicit no-fallback test. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Package got wraps the got(1) binary for repository operations that Gotea
|
|
// has migrated off git. Keep all got process handling in this package.
|
|
package got
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gotea.dev/modules/setting"
|
|
)
|
|
|
|
// ErrNotInstalled means the configured got executable could not be started.
|
|
var ErrNotInstalled = errors.New("got executable not found")
|
|
|
|
// Executable returns the configured got path, defaulting to "got".
|
|
func Executable() string {
|
|
path := strings.TrimSpace(setting.Git.GotPath)
|
|
if path == "" {
|
|
return "got"
|
|
}
|
|
return path
|
|
}
|
|
|
|
func run(ctx context.Context, args ...string) (stdout, stderr string, err error) {
|
|
cmd := exec.CommandContext(ctx, Executable(), args...)
|
|
var out, errOut strings.Builder
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &errOut
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
if errors.Is(err, exec.ErrNotFound) {
|
|
return out.String(), errOut.String(), ErrNotInstalled
|
|
}
|
|
return out.String(), errOut.String(), fmt.Errorf("got %s: %w: %s",
|
|
strings.Join(args, " "), err, strings.TrimSpace(errOut.String()))
|
|
}
|
|
return out.String(), errOut.String(), nil
|
|
}
|
|
|
|
// Version returns the got version without the leading "got ".
|
|
func Version(ctx context.Context) (string, error) {
|
|
out, _, err := run(ctx, "--version")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
fields := strings.Fields(strings.TrimSpace(out))
|
|
if len(fields) < 2 || fields[0] != "got" {
|
|
return "", fmt.Errorf("unexpected got version output: %q", strings.TrimSpace(out))
|
|
}
|
|
return fields[1], nil
|
|
}
|
|
|
|
// IsRepository reports whether path looks like a bare Git-format repository.
|
|
func IsRepository(path string) bool {
|
|
if st, err := os.Stat(filepath.Join(path, "HEAD")); err != nil || !st.Mode().IsRegular() {
|
|
return false
|
|
}
|
|
if st, err := os.Stat(filepath.Join(path, "objects")); err != nil || !st.IsDir() {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// InitBare creates a bare repository at path, or leaves an existing one alone.
|
|
// objectFormat must be "sha1" or "sha256".
|
|
func InitBare(ctx context.Context, path, objectFormat, defaultBranch string) error {
|
|
if objectFormat == "" {
|
|
objectFormat = "sha1"
|
|
}
|
|
if objectFormat != "sha1" && objectFormat != "sha256" {
|
|
return fmt.Errorf("invalid got object format: %s", objectFormat)
|
|
}
|
|
if IsRepository(path) {
|
|
return nil
|
|
}
|
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
|
return fmt.Errorf("create repository directory: %w", err)
|
|
}
|
|
if defaultBranch == "" {
|
|
defaultBranch = "main"
|
|
}
|
|
_, _, err := run(ctx, "init", "-A", objectFormat, "-b", defaultBranch, path)
|
|
return err
|
|
}
|