fix(search): preserve canonical repo source in grouped installs

Use a repo-root source in grouped search installs by normalizing from CloneURL instead of carrying subdir Raw values. This prevents malformed metadata sources (for example duplicated subdir paths) and keeps provider-agnostic behavior for GitHub/GitLab/Bitbucket/Azure and SSH URLs.

Also centralize discovery source formatting for metadata so whole-repo SSH installs use //subdir syntax consistently.

Add regression coverage:
- cmd unit tests for grouped clone source normalization across providers
- install unit tests for discovery source formatting (including SSH)
- integration test in tests/integration that verifies grouped install metadata source round-trips through ParseSource.
This commit is contained in:
Willie
2026-02-27 05:39:49 +08:00
parent 5a111ce8f6
commit 0a28fcdbdb
5 changed files with 301 additions and 7 deletions
+19 -4
View File
@@ -264,6 +264,24 @@ type sourceGroup struct {
results []search.SearchResult // all results sharing this clone URL
}
// repoSourceForGroupedClone converts a subdir source into a repo-root source.
// This keeps provider-specific details (GitLab/Bitbucket/Azure/GitHub) by
// parsing CloneURL directly, then ensures Subdir is cleared for whole-repo clone.
func repoSourceForGroupedClone(src *install.Source) install.Source {
repoSource := *src
repoSource.Subdir = ""
repoSource.Raw = repoSource.CloneURL
// Re-parse CloneURL to get a canonical root-level name/type.
if root, err := install.ParseSource(repoSource.CloneURL); err == nil {
repoSource.Type = root.Type
repoSource.Raw = root.Raw
repoSource.Name = root.Name
}
return repoSource
}
// groupByRepo partitions selected search results by CloneURL.
// Results that share the same git repo are grouped together for a single clone.
// Results that cannot be grouped (parse failure, local path, non-subdir singles)
@@ -478,10 +496,7 @@ func batchInstallFromSearchWithProgress(selected []search.SearchResult, mode run
// Phase 1: grouped install — clone each repo once, install multiple skills.
for _, group := range groups {
// Build a whole-repo source (no Subdir) for cloning.
// Copy the parsed source and clear Subdir so the clone fetches the
// entire repo rather than a single subdirectory.
repoSource := *group.source
repoSource.Subdir = ""
repoSource := repoSourceForGroupedClone(group.source)
// Use the first skill's name for clone progress display.
cloneLabel := group.results[0].Name
+68
View File
@@ -3,6 +3,8 @@ package main
import (
"strings"
"testing"
"skillshare/internal/install"
)
func TestClassifyFailureDetail(t *testing.T) {
@@ -102,3 +104,69 @@ func TestClassifyFailureDetail(t *testing.T) {
})
}
}
func TestRepoSourceForGroupedClone(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "github shorthand subdir",
input: "openai/skills/skills/react",
},
{
name: "gitlab web tree url",
input: "https://gitlab.com/team/repo/-/tree/main/skills/docker",
},
{
name: "bitbucket web src url",
input: "https://bitbucket.org/team/repo/src/main/skills/vue",
},
{
name: "azure devops https subdir",
input: "https://dev.azure.com/org/project/_git/repo/skills/node",
},
{
name: "git ssh subdir",
input: "git@gitlab.com:team/monorepo.git//frontend/ui-skill",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
src, err := install.ParseSource(tt.input)
if err != nil {
t.Fatalf("ParseSource(%q) error: %v", tt.input, err)
}
if src.Subdir == "" {
t.Fatalf("test input must include subdir, got empty for %q", tt.input)
}
repo := repoSourceForGroupedClone(src)
if repo.Subdir != "" {
t.Errorf("repo.Subdir = %q, want empty", repo.Subdir)
}
if repo.Raw == "" {
t.Fatal("repo.Raw should not be empty")
}
if repo.CloneURL != src.CloneURL {
t.Errorf("repo.CloneURL = %q, want %q", repo.CloneURL, src.CloneURL)
}
if repo.Name == "" {
t.Fatal("repo.Name should not be empty")
}
parsedRoot, err := install.ParseSource(repo.Raw)
if err != nil {
t.Fatalf("repo.Raw should be parseable as root source, got error: %v (raw=%q)", err, repo.Raw)
}
if parsedRoot.Subdir != "" {
t.Errorf("parsed root source should not have subdir, got %q", parsedRoot.Subdir)
}
if parsedRoot.CloneURL != src.CloneURL {
t.Errorf("parsed root CloneURL = %q, want %q", parsedRoot.CloneURL, src.CloneURL)
}
})
}
}
+19 -3
View File
@@ -6,6 +6,22 @@ import (
"path/filepath"
)
// buildDiscoverySkillSource constructs metadata Source string for a skill
// selected from a discovery result.
func buildDiscoverySkillSource(source *Source, skillPath string) string {
if skillPath == "." {
return source.Raw
}
if source.HasSubdir() {
return source.Raw + "/" + skillPath
}
// Whole-repo SSH sources encode subdir using //path.
if source.Type == SourceTypeGitSSH {
return source.Raw + "//" + skillPath
}
return source.Raw + "/" + skillPath
}
func installImpl(source *Source, destPath string, opts InstallOptions) (*InstallResult, error) {
result := &InstallResult{
SkillName: source.Name,
@@ -148,15 +164,15 @@ func installFromDiscoveryImpl(discovery *DiscoveryResult, skill SkillInfo, destP
if skill.Path == "." {
// Root skill of a subdir discovery
fullSource = discovery.Source.Raw
fullSource = buildDiscoverySkillSource(discovery.Source, skill.Path)
fullSubdir = discovery.Source.Subdir
} else if discovery.Source.HasSubdir() {
// Nested skill within subdir discovery
fullSource = discovery.Source.Raw + "/" + skill.Path
fullSource = buildDiscoverySkillSource(discovery.Source, skill.Path)
fullSubdir = discovery.Source.Subdir + "/" + skill.Path
} else {
// Whole-repo discovery
fullSource = discovery.Source.Raw + "/" + skill.Path
fullSource = buildDiscoverySkillSource(discovery.Source, skill.Path)
fullSubdir = skill.Path
}
+56
View File
@@ -65,6 +65,62 @@ func TestInstallFromDiscovery_FullSubdirUsesForwardSlashes(t *testing.T) {
}
}
func TestBuildDiscoverySkillSource(t *testing.T) {
tests := []struct {
name string
source *Source
skill string
expected string
}{
{
name: "https whole repo",
source: &Source{
Type: SourceTypeGitHTTPS,
Raw: "https://gitlab.com/team/repo.git",
},
skill: "skills/docker",
expected: "https://gitlab.com/team/repo.git/skills/docker",
},
{
name: "ssh whole repo uses double slash",
source: &Source{
Type: SourceTypeGitSSH,
Raw: "git@gitlab.com:team/repo.git",
},
skill: "frontend/ui-skill",
expected: "git@gitlab.com:team/repo.git//frontend/ui-skill",
},
{
name: "ssh with source subdir keeps single slash for nested skill",
source: &Source{
Type: SourceTypeGitSSH,
Raw: "git@gitlab.com:team/repo.git//skills",
Subdir: "skills",
},
skill: "ui-skill",
expected: "git@gitlab.com:team/repo.git//skills/ui-skill",
},
{
name: "root skill keeps raw",
source: &Source{
Type: SourceTypeGitHub,
Raw: "openai/skills/skills",
},
skill: ".",
expected: "openai/skills/skills",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildDiscoverySkillSource(tt.source, tt.skill)
if got != tt.expected {
t.Errorf("buildDiscoverySkillSource() = %q, want %q", got, tt.expected)
}
})
}
}
func TestComputeFileHashes_Basic(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# Hello"), 0644)
@@ -0,0 +1,139 @@
//go:build !online
package integration
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"skillshare/internal/install"
"skillshare/internal/search"
"skillshare/internal/testutil"
)
func TestSearchBatchGroupedInstall_MetadataSourceParseRoundTrip(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + "\ntargets: {}\n")
repoPath := filepath.Join(sb.Root, "monorepo")
if err := os.MkdirAll(filepath.Join(repoPath, "skills", "alpha-skill"), 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(repoPath, "skills", "beta-skill"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repoPath, "skills", "alpha-skill", "SKILL.md"), []byte("# alpha"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(repoPath, "skills", "beta-skill", "SKILL.md"), []byte("# beta"), 0644); err != nil {
t.Fatal(err)
}
initGitRepo(t, repoPath)
// Route GitLab clone URL to the local test repo so the test remains offline.
configureGitURLRewriteOrSkip(t, sb.Home, repoPath, "https://gitlab.com/team/monorepo.git")
selected := []search.SearchResult{
{
Name: "alpha-skill",
Source: "https://gitlab.com/team/monorepo/-/tree/main/skills/alpha-skill",
},
{
Name: "beta-skill",
Source: "https://gitlab.com/team/monorepo/-/tree/main/skills/beta-skill",
},
}
// Simulate grouped search install: clone repo once, install both skills from discovery.
firstSource, err := install.ParseSource(selected[0].Source)
if err != nil {
t.Fatalf("parse first source: %v", err)
}
repoSource := repoSourceForGroupedCloneForTest(firstSource)
discovery, err := install.DiscoverFromGitWithProgress(&repoSource, nil)
if err != nil {
t.Fatalf("discover from grouped clone source: %v", err)
}
defer install.CleanupDiscovery(discovery)
for _, sr := range selected {
src, parseErr := install.ParseSource(sr.Source)
if parseErr != nil {
t.Fatalf("parse source %q: %v", sr.Source, parseErr)
}
skill, ok := findSkillBySubdir(discovery, src.Subdir)
if !ok {
t.Fatalf("skill subdir %q not found in discovery", src.Subdir)
}
destPath := filepath.Join(sb.SourcePath, sr.Name)
if _, installErr := install.InstallFromDiscovery(discovery, skill, destPath, install.InstallOptions{}); installErr != nil {
t.Fatalf("install %s from discovery failed: %v", sr.Name, installErr)
}
}
for _, name := range []string{"alpha-skill", "beta-skill"} {
skillPath := filepath.Join(sb.SourcePath, name)
meta, err := install.ReadMeta(skillPath)
if err != nil {
t.Fatalf("read meta for %s: %v", name, err)
}
if meta == nil {
t.Fatalf("meta missing for %s", name)
}
parsed, err := install.ParseSource(meta.Source)
if err != nil {
t.Fatalf("meta source for %s is not parseable: %q (%v)", name, meta.Source, err)
}
if parsed.CloneURL != "https://gitlab.com/team/monorepo.git" {
t.Fatalf("unexpected clone URL for %s: got %q", name, parsed.CloneURL)
}
wantSubdir := "skills/" + name
if parsed.Subdir != wantSubdir {
t.Fatalf("unexpected subdir for %s: got %q, want %q (source=%q)", name, parsed.Subdir, wantSubdir, meta.Source)
}
}
}
func repoSourceForGroupedCloneForTest(src *install.Source) install.Source {
repoSource := *src
repoSource.Subdir = ""
repoSource.Raw = repoSource.CloneURL
if root, err := install.ParseSource(repoSource.CloneURL); err == nil {
repoSource.Type = root.Type
repoSource.Raw = root.Raw
repoSource.Name = root.Name
}
return repoSource
}
func findSkillBySubdir(discovery *install.DiscoveryResult, subdir string) (install.SkillInfo, bool) {
for _, sk := range discovery.Skills {
if sk.Path == subdir {
return sk, true
}
}
return install.SkillInfo{}, false
}
func configureGitURLRewriteOrSkip(t *testing.T, home, repoPath, remoteURL string) {
t.Helper()
key := fmt.Sprintf("url.file://%s/.insteadOf", repoPath)
cmd := exec.Command("git", "config", "--global", key, remoteURL)
cmd.Env = append(os.Environ(), "HOME="+home)
if out, err := cmd.CombinedOutput(); err != nil {
t.Skipf("git config unavailable: %v (%s)", err, string(out))
}
}