From a3cade08a0b0101688e8930916c04866860b2650 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 6 Jun 2026 19:21:52 +0000 Subject: Prepare for refname; use lgo; etc. --- internal/testgit/command.go | 2 +- internal/testgit/refname.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ internal/testgit/repo.go | 1 + 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 internal/testgit/refname.go (limited to 'internal/testgit') diff --git a/internal/testgit/command.go b/internal/testgit/command.go index 341b635c..a7d8651a 100644 --- a/internal/testgit/command.go +++ b/internal/testgit/command.go @@ -13,7 +13,7 @@ func (repo *Repo) Command( ) *exec.Cmd { tb.Helper() - cmd := exec.CommandContext(tb.Context(), command, args...) + cmd := exec.CommandContext(tb.Context(), command, args...) //nolint:gosec // Test helper runs caller-selected commands. cmd.Dir = repo.path cmd.Env = repo.env diff --git a/internal/testgit/refname.go b/internal/testgit/refname.go new file mode 100644 index 00000000..c3d0f9b7 --- /dev/null +++ b/internal/testgit/refname.go @@ -0,0 +1,77 @@ +package testgit + +import ( + "os/exec" + "strings" + "testing" +) + +type RefFormatOptions struct { + AllowOneLevel bool + RefspecPattern bool +} + +func (repo *Repo) CheckRefFormat(tb testing.TB, name string, opts RefFormatOptions) error { + tb.Helper() + + _, err := repo.Run(tb, nil, "git", refFormatArgs("check-ref-format", name, opts)...) + + return err +} + +func (repo *Repo) NormalizeRefFormat(tb testing.TB, name string, opts RefFormatOptions) (string, error) { + tb.Helper() + + out, err := repo.Run(tb, nil, "git", refFormatArgs("check-ref-format", name, opts, "--normalize")...) + if err != nil { + return "", err + } + + return strings.TrimSuffix(string(out), "\n"), nil +} + +func (repo *Repo) CheckBranchName(tb testing.TB, name string) (string, error) { + tb.Helper() + + out, err := repo.Run(tb, nil, "git", "check-ref-format", "--branch", name) + if err != nil { + return "", err + } + + branchName := strings.TrimSuffix(string(out), "\n") + if strings.HasPrefix(branchName, "refs/") { + return branchName, nil + } + + return "refs/heads/" + branchName, nil +} + +func (repo *Repo) CheckTagName(tb testing.TB, name string) (string, error) { + tb.Helper() + + if strings.HasPrefix(name, "-") || name == "HEAD" { + return "", exec.ErrNotFound + } + + _, err := repo.Run(tb, nil, "git", "check-ref-format", "refs/tags/"+name) + if err != nil { + return "", err + } + + return "refs/tags/" + name, nil +} + +func refFormatArgs(command string, name string, opts RefFormatOptions, extra ...string) []string { + args := []string{command} + args = append(args, extra...) + + if opts.AllowOneLevel { + args = append(args, "--allow-onelevel") + } + + if opts.RefspecPattern { + args = append(args, "--refspec-pattern") + } + + return append(args, name) +} diff --git a/internal/testgit/repo.go b/internal/testgit/repo.go index 940ba1e1..d10c7db9 100644 --- a/internal/testgit/repo.go +++ b/internal/testgit/repo.go @@ -13,6 +13,7 @@ type Repo struct { env []string } +//exhaustruct:ignore type RepoOptions struct { ObjectFormat id.ObjectFormat } -- cgit v1.3.1-10-gc9f91