Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

estimate: show duplicated modules import path and repository path, but dimmed #241

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 55 additions & 32 deletions estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,50 +174,73 @@ func estimate(importpath string) error {
// Analyse the dependency graph
var lines []string
seen := make(map[string]bool)
rrseen := make(map[string]bool)
needed := make(map[string]int)
var visit func(n *Node, indent int)
visit = func(n *Node, indent int) {
// Get the module name without its version, as go mod graph
// can return multiple times the same module with different
// versions.
mod, _, _ := strings.Cut(n.name, "@")
if seen[mod] {
count, isNeeded := needed[mod]
if isNeeded {
count++
needed[mod] = count
lines = append(lines, fmt.Sprintf("%s\033[90m%s (%d)\033[0m", strings.Repeat(" ", indent), mod, count))
} else if seen[mod] {
return
}
seen[mod] = true
// Go version dependency is indicated as a dependency to "go" and
// "toolchain", we do not use this information for now.
if mod == "go" || mod == "toolchain" {
return
}
if _, ok := golangBinaries[mod]; ok {
return // already packaged in Debian
}
var debianVersion string
// Check for potential other major versions already in Debian.
for _, otherVersion := range otherVersions(mod) {
if _, ok := golangBinaries[otherVersion]; ok {
debianVersion = otherVersion
break
} else {
seen[mod] = true
// Go version dependency is indicated as a dependency to "go" and
// "toolchain", we do not use this information for now.
if mod == "go" || mod == "toolchain" {
return
}
}
if debianVersion == "" {
// When multiple modules are developped in the same repo,
// the repo root is often used as the import path metadata
// in Debian, so we do a last try with that.
if _, ok := golangBinaries[mod]; ok {
return // already packaged in Debian
}
var repoRoot string
rr, err := vcs.RepoRootForImportPath(mod, false)
if err != nil {
log.Printf("Could not determine repo path for import path %q: %v\n", mod, err)
} else if _, ok := golangBinaries[rr.Root]; ok {
// Log info to indicate that it is an approximate match
// but consider that it is packaged and skip the children.
log.Printf("%s is packaged as %s in Debian", mod, rr.Root)
return
repoRoot = mod
} else {
repoRoot = rr.Root
}
}
if debianVersion != "" {
lines = append(lines, fmt.Sprintf("%s%s\t(%s in Debian)", strings.Repeat(" ", indent), mod, debianVersion))
} else {
lines = append(lines, fmt.Sprintf("%s%s", strings.Repeat(" ", indent), mod))
var debianVersion string
// Check for potential other major versions already in Debian.
for _, otherVersion := range otherVersions(mod) {
if _, ok := golangBinaries[otherVersion]; ok {
debianVersion = otherVersion
break
}
}
if debianVersion == "" {
// When multiple modules are developped in the same repo,
// the repo root is often used as the import path metadata
// in Debian, so we do a last try with that.
if _, ok := golangBinaries[repoRoot]; ok {
// Log info to indicate that it is an approximate match
// but consider that it is packaged and skip the children.
log.Printf("%s is packaged as %s in Debian", mod, repoRoot)
return
}
}
line := strings.Repeat(" ", indent)
if rrseen[repoRoot] {
line += fmt.Sprintf("\033[90m%s\033[0m", mod)
} else if strings.HasPrefix(mod, repoRoot) && len(mod) > len(repoRoot) {
suffix := mod[len(repoRoot):]
line += fmt.Sprintf("%s\033[90m%s\033[0m", repoRoot, suffix)
} else {
line += mod
}
if debianVersion != "" {
line += fmt.Sprintf("\t(%s in Debian)", debianVersion)
}
lines = append(lines, line)
rrseen[repoRoot] = true
needed[mod] = 1
}
for _, n := range n.children {
visit(n, indent+1)
Expand Down
Loading