From 57d4d93ed097d8abe037785a320a99a5a81c5de4 Mon Sep 17 00:00:00 2001 From: Peng Guanwen Date: Sat, 3 Dec 2022 14:55:31 +0800 Subject: [PATCH] Add dedup option --- CHANGELOG.md | 2 ++ docs/src/index.md | 11 ++++++----- src/PkgDependency.jl | 17 ++++++++++------- test/runtests.jl | 12 +++++++----- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6904c8..4877374 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Option to show the repo link ([#11]) +- Option to not cut duplicate branches in tree ([#10]) ### Fixed @@ -55,5 +56,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#5]: https://github.com/peng1999/PkgDependency.jl/issues/5 [#6]: https://github.com/peng1999/PkgDependency.jl/issues/6 [#7]: https://github.com/peng1999/PkgDependency.jl/issues/7 +[#10]: https://github.com/peng1999/PkgDependency.jl/issues/10 [#11]: https://github.com/peng1999/PkgDependency.jl/issues/11 diff --git a/docs/src/index.md b/docs/src/index.md index bea13b3..4146bd0 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -20,11 +20,12 @@ julia> PkgDependency.tree("Tables") Unless otherwise specified, all methods of `tree` function support following kwargs: -| kwarg | description | -| --- | --- | -| `reverse=true` | get a reverse dependency tree | -| `compat=true` | show compat info in tree | -| `show_link=true` | show packages' repo link in tree | +| kwarg | default | description | +| --- | --- | --- | +| `reverse` | `false` | get a reverse dependency tree | +| `compat` | `false` | show compat info in tree | +| `show_link` | `false` | show packages' repo link in tree | +| `dedup` | `true` | hide duplicate dependencies in tree | ## API diff --git a/src/PkgDependency.jl b/src/PkgDependency.jl index ae051c1..6aae82f 100644 --- a/src/PkgDependency.jl +++ b/src/PkgDependency.jl @@ -10,7 +10,7 @@ function __init__() end """ - tree(...; reverse=false, compat=false, show_link=false) + tree(...; reverse=false, compat=false, show_link=false, dedup=true) """ function tree end @@ -20,7 +20,7 @@ function tree end Print dependency tree of current project. `reverse` kwarg is not supported in this method. """ -function tree(; compat=false, show_link=false) +function tree(; compat=false, show_link=false, dedup=true) project = Pkg.project() if project.ispackage name = something(project.name, "Unnamed Project") @@ -31,7 +31,7 @@ function tree(; compat=false, show_link=false) end registries = check_and_get_registries(; show_link) - Tree(builddict(project.uuid, project; compat, registries), title="$name $version") + Tree(builddict(project.uuid, project; compat, registries, dedup), title="$name $version") end """ @@ -39,7 +39,7 @@ end Print dependency tree of a package identified by UUID """ -function tree(uuid::UUID; reverse=false, compat=false, show_link=false) +function tree(uuid::UUID; reverse=false, compat=false, show_link=false, dedup=true) graph = Pkg.dependencies() if reverse revgraph = Pkg.dependencies() @@ -59,7 +59,7 @@ function tree(uuid::UUID; reverse=false, compat=false, show_link=false) # registries is used to find url registries = check_and_get_registries(; show_link) - Tree(builddict(uuid, project; graph, compat, registries), title="$name v$version") + Tree(builddict(uuid, project; graph, compat, registries, dedup), title="$name v$version") end """ @@ -110,7 +110,7 @@ compatstr(c::String) = c compatstr(c::Any) = c.str # returns dependencies of info as OrderedDict, or nothing when no dependencies -function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), listed=Set{UUID}(), compat=false, registries=nothing) +function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), listed=Set{UUID}(), dedup=true, compat=false, registries=nothing) deps = info.dependencies compats = compat && !isnothing(uuid) ? compatinfo(uuid) : Dict() children = OrderedDict() @@ -143,7 +143,10 @@ function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), li child = nothing if uuid ∉ listed push!(listed, uuid) - child = builddict(uuid, subpkg; graph, listed, compat, registries) + child = builddict(uuid, subpkg; graph, listed, compat, registries, dedup) + if !dedup + pop!(listed, uuid) + end end push!(children, name => child) end diff --git a/test/runtests.jl b/test/runtests.jl index d312edc..b0134dc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,14 +3,16 @@ using PkgDependency import Term: Tree @testset "Make sure function runs without error" begin + for dedup in [true; false] for compat in [true; false] - @test PkgDependency.tree(; compat) isa Tree - @test PkgDependency.tree("Term"; compat) isa Tree - @test PkgDependency.tree("Term"; reverse=true, compat) isa Tree + @test PkgDependency.tree(; compat, dedup) isa Tree + @test PkgDependency.tree("Term"; compat, dedup) isa Tree + @test PkgDependency.tree("Term"; reverse=true, compat, dedup) isa Tree if VERSION < v"1.7" - @test_throws ErrorException PkgDependency.tree("Term"; compat, show_link=true) isa Tree + @test_throws ErrorException PkgDependency.tree("Term"; compat, show_link=true, dedup) isa Tree else - @test PkgDependency.tree("Term"; compat, show_link=true) isa Tree + @test PkgDependency.tree("Term"; compat, show_link=true, dedup) isa Tree end end + end end