gunk-dev/the-valley

normalize.go

sourcecaaa5bee5e11main's published tip history
1.3 KiB
1package project
2
3import (
4 "strings"
5
6 "the-valley/flow/internal/git"
7)
8
9// NormalizeRepoName resolves a repo reference to its shortest canonical name
10// using the project registry. The rules are:
11// 1. If ref is a registered project name, return it as-is.
12// 2. If ref is owner/repo (or a full GitHub URL) and the repo portion matches
13// a registered project name, return just the project name.
14// 3. If ref is owner/repo that does NOT match a registered project, keep owner/repo.
15// 4. If ref is empty, return empty (caller decides how to handle).
16//
17// Full GitHub URLs are stripped to owner/repo before checking.
18func NormalizeRepoName(ref string, reg *Registry) string {
19 if ref == "" {
20 return ""
21 }
22
23 // Strip URL prefixes and .git suffix to get owner/repo form
24 cleaned := git.CleanGitHubRef(ref)
25
26 // If no slash, it might already be a project name
27 if !strings.Contains(cleaned, "/") {
28 if reg != nil {
29 if _, ok := reg.Get(cleaned); ok {
30 return cleaned
31 }
32 }
33 return cleaned
34 }
35
36 // It's owner/repo — extract the repo portion
37 parts := strings.SplitN(cleaned, "/", 3)
38 if len(parts) < 2 || parts[0] == "" || parts[1] == "" {
39 return cleaned
40 }
41 owner := parts[0]
42 repo := parts[1]
43
44 // Check if the repo name matches a registered project
45 if reg != nil {
46 if _, ok := reg.Get(repo); ok {
47 return repo
48 }
49 }
50
51 return owner + "/" + repo
52}