1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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)
}
|