package cli import ( "context" "errors" "fmt" "os" "strings" "tangled.org/rockorager.dev/knit/internal/git" "tangled.org/rockorager.dev/knit/internal/tangled" ) // repoRef is a repository as a user typed it: a repo DID, or an owner (handle // or DID) and a name. type repoRef struct { DID string Owner string Name string } // parseRepoRef understands OWNER/NAME, HOST/OWNER/NAME, a repo DID, and // https, ssh, and scp-like URLs of the web interface or a knot. A bare NAME // leaves Owner empty. func parseRepoRef(s string) (repoRef, error) { path, isURL := s, false colon, slash := strings.IndexByte(s, ':'), strings.IndexByte(s, '/') scpLike := colon > 0 && !strings.HasPrefix(s, "did:") && (slash < 0 || colon < slash) if strings.Contains(s, "://") || scpLike { u, err := git.ParseURL(s) if err != nil { return repoRef{}, err } path, isURL = u.Path, true } path = strings.TrimSuffix(strings.Trim(path, "/"), ".git") parts := strings.Split(path, "/") for i := range parts { parts[i] = strings.TrimPrefix(parts[i], "@") } switch { case len(parts) == 1 && strings.HasPrefix(parts[0], "did:"): return repoRef{DID: parts[0]}, nil case len(parts) == 1 && parts[0] != "": return repoRef{Name: parts[0]}, nil case len(parts) == 2: return repoRef{Owner: parts[0], Name: parts[1]}, nil case len(parts) >= 3: // HOST/OWNER/NAME, or a URL path with a trailing part such as // /issues/12 if !isURL && len(parts) == 3 { return repoRef{Owner: parts[1], Name: parts[2]}, nil } return repoRef{Owner: parts[0], Name: parts[1]}, nil } return repoRef{}, fmt.Errorf("can't make sense of repository %q; expected OWNER/NAME", s) } // lookupRepo resolves a repository argument. A bare name refers to one of // the signed-in user's repositories. func (a *App) lookupRepo(ctx context.Context, s string) (*tangled.Repo, error) { ref, err := parseRepoRef(s) if err != nil { return nil, err } c := a.Client() if ref.DID != "" { return c.GetRepoByDID(ctx, ref.DID) } if ref.Owner == "" { me, err := c.Me(ctx) if err != nil { return nil, fmt.Errorf("%q has no owner and you're not logged in: %w", s, err) } ref.Owner = me } return c.GetRepo(ctx, ref.Owner, ref.Name) } // baseRepo is the repository a command acts on: --repo, $KNIT_REPO, or the // first git remote that points at a Tangled repository. Remotes named // upstream are tried first, so a fork's commands act on what it was forked // from, then origin, then the rest. func (a *App) baseRepo(ctx context.Context) (*tangled.Repo, error) { repo, _, err := a.baseRepoAndRemote(ctx) return repo, err } func (a *App) baseRepoAndRemote(ctx context.Context) (*tangled.Repo, *git.Remote, error) { if s := a.RepoFlag; s != "" { r, err := a.lookupRepo(ctx, s) return r, a.remoteFor(r), err } if s := os.Getenv("KNIT_REPO"); s != "" { r, err := a.lookupRepo(ctx, s) return r, a.remoteFor(r), err } remotes, err := git.Remotes() if err != nil { return nil, nil, errors.New("not in a git repository; pass --repo OWNER/NAME") } var lastErr error for _, remote := range orderRemotes(remotes) { r, err := a.repoForRemote(ctx, remote) if err == nil { return r, remote, nil } lastErr = err } if lastErr != nil { return nil, nil, fmt.Errorf("no git remote points at a Tangled repository (%v); pass --repo OWNER/NAME", lastErr) } return nil, nil, errors.New("no git remotes found; pass --repo OWNER/NAME") } func orderRemotes(remotes []*git.Remote) []*git.Remote { rank := func(name string) int { switch name { case "upstream": return 0 case "origin": return 1 } return 2 } out := append([]*git.Remote(nil), remotes...) for i := 1; i < len(out); i++ { for j := i; j > 0 && rank(out[j].Name) < rank(out[j-1].Name); j-- { out[j], out[j-1] = out[j-1], out[j] } } return out } func (a *App) repoForRemote(ctx context.Context, remote *git.Remote) (*tangled.Repo, error) { if remote.FetchURL == "" { return nil, fmt.Errorf("remote %s has no URL", remote.Name) } ref, err := parseRepoRef(remote.FetchURL) if err != nil { return nil, err } c := a.Client() if ref.DID != "" { return c.GetRepoByDID(ctx, ref.DID) } if ref.Owner == "" { return nil, fmt.Errorf("remote %s: can't find an owner in %s", remote.Name, remote.FetchURL) } return c.GetRepo(ctx, ref.Owner, ref.Name) } // remoteFor finds the git remote of the current repository that points at r, // if any. func (a *App) remoteFor(r *tangled.Repo) *git.Remote { if r == nil { return nil } remotes, err := git.Remotes() if err != nil { return nil } for _, remote := range remotes { ref, err := parseRepoRef(remote.FetchURL) if err != nil { continue } if ref.DID == r.DID || (strings.EqualFold(ref.Name, r.Name) && (ref.Owner == r.Owner || strings.EqualFold(ref.Owner, r.OwnerHandle))) { return remote } } return nil } // parseTicketArg accepts 12, #12, or a web URL such as // https://tangled.org/alice/proj/issues/12. A URL also names the repository. func parseTicketArg(s string) (number, repo string) { for _, kind := range []string{"/issues/", "/pulls/"} { if i := strings.Index(s, kind); i >= 0 && strings.Contains(s, "://") { rest := s[i+len(kind):] if j := strings.IndexAny(rest, "/?#"); j >= 0 { rest = rest[:j] } return rest, s[:i] } } return strings.TrimPrefix(s, "#"), "" } // ticketRepo resolves the repository for a ticket argument. func (a *App) ticketRepo(ctx context.Context, arg string) (string, *tangled.Repo, error) { number, repoURL := parseTicketArg(arg) if repoURL != "" { r, err := a.lookupRepo(ctx, repoURL) return number, r, err } r, err := a.baseRepo(ctx) return number, r, err }