Add got.Branches and route both GetBranchNames implementations through it, so branch listing no longer runs git for-each-ref. Names come from `got ref -l refs/heads/`, not `got branch -l`: the latter also reports refs/remotes copies and can print a name twice. Order comes from `got branch -lt`; names that pass misses are appended, so a failure there degrades to lexical order instead of failing the listing. Two got behaviours needed guarding: - `-r` searches upwards. A path that is not a repository made got answer for the enclosing one, so a broken fixture silently returned the parent repository's refs. run now validates the target first. - got refuses a repository without objects/pack or refs/. Those are empty directories, so a fixture checked into git loses them. run recreates the layout, content-free and idempotent. repo2_empty carried .gitkeep files under refs/heads and refs/tags; got reads them as references and fails with "bad reference name". Dropped both, the layout is now created on demand. Tests: go test ./modules/got ./modules/git ./modules/git -tags gogit ./services/repository/... ./modules/repository/... ./models/git/... ./routers/web/repo/... ./services/context/... - all pass. New tests cover ordering, paging, remote-copy filtering, the missing-directory repair, the enclosing-repository refusal, and absence of a git fallback. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package got
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// branchNameFromRefLine splits a "refs/heads/<name>: <object>" listing line.
|
|
func branchNameFromRefLine(line string) (string, bool) {
|
|
name, ok := strings.CutPrefix(line, "refs/heads/")
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
name, _, ok = strings.Cut(name, ": ")
|
|
if !ok || !validBranchName(name) {
|
|
return "", false
|
|
}
|
|
return name, true
|
|
}
|
|
|
|
// Branches returns the local branches of the repository at path, most recently
|
|
// committed first, honouring skip and limit. limit 0 means "no limit"; the
|
|
// second return value is always the untruncated branch count.
|
|
//
|
|
// got ref -l yields the authoritative refs/heads names (got branch -l also
|
|
// lists remote copies and can repeat them), while got branch -lt supplies the
|
|
// commit order. If the ordering pass fails the branches stay in the lexical
|
|
// order got ref -l produced: wrong last-push-first ordering is not worth
|
|
// failing a repository listing over, and nothing here falls back to git.
|
|
func Branches(ctx context.Context, path string, skip, limit int) ([]string, int, error) {
|
|
out, _, err := run(ctx, "ref", "-l", "-r", path, "refs/heads/")
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
local := make(map[string]struct{})
|
|
var lexical []string
|
|
for _, line := range strings.Split(out, "\n") {
|
|
name, ok := branchNameFromRefLine(line)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if _, dup := local[name]; dup {
|
|
continue
|
|
}
|
|
local[name] = struct{}{}
|
|
lexical = append(lexical, name)
|
|
}
|
|
|
|
ordered := make([]string, 0, len(lexical))
|
|
seen := make(map[string]struct{}, len(lexical))
|
|
if timeOut, _, timeErr := run(ctx, "branch", "-lt", "-r", path); timeErr == nil {
|
|
for _, line := range strings.Split(timeOut, "\n") {
|
|
name, _, ok := strings.Cut(strings.TrimSpace(line), ": ")
|
|
if !ok {
|
|
continue
|
|
}
|
|
name = strings.TrimSpace(name)
|
|
if _, isLocal := local[name]; !isLocal {
|
|
continue // a copy of a remote branch, not ours
|
|
}
|
|
if _, dup := seen[name]; dup {
|
|
continue
|
|
}
|
|
seen[name] = struct{}{}
|
|
ordered = append(ordered, name)
|
|
}
|
|
}
|
|
for _, name := range lexical {
|
|
if _, done := seen[name]; done {
|
|
continue
|
|
}
|
|
seen[name] = struct{}{}
|
|
ordered = append(ordered, name)
|
|
}
|
|
|
|
total := len(ordered)
|
|
if skip > 0 {
|
|
if skip >= len(ordered) {
|
|
return []string{}, total, nil
|
|
}
|
|
ordered = ordered[skip:]
|
|
}
|
|
if limit > 0 && limit < len(ordered) {
|
|
ordered = ordered[:limit]
|
|
}
|
|
return ordered, total, nil
|
|
}
|