mugit/internal/git/paths.go (view raw)
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | securejoin "github.com/cyphar/filepath-securejoin" |
| 8 | ) |
| 9 | |
| 10 | func ResolveName(name string) string { |
| 11 | return strings.TrimSuffix(name, ".git") + ".git" |
| 12 | } |
| 13 | |
| 14 | func ResolvePath(baseDir, repoName string) (string, error) { |
| 15 | path, err := securejoin.SecureJoin(baseDir, repoName) |
| 16 | if err != nil { |
| 17 | return "", fmt.Errorf("failed to secure join paths: %w", err) |
| 18 | } |
| 19 | return path, err |
| 20 | } |
| 21 | |
| 22 | // topLevelEntry returns the top-level entry name under base for a given path. |
| 23 | // e.g. base="lua", path="lua/plugins/foo.lua" -> "plugins" |
| 24 | // e.g. base="", path="README.md" -> "README.md" |
| 25 | // returns "" if path is not under base. |
| 26 | func topLevelEntry(fullPath, base string) string { |
| 27 | if base != "" && base != "." { |
| 28 | if !strings.HasPrefix(fullPath, base+"/") { |
| 29 | return "" |
| 30 | } |
| 31 | fullPath = fullPath[len(base)+1:] |
| 32 | } |
| 33 | if before, _, ok := strings.Cut(fullPath, "/"); ok { |
| 34 | return before |
| 35 | } |
| 36 | return fullPath |
| 37 | } |