Documentation
¶
Overview ¶
Package project provides utilities for detecting and normalizing project names.
It replicates the detection logic from the Claude Code shell helpers and OpenCode TypeScript plugin in pure Go, so CLI and MCP server can share a single canonical implementation.
Index ¶
Constants ¶
const ( SourceGitRemote = "git_remote" // derived from git remote origin URL SourceGitRoot = "git_root" // derived from git repository root basename SourceGitChild = "git_child" // auto-promoted from single child git repo SourceDirBasename = "dir_basename" // fallback: directory basename SourceAmbiguous = "ambiguous" // cwd contains multiple git repos (Case 4) SourceExplicitOverride = "explicit_override" // JR2-2: caller explicitly supplied a project name SourceRequestBody = "request_body" // REQ-414: project came from the request body (server-side, no filesystem path) )
Source constants describe how the project name was resolved.
Variables ¶
var ErrAmbiguousProject = errors.New("ambiguous project: multiple git repos found in cwd")
ErrAmbiguousProject is returned when the working directory is a parent of multiple git repositories and we cannot auto-select one.
Functions ¶
func DetectProject ¶
DetectProject detects the project name for a given directory. Priority: git remote origin repo name → git root basename → dir basename. The returned name is always non-empty and already normalized (lowercase, trimmed). This function is a backward-compatible wrapper around DetectProjectFull. On ErrAmbiguousProject, falls back to filepath.Base(dir) so CLI callers never receive an empty string (design §9 backward-compat requirement).
Types ¶
type DetectionResult ¶ added in v1.13.0
type DetectionResult struct {
// Project is the resolved project name. Empty when Error==ErrAmbiguousProject.
Project string
// Source describes how the project name was derived.
Source string
// Path is the canonical directory associated with the project
// (repo root for git cases, input dir for dir_basename).
Path string
// Warning is a non-empty advisory message when Source==SourceGitChild.
Warning string
// Error is non-nil only for ErrAmbiguousProject.
Error error
// AvailableProjects is populated only when Error==ErrAmbiguousProject.
AvailableProjects []string
}
DetectionResult carries the full output of DetectProjectFull.
func DetectProjectFull ¶ added in v1.13.0
func DetectProjectFull(dir string) DetectionResult
DetectProjectFull resolves the project for dir using a 5-case algorithm:
- git_remote — cwd is a git root with a remote → derive name from remote URL
- git_root — cwd is inside a git repo → use repo root basename
- git_child — cwd has exactly one git-repo child → auto-promote it
- ambiguous — cwd has multiple git-repo children → return ErrAmbiguousProject
- dir_basename — none of the above → use filepath.Base(dir)
type ProjectMatch ¶
type ProjectMatch struct {
Name string // The existing project name
MatchType string // "case-insensitive", "substring", or "levenshtein"
Distance int // Levenshtein distance (0 for case-insensitive and substring matches)
}
ProjectMatch represents a project name that is similar to a query string.
func FindSimilar ¶
func FindSimilar(name string, existing []string, maxDistance int) []ProjectMatch
FindSimilar finds projects similar to the given name from a list of existing project names. Similarity is determined by three criteria:
- Case-insensitive exact match (different case, same letters)
- Substring containment (query is a substring of candidate or vice-versa)
- Levenshtein distance ≤ maxDistance
Exact matches (identical strings) are always excluded.
Results are ordered: case-insensitive matches first, then substring matches, then levenshtein matches sorted by distance ascending.