Go, standard library only, with git shelled out for list alone. list enumerates a publisher's .loom/published by partial clone and ls-tree; add fetches one document, writes it under .loom/externals and records the resolved origin and the publisher's ETag in .loom/externals/.locks; check asks conditionally and reports. The first real run did what the tool exists for. All eight documents adopted by hand before it existed reported unlocked — the tool refuses to invent a lock by adopting whatever the remote currently serves, since that would assert the local copy is the one being served, which is the thing it was about to check. Locking them fetched two that had moved: bedrock/starting.md, which now says the worked example is private and will not link to something you cannot fetch, and cart.md, which went to v1. cart v1 changes a role we cast: a cart is not committed, because a committed cart grows a third file by itself — version control does not require anybody to ask, so the two-file rule is never invoked — and because ignored, gone means gone. Adds .loom/cart/ to .gitignore and supersedes the isolation entry rather than editing it. osprey and marmalade are already in history and are left there: rewriting to honour a rule adopted afterwards costs more than it buys. Records the conflict this creates rather than settling it: the annotation protocol here says commit before dissolving because git is the only archive, and an ignored cart has no archive, so dissolving would destroy the annotations outright. Credentials are read-only, per host, and passed to git through the environment rather than argv, because argv is visible to every process on the machine. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018UTxuSizozEA8yDitPuris
108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package external
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"git.hypertheory-labs.dev/loom/loom-cli/internal/config"
|
|
)
|
|
|
|
// PublishedDir is the only directory in somebody else's repository that list
|
|
// looks at. What is not exported is not hidden — it is simply not what you
|
|
// depend on.
|
|
const PublishedDir = ".loom/published"
|
|
|
|
// List enumerates a publisher's published surface.
|
|
//
|
|
// It shells out to git rather than using a host's REST API, because git is the
|
|
// one interface gitea, GitHub and GitLab all speak the same way: their contents
|
|
// APIs have three different URL shapes, three JSON shapes and three auth
|
|
// schemes, and a private repository refuses the anonymous ones. The cost is that
|
|
// git must be on PATH.
|
|
func List(repoURL string, out io.Writer) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dir, err := os.MkdirTemp("", "loomctl-list-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
clone := exec.Command("git", "clone",
|
|
"--filter=blob:none", // trees only: we want names, not contents
|
|
"--depth=1", // and bound the damage if the server ignores the filter
|
|
"--no-checkout",
|
|
"--quiet",
|
|
repoURL, dir,
|
|
)
|
|
clone.Env = gitEnv(cfg, repoURL)
|
|
var stderr bytes.Buffer
|
|
clone.Stderr = &stderr
|
|
if err := clone.Run(); err != nil {
|
|
return fmt.Errorf("git clone: %w\n%s", err, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
// git's fallback when a server refuses the filter is silent apart from this
|
|
// warning, and the fallback is to download everything.
|
|
if strings.Contains(stderr.String(), "filtering not recognized by server") {
|
|
fmt.Fprintf(out, "warning: %s ignored --filter, so this fetched every blob at HEAD\n\n", repoURL)
|
|
}
|
|
|
|
ls := exec.Command("git", "-C", dir, "ls-tree", "--name-only", "HEAD:"+PublishedDir)
|
|
var names, lsErr bytes.Buffer
|
|
ls.Stdout, ls.Stderr = &names, &lsErr
|
|
if err := ls.Run(); err != nil {
|
|
fmt.Fprintf(out, "%s publishes nothing — no %s\n", repoURL, PublishedDir)
|
|
return nil
|
|
}
|
|
|
|
for _, n := range strings.Split(strings.TrimSpace(names.String()), "\n") {
|
|
if n != "" {
|
|
fmt.Fprintln(out, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// gitEnv passes credentials to git through the environment rather than through
|
|
// -c on the command line, because argv is visible to every process on the
|
|
// machine and an environment is not.
|
|
func gitEnv(cfg *config.Config, repoURL string) []string {
|
|
env := append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
host := hostOf(repoURL)
|
|
if host == "" {
|
|
return env
|
|
}
|
|
t := cfg.TokenFor(host)
|
|
if t == "" {
|
|
return env
|
|
}
|
|
return append(env,
|
|
"GIT_CONFIG_COUNT=1",
|
|
"GIT_CONFIG_KEY_0=http.extraHeader",
|
|
"GIT_CONFIG_VALUE_0=Authorization: token "+t,
|
|
)
|
|
}
|
|
|
|
func hostOf(raw string) string {
|
|
i := strings.Index(raw, "://")
|
|
if i < 0 {
|
|
return ""
|
|
}
|
|
rest := raw[i+3:]
|
|
if at := strings.Index(rest, "@"); at >= 0 {
|
|
rest = rest[at+1:]
|
|
}
|
|
if s := strings.IndexAny(rest, "/:"); s >= 0 {
|
|
rest = rest[:s]
|
|
}
|
|
return rest
|
|
}
|