parseGithubSource extracts owner, repo, branch and in-repo path from a GitHub URL or "owner/repo" shorthand. It supports the permalink, tree, and blob forms used by awesome-list catalogs: https://github.com/owner/repo https://github.com/owner/repo/tree/main/skills/foo https://github.com/owner/re
(raw string)
| 231 | // |
| 232 | // branch and path are empty when the source carries neither. |
| 233 | func parseGithubSource(raw string) (owner, repo, branch, path string, ok bool) { |
| 234 | raw = strings.TrimSpace(raw) |
| 235 | if raw == "" { |
| 236 | return "", "", "", "", false |
| 237 | } |
| 238 | s := raw |
| 239 | s = strings.TrimPrefix(s, "https://") |
| 240 | s = strings.TrimPrefix(s, "http://") |
| 241 | s = strings.TrimPrefix(s, "www.") |
| 242 | |
| 243 | if !strings.HasPrefix(strings.ToLower(s), "github.com/") { |
| 244 | // Shorthand "owner/repo" or "owner/repo@branch". |
| 245 | owner, repo, branch, path, ok = parseShorthandSource(s) |
| 246 | return owner, repo, branch, path, ok |
| 247 | } |
| 248 | |
| 249 | rest := strings.TrimPrefix(s, "github.com/") |
| 250 | rest = strings.TrimSuffix(rest, ".git") |
| 251 | parts := strings.Split(rest, "/") |
| 252 | if len(parts) < 2 || parts[0] == "" || parts[1] == "" { |
| 253 | return "", "", "", "", false |
| 254 | } |
| 255 | owner, repo = parts[0], parts[1] |
| 256 | // parts[2] is "tree"/"blob"/"commit"; parts[3] is the ref; parts[4:] is the path. |
| 257 | if len(parts) >= 4 && (strings.EqualFold(parts[2], "tree") || strings.EqualFold(parts[2], "blob") || strings.EqualFold(parts[2], "commit")) { |
| 258 | branch = parts[3] |
| 259 | if len(parts) > 4 { |
| 260 | path = strings.Join(parts[4:], "/") |
| 261 | } |
| 262 | } else if len(parts) > 2 { |
| 263 | path = strings.Join(parts[2:], "/") |
| 264 | } |
| 265 | return owner, repo, branch, path, true |
| 266 | } |
| 267 | |
| 268 | // parseShorthandSource handles "owner/repo" and "owner/repo@branch" forms. |
| 269 | func parseShorthandSource(s string) (owner, repo, branch, path string, ok bool) { |
no test coverage detected