Skip to content

Commit

Permalink
update label to accept external references to the main repository
Browse files Browse the repository at this point in the history
add a test

update

remove hardcode logic

clean up logic

Add comments to clarify label handling

add more comments
  • Loading branch information
tyler-french committed Jul 20, 2021
1 parent 0ac66c9 commit 19a3846
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 12 additions & 3 deletions label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func New(repo, pkg, name string) Label {
var NoLabel = Label{}

var (
labelRepoRegexp = regexp.MustCompile(`^$|^[A-Za-z][A-Za-z0-9_]*$`)
labelRepoRegexp = regexp.MustCompile(`^@$|^[A-Za-z][A-Za-z0-9_]*$`)
labelPkgRegexp = regexp.MustCompile(`^[A-Za-z0-9/._-]*$`)
labelNameRegexp = regexp.MustCompile(`^[A-Za-z0-9_/.+=,@~-]*$`)
)
Expand All @@ -77,9 +77,14 @@ func Parse(s string) (Label, error) {
if strings.HasPrefix(s, "@") {
relative = false
endRepo := strings.Index(s, "//")
if endRepo > 0 {
if endRepo > len("@") {
repo = s[len("@"):endRepo]
s = s[endRepo:]
// If the label begins with "@//...", set repo = "@"
// to remain distinct from "//...", where repo = ""
} else if endRepo == len("@") {
repo = s[:len("@")]
s = s[len("@"):]
} else {
repo = s[len("@"):]
s = "//:" + repo
Expand Down Expand Up @@ -134,8 +139,12 @@ func (l Label) String() string {
}

var repo string
if l.Repo != "" {
if l.Repo != "" && l.Repo != "@" {
repo = fmt.Sprintf("@%s", l.Repo)
} else {
// if l.Repo == "", the label string will begin with "//"
// if l.Repo == "@", the label string will begin with "@//"
repo = l.Repo
}

if path.Base(l.Pkg) == l.Name {
Expand Down
7 changes: 5 additions & 2 deletions label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestLabelString(t *testing.T) {
}, {
l: Label{Relative: true, Name: "foo"},
want: ":foo",
}, {
l: Label{Repo: "@", Pkg: "foo/bar", Name: "baz"},
want: "@//foo/bar:baz",
},
} {
if got, want := spec.l.String(), spec.want; got != want {
Expand All @@ -61,8 +64,8 @@ func TestParse(t *testing.T) {
{str: "@//:", wantErr: true},
{str: "@a:b", wantErr: true},
{str: "@a//", wantErr: true},
{str: "@//:a", want: Label{Name: "a", Relative: false}},
{str: "@//a:b", want: Label{Repo: "", Pkg: "a", Name: "b"}},
{str: "@//:a", want: Label{Repo: "@", Name: "a", Relative: false}},
{str: "@//a:b", want: Label{Repo: "@", Pkg: "a", Name: "b"}},
{str: ":a", want: Label{Name: "a", Relative: true}},
{str: "a", want: Label{Name: "a", Relative: true}},
{str: "//:a", want: Label{Name: "a", Relative: false}},
Expand Down

0 comments on commit 19a3846

Please sign in to comment.