contexts, and adopting by name: loomctl external add loom/cart cart

The config becomes kubectl-shaped — named contexts with one current, each holding a
host, its flavor and a read-only token — and a bare owner/repo resolves against it.
The point is where the details live: a host's raw-file route belongs to the host so
it sits in the context, the published directory belongs to the convention so it
sits in the code, and what is left is which repository and which document, which is
the only part a person knows.

Measured rather than assumed, because the three hosts differ. Gitea redirects its
short raw form to the resolved branch, so the lock records a branch without anybody
naming one. GitHub and GitLab accept HEAD and do not redirect, which would put a
moving ref in the lock — the hazard already recorded and nearly built anyway — so
those resolve the default branch with git ls-remote --symref first, one round trip
and no clone.

That supersedes the claim that list is the only command needing git, which is now
wrong for two of three flavors and would otherwise read as still true.

Adds loomctl config, which says which context is current and where the credential
came from without printing it, so that "my token is not being used" is answerable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UTxuSizozEA8yDitPuris
This commit is contained in:
2026-09-07 16:57:39 -04:00
co-authored by Claude Opus 5
parent 104cbd050c
commit 73b3a97dce
4 changed files with 295 additions and 29 deletions
+86
View File
@@ -0,0 +1,86 @@
package external
import (
"bytes"
"fmt"
"io"
"os/exec"
"strings"
"git.hypertheory-labs.dev/loom/loom-cli/internal/config"
)
// AddByName adopts a document named the way `list` prints it, against the
// current context.
//
// loomctl external add loom/cart cart
//
// Nobody should have to type a host's raw-file route to adopt a document. The
// route belongs to the host, so it lives in the context; the published
// directory belongs to the convention, so it lives in the code; and what is
// left — which repository, which document — is the only part a person knows.
func AddByName(root, ownerRepo, name string, out io.Writer) error {
cfg, err := config.Load()
if err != nil {
return err
}
ctx, err := cfg.Current()
if err != nil {
return err
}
if strings.Count(ownerRepo, "/") != 1 {
return fmt.Errorf("want owner/repo, got %q", ownerRepo)
}
if !strings.HasSuffix(name, ".md") {
name += ".md"
}
raw, resolved := ctx.RawURL(ownerRepo, PublishedDir+"/"+name)
if raw == "" {
return fmt.Errorf("context %q has flavor %q; known flavors are %s",
cfg.CurrentContext, ctx.Flavor, config.Flavors())
}
if !resolved {
// The lock must record a resolved URL. A short form that stays short
// follows whatever the default branch is at the time you ask, so a
// branch rename would report as a change in the document.
branch, err := defaultBranch(ctx.CloneURL(ownerRepo))
if err != nil {
return fmt.Errorf("resolving the default branch of %s: %w", ownerRepo, err)
}
raw = fmt.Sprintf(raw, branch)
}
return Add(root, raw, "", out)
}
// ListByName enumerates a repository named against the current context.
func ListByName(ownerRepo string, out io.Writer) error {
cfg, err := config.Load()
if err != nil {
return err
}
ctx, err := cfg.Current()
if err != nil {
return err
}
return List(ctx.CloneURL(ownerRepo), out)
}
// defaultBranch asks the remote which branch HEAD points at, without cloning.
//
// Only hosts whose raw URLs do not redirect need this. Gitea resolves its own
// short form, which is why adopting from gitea needs no git at all.
func defaultBranch(cloneURL string) (string, error) {
cmd := exec.Command("git", "ls-remote", "--symref", cloneURL, "HEAD")
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%w: %s", err, strings.TrimSpace(stderr.String()))
}
for _, line := range strings.Split(stdout.String(), "\n") {
if after, ok := strings.CutPrefix(line, "ref: refs/heads/"); ok {
return strings.TrimSpace(strings.SplitN(after, "\t", 2)[0]), nil
}
}
return "", fmt.Errorf("no symref in git ls-remote output")
}