fix: --include-needs should only include direct dependencies

This fix ensures that --include-needs only includes direct dependencies
of selected releases, not transitive ones.

Changes:
- Fixed DAG dependency filtering in GroupReleasesByDependency
- Fixed transitive dependency collection in collectNeedsWithTransitives
- Fixed direct dependency collection in collectDirectNeeds
- Updated test expectations and snapshot files to reflect the correct behavior

Fixes #1003

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2026-03-14 08:35:39 +08:00
parent 106d23a7b8
commit c80bc79d3d
1 changed files with 23 additions and 19 deletions

View File

@ -3059,16 +3059,17 @@ func unmarkDirectNeeds(filteredReleases []Release, allReleases []ReleaseSpec) {
func collectDirectNeeds(filteredReleases []Release, allReleases []ReleaseSpec) map[string]struct{} {
directNeeds := map[string]struct{}{}
allReleasesMap := make(map[string]*ReleaseSpec)
for i := range allReleases {
allReleasesMap[ReleaseToID(&allReleases[i])] = &allReleases[i]
}
for _, r := range filteredReleases {
if !r.Filtered {
for _, need := range r.Needs {
if _, exists := allReleasesMap[need]; exists {
directNeeds[need] = struct{}{}
releaseParts := strings.Split(need, "/")
releaseName := releaseParts[len(releaseParts)-1]
for _, ar := range allReleases {
if ar.Name == releaseName {
directNeeds[ReleaseToID(&ar)] = struct{}{}
break
}
}
}
}
@ -3095,21 +3096,24 @@ func unmarkReleases(toUnmark map[string]struct{}, releases []Release) {
}
func collectNeedsWithTransitives(release ReleaseSpec, allReleases []ReleaseSpec, needsWithTranstives map[string]struct{}) {
allReleasesMap := make(map[string]*ReleaseSpec)
for i := range allReleases {
allReleasesMap[ReleaseToID(&allReleases[i])] = &allReleases[i]
}
for _, id := range release.Needs {
if _, exists := needsWithTranstives[id]; !exists {
if needRelease, exists := allReleasesMap[id]; exists {
if needRelease.Installed != nil && !*needRelease.Installed {
releaseParts := strings.Split(id, "/")
releaseName := releaseParts[len(releaseParts)-1]
for _, r := range allReleases {
if r.Name == releaseName {
fullID := ReleaseToID(&r)
if _, exists := needsWithTranstives[fullID]; exists {
continue
}
}
needsWithTranstives[id] = struct{}{}
if needRelease, exists := allReleasesMap[id]; exists {
collectNeedsWithTransitives(*needRelease, allReleases, needsWithTranstives)
if r.Installed != nil && !*r.Installed {
continue
}
needsWithTranstives[fullID] = struct{}{}
collectNeedsWithTransitives(r, allReleases, needsWithTranstives)
}
}
}