Files
mgsh/completion.go
T
mikeandClaude Opus 5 915ef1783a Add release: publish tagged releases on the mirror servers
`release [@name ...] <tag> [notes]` does the whole chain in one step —
create the annotated tag, push it to the internal server, then push it to
each selected mirror and turn it into a release object there. Target
selection reuses pushremote's @name mechanism, so the two behave alike.

Notes are generated when none are given: the tag's own annotation when it
carries more than the default, otherwise the commit subjects since the
previous tag, capped at 50 lines. `tag add v1.0 "why this exists"` now
takes a message, which is what that fallback reads; before, the
annotation was always just the tag name.

Tags ending in -rc/-alpha/-beta/-pre are marked as pre-releases on Gitea
and GitHub. Releasing the same tag twice updates the existing release;
a tag that already points at a different commit stops the command, since
moving a published tag makes one version mean different things per
server. A repository that is not on the mirror yet is reported instead of
being created as a side effect.

Binary assets are deliberately out of scope: Gitea attaches them to the
release, GitHub uses a separate upload host, and GitLab does not host
them at all but wants a link into its package registry.

The providers differ in path shape and field names -- GitLab addresses
projects by URL-encoded path, calls the notes "description" and has no
pre-release flag -- so this comes with a recording httptest stand-in that
asserts the exact requests for all three. That harness also covers
authUser, repoExists and the auth header forms, which had no test at all.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 15:47:05 +02:00

188 lines
5.2 KiB
Go

package main
import (
"os"
"path/filepath"
"strings"
"github.com/chzyer/readline"
)
// completer wires up Tab completion. Command names complete at the start of the
// line; cd/open/view complete local project names; clone/show complete
// repository names cached from the git server; checkout/tag complete branch and
// tag names; dist completes filesystem paths.
func completer() *readline.PrefixCompleter {
return readline.NewPrefixCompleter(
readline.PcItem("cd", readline.PcItemDynamic(dynLocalProjects)),
readline.PcItem("open", readline.PcItemDynamic(dynLocalProjects)),
readline.PcItem("view", readline.PcItemDynamic(dynLocalProjects)),
readline.PcItem("clone",
readline.PcItem("-a", readline.PcItemDynamic(dynServerArchives)),
readline.PcItemDynamic(dynServerRepos),
),
readline.PcItem("cloneall"),
readline.PcItem("show", readline.PcItemDynamic(dynServerRepos)),
readline.PcItem("list", readline.PcItem("-a")),
readline.PcItem("push"),
readline.PcItem("pushremote", readline.PcItemDynamic(dynRemoteNames)),
readline.PcItem("release", readline.PcItemDynamic(dynRemoteNames)),
readline.PcItem("pull"),
readline.PcItem("fetch"),
readline.PcItem("status", readline.PcItem("-a")),
readline.PcItem("overview"),
readline.PcItem("diff"),
readline.PcItem("init"),
readline.PcItem("edit"),
readline.PcItem("checkout", readline.PcItemDynamic(dynCheckout)),
readline.PcItem("log"),
readline.PcItem("archive"),
readline.PcItem("dist", readline.PcItemDynamic(dynPaths)),
readline.PcItem("login"),
readline.PcItem("count"),
readline.PcItem("tag",
readline.PcItem("add"),
readline.PcItem("checkout", readline.PcItemDynamic(dynTags)),
readline.PcItem("delete", readline.PcItemDynamic(dynTags)),
),
readline.PcItem("alias", readline.PcItemDynamic(dynAliasNames)),
readline.PcItem("unalias", readline.PcItemDynamic(dynAliasNames)),
readline.PcItem("config", readline.PcItem("-k")),
readline.PcItem("rescan"),
readline.PcItem("help"),
readline.PcItem("quit"),
readline.PcItem("exit"),
)
}
// dynLocalProjects lists project directories under BASE (already name-sorted).
func dynLocalProjects(string) []string {
entries, err := os.ReadDir(BASE)
if err != nil {
return nil
}
var out []string
for _, e := range entries {
if e.IsDir() && !strings.HasPrefix(e.Name(), ".") {
out = append(out, e.Name())
}
}
return out
}
var (
serverRepos []string
serverArchives []string
serverFetched bool
)
// fetchServerRepos queries the server once and caches the repository/archive
// names for Tab completion.
func fetchServerRepos() {
if serverFetched {
return
}
lines, err := sshOut("/bin/ls .")
if err != nil {
// a transient failure (server down, no network) must not cache an
// empty list for the rest of the session — the next Tab tries again
return
}
var repos []string
for _, ln := range lines {
if m := gitDirRe.FindStringSubmatch(strings.TrimSpace(ln)); m != nil {
repos = append(repos, m[1])
}
}
// a missing ./archive is a permanent, unremarkable state: still cache
var archives []string
if lines, err := sshOut("/bin/ls archive"); err == nil {
for _, ln := range lines {
t := strings.TrimSpace(ln)
if strings.HasSuffix(t, ".git.tar.gz") {
archives = append(archives, strings.TrimSuffix(t, ".git.tar.gz"))
}
}
}
serverRepos, serverArchives = repos, archives
serverFetched = true
}
// rescanServer clears the cached server listing so the next completion (or use)
// re-fetches it.
func rescanServer() {
serverFetched = false
serverRepos = nil
serverArchives = nil
}
func dynServerRepos(string) []string { fetchServerRepos(); return serverRepos }
func dynServerArchives(string) []string { fetchServerRepos(); return serverArchives }
// dynRemoteNames offers the configured mirror targets as `@name` selectors for
// `pushremote`, resolved against the active project's configuration.
func dynRemoteNames(string) []string {
targets, _ := cfg.mirrorTargets()
var out []string
for _, t := range targets {
out = append(out, "@"+t.Name)
}
return out
}
// dynBranches / dynTags list the active project's local branches / tags.
func dynBranches(string) []string {
if !isDir(DIR + "/.git") {
return nil
}
out, _ := gitCapture(DIR, "for-each-ref", "--format=%(refname:short)", "refs/heads")
return splitLines(out)
}
func dynTags(string) []string {
if !isDir(DIR + "/.git") {
return nil
}
out, _ := gitCapture(DIR, "tag", "-l")
return splitLines(out)
}
// dynCheckout offers both branches and tags for `checkout`.
func dynCheckout(line string) []string {
return append(dynBranches(line), dynTags(line)...)
}
// dynPaths completes filesystem paths for the last token on the line.
func dynPaths(line string) []string {
partial := ""
if !strings.HasSuffix(line, " ") {
if f := strings.Fields(line); len(f) > 0 {
partial = f[len(f)-1]
}
}
dir := "."
if partial != "" {
if strings.HasSuffix(partial, "/") {
dir = partial
} else {
dir = filepath.Dir(partial)
}
}
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var out []string
for _, e := range entries {
if strings.HasPrefix(e.Name(), ".") {
continue
}
p := filepath.Join(dir, e.Name())
if e.IsDir() {
p += "/"
}
out = append(out, p)
}
return out
}