giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
IsBranchExist and IsTagExist now ask got.RefExists instead of git's cat-file batch. got's exit status is useless here: `got ref -l <unknown>` exits 0 with no output, and an empty repository reports "HEAD: refs/heads/main" for a branch that does not exist yet. The wrapper therefore parses the "<name>: <value>" line and follows symbolic entries with a bounded depth. IsReferenceExist/IsObjectExist keep the cat-file batch: they answer "is this object present" for bare and short commit IDs, which got ref cannot, and `got cat` resolves unknown arguments as paths in HEAD's tree, so it would report file names as existing objects. That gap is documented, not silent. Verified: go test ./modules/got ./modules/git (ref_test.go covers both ref behaviours, the empty repository, and the enclosing-repository escape; ref_got_test.go covers true/false branches and tags plus a missing got binary). Full go test ./modules/... ./services/... ./models/... ./routers/... and make -C cgi check pass. Assisted-by: splinter:claude-opus-4
103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package got
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// validRefName rejects names that got would read as an option or that no
|
|
// reference can have. Placement is the caller's job; this only keeps the
|
|
// argument list intact and stops the obvious garbage.
|
|
func validRefName(name string) bool {
|
|
if name == "" || strings.HasPrefix(name, "-") || strings.ContainsAny(name, " \t\n\r") {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// RefExists reports whether the repository has a reference with exactly that
|
|
// name that resolves to an object, matching git show-ref --verify. Two got
|
|
// behaviours make that harder than the exit status:
|
|
//
|
|
// - got exits 0 and prints nothing for an unknown name, so the answer is the
|
|
// "<name>: <value>" line, not the command result.
|
|
// - in an empty repository got still lists HEAD, as "HEAD: refs/heads/main",
|
|
// pointing at a branch that does not exist yet. A symbolic entry therefore
|
|
// only counts when its target resolves.
|
|
//
|
|
// A bare object ID is not a reference and answers false, again like
|
|
// show-ref --verify; callers that pass a commit ID rely on that.
|
|
func RefExists(ctx context.Context, path, name string) (bool, error) {
|
|
for range maxSymbolicDepth {
|
|
if !validRefName(name) {
|
|
return false, nil
|
|
}
|
|
out, _, err := run(ctx, "ref", "-l", "-r", path, name)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "bad reference name") {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
prefix := name + ": "
|
|
var value string
|
|
var found bool
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if v, ok := strings.CutPrefix(line, prefix); ok {
|
|
value, found = strings.TrimSpace(v), true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false, nil
|
|
}
|
|
if isObjectID(value) {
|
|
return true, nil
|
|
}
|
|
name = value // symbolic: resolve its target before answering
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// maxSymbolicDepth bounds symbolic reference chains; git limits them too, and
|
|
// a loop must not hang the caller.
|
|
const maxSymbolicDepth = 5
|
|
|
|
// RefObjectID returns the object a reference points at. A symbolic reference
|
|
// reports its target instead of an object, which is an error here.
|
|
func RefObjectID(ctx context.Context, path, name string) (string, error) {
|
|
if !validRefName(name) {
|
|
return "", fmt.Errorf("invalid reference name: %q", name)
|
|
}
|
|
out, _, err := run(ctx, "ref", "-l", "-r", path, name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
prefix := name + ": "
|
|
for _, line := range strings.Split(out, "\n") {
|
|
value, ok := strings.CutPrefix(line, prefix)
|
|
if !ok {
|
|
continue
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
if isObjectID(value) {
|
|
return value, nil
|
|
}
|
|
return "", fmt.Errorf("reference %s is not an object: %s", name, value)
|
|
}
|
|
return "", ErrRefNotFound{Name: name}
|
|
}
|
|
|
|
// ErrRefNotFound reports a missing reference.
|
|
type ErrRefNotFound struct {
|
|
Name string
|
|
}
|
|
|
|
func (e ErrRefNotFound) Error() string {
|
|
return "reference not found: " + e.Name
|
|
}
|