simplifyURL strips given URL of extra parts like extra path segments (i.e., anything beyond `/owner/repo`), query strings, or fragments. This function never returns an error. The rationale behind this function is to let users clone a repo with any URL related to the repo; like: - (Tree)
(u *url.URL)
| 263 | // - (Issue/PR comment) github.com/owner/repo/pull/999#issue-9999999999 |
| 264 | // - (Commit history) github.com/owner/repo/commits/main/?author=foo |
| 265 | func simplifyURL(u *url.URL) *url.URL { |
| 266 | result := &url.URL{ |
| 267 | Scheme: u.Scheme, |
| 268 | User: u.User, |
| 269 | Host: u.Host, |
| 270 | Path: u.Path, |
| 271 | } |
| 272 | |
| 273 | pathParts := strings.SplitN(strings.Trim(u.Path, "/"), "/", 3) |
| 274 | if len(pathParts) <= 2 { |
| 275 | return result |
| 276 | } |
| 277 | |
| 278 | result.Path = strings.Join(pathParts[0:2], "/") |
| 279 | return result |
| 280 | } |