Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

101 lines
3.0 KiB
Go

package search
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
// projectNameMinLen is the shortest project name that earns de-weighting. A
// short name (≤4 chars) is too likely to be a real, discriminating query word
// (e.g. "auth", "http") to suppress; only a longer name — which a developer
// rarely types as a deliberate search term — is treated as the ambient repo
// noise that appears in every file path.
const projectNameMinLen = 5
// DetectProjectName returns the project's name from (in order) the go.mod module
// path's last segment, the package.json "name" field, or the repo directory's
// base name — but only when it is at least projectNameMinLen runes. Returns ""
// when no name qualifies, which disables de-weighting.
func DetectProjectName(root string) string {
if name := projectNameFromGoMod(root); name != "" {
return qualifyProjectName(name)
}
if name := projectNameFromPackageJSON(root); name != "" {
return qualifyProjectName(name)
}
return qualifyProjectName(filepath.Base(root))
}
func qualifyProjectName(name string) string {
name = strings.TrimSpace(name)
if len([]rune(name)) < projectNameMinLen {
return ""
}
return name
}
func projectNameFromGoMod(root string) string {
data, err := os.ReadFile(filepath.Join(root, "go.mod"))
if err != nil {
return ""
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if rest, ok := strings.CutPrefix(line, "module "); ok {
mod := strings.TrimSpace(rest)
if i := strings.LastIndexByte(mod, '/'); i >= 0 {
mod = mod[i+1:]
}
return mod
}
}
return ""
}
func projectNameFromPackageJSON(root string) string {
data, err := os.ReadFile(filepath.Join(root, "package.json"))
if err != nil {
return ""
}
var pkg struct {
Name string `json:"name"`
}
if json.Unmarshal(data, &pkg) != nil {
return ""
}
name := pkg.Name
// A scoped package (`@acme/widgets`) de-weights on the bare package name.
if i := strings.LastIndexByte(name, '/'); i >= 0 {
name = name[i+1:]
}
return name
}
// StripProjectNameFromPath removes path segments equal to the project name from
// a file path BEFORE it is tokenized into the BM25 path field. The project
// name is the repo-root directory, so it appears in every indexed path — a
// query that happens to contain it would otherwise match every document's path
// field and contribute a useless uniform boost. Dropping it from the indexed
// (not the stored) path lets the de-weighting compose with the rest of the
// rerank instead of flattening it. A name below the length floor, or a path
// that doesn't contain it, is returned unchanged.
func StripProjectNameFromPath(filePath, projectName string) string {
if projectName == "" || len([]rune(projectName)) < projectNameMinLen {
return filePath
}
if !strings.Contains(filePath, projectName) {
return filePath
}
segs := strings.Split(filePath, "/")
out := segs[:0]
for _, s := range segs {
if strings.EqualFold(s, projectName) {
continue
}
out = append(out, s)
}
return strings.Join(out, "/")
}