| 1 | // Package forgekey owns repository keys. Recognising a host is not permission |
| 2 | // to execute work there; callers retain their forge admission policy. |
| 3 | package forgekey |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | const ( |
| 12 | Hub = "amadan.net" |
| 13 | GitHub = "github.com" |
| 14 | ) |
| 15 | |
| 16 | var ErrKey = errors.New("invalid repository key") |
| 17 | var segment = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`) |
| 18 | var newName = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]{1,38}$`) |
| 19 | |
| 20 | // Key preserves the forge's spelling. Use Equal for identity comparisons. |
| 21 | type Key struct{ Host, Owner, Name string } |
| 22 | |
| 23 | func KnownHost(host string) bool { return host == Hub || host == GitHub } |
| 24 | func (k Key) String() string { return k.Host + "/" + k.Owner + "/" + k.Name } |
| 25 | |
| 26 | // Parse accepts a key, or a bare owner/name for the hub. URLs are not keys. |
| 27 | func Parse(raw string) (Key, error) { |
| 28 | parts := strings.Split(raw, "/") |
| 29 | if len(parts) == 2 { |
| 30 | if KnownHost(parts[0]) { |
| 31 | return Key{}, ErrKey |
| 32 | } |
| 33 | parts = append([]string{Hub}, parts...) |
| 34 | } |
| 35 | if len(parts) != 3 || !KnownHost(parts[0]) { |
| 36 | return Key{}, ErrKey |
| 37 | } |
| 38 | for _, p := range parts[1:] { |
| 39 | if !segment.MatchString(p) || p == "." || p == ".." { |
| 40 | return Key{}, ErrKey |
| 41 | } |
| 42 | } |
| 43 | return Key{Host: parts[0], Owner: parts[1], Name: parts[2]}, nil |
| 44 | } |
| 45 | |
| 46 | func Equal(a, b string) bool { |
| 47 | ka, ea := Parse(a) |
| 48 | kb, eb := Parse(b) |
| 49 | if ea != nil || eb != nil || ka.Host != kb.Host { |
| 50 | return false |
| 51 | } |
| 52 | if ka.Host == GitHub { |
| 53 | return strings.EqualFold(ka.String(), kb.String()) |
| 54 | } |
| 55 | return ka == kb |
| 56 | } |
| 57 | |
| 58 | // Display keeps the host on non-hub keys so names cannot hide their forge. |
| 59 | func Display(raw string) string { |
| 60 | return strings.TrimPrefix(raw, Hub+"/") |
| 61 | } |
| 62 | |
| 63 | // Normalize accepts the person-facing HTTPS and .git spellings. Validation |
| 64 | // remains explicit at the caller's admission boundary. |
| 65 | func Normalize(raw string) string { |
| 66 | r := strings.TrimSpace(raw) |
| 67 | r = strings.TrimPrefix(r, "https://") |
| 68 | r = strings.TrimSuffix(strings.TrimSuffix(r, "/"), ".git") |
| 69 | if !strings.HasPrefix(r, Hub+"/") && !strings.HasPrefix(r, GitHub+"/") { |
| 70 | r = Hub + "/" + r |
| 71 | } |
| 72 | return r |
| 73 | } |
| 74 | |
| 75 | // ValidHub preserves the hub's historical key regexp. Parse additionally |
| 76 | // rejects dot path segments where a canonical path is required. |
| 77 | func ValidHub(raw string) bool { |
| 78 | parts := strings.Split(raw, "/") |
| 79 | return len(parts) == 3 && parts[0] == Hub && segment.MatchString(parts[1]) && segment.MatchString(parts[2]) |
| 80 | } |
| 81 | |
| 82 | func NewHubRepo(raw string) bool { |
| 83 | parts := strings.Split(raw, "/") |
| 84 | return ValidHub(raw) && newName.MatchString(parts[2]) |
| 85 | } |
| 86 | |
| 87 | // HasHubPrefix preserves legacy prefix-only checks. Canonical admission uses Parse. |
| 88 | func HasHubPrefix(raw string) bool { return strings.HasPrefix(raw, Hub+"/") } |
| 89 | |
| 90 | // HubBranchURL preserves the hub's existing branch-link spelling. |
| 91 | // Callers validate the repository and branch at their admission boundary. |
| 92 | func HubBranchURL(repo, branch string) string { return "https://" + repo + "?branch=" + branch } |
| 93 | |