diff options
| author | 2026-06-06 19:21:52 +0000 | |
|---|---|---|
| committer | 2026-06-06 19:21:52 +0000 | |
| commit | a3cade08a0b0101688e8930916c04866860b2650 (patch) | |
| tree | de10695bc4342f04bc512b050caaddfecd0a321b /internal/testgit/refname.go | |
| parent | config: Just use sha256 test (diff) | |
Prepare for refname; use lgo; etc.
Diffstat (limited to 'internal/testgit/refname.go')
| -rw-r--r-- | internal/testgit/refname.go | 77 |
1 files changed, 77 insertions, 0 deletions
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) +} |
