pactree: Fix tree drawing for --optional

Message ID 20200729233054.18118-1-jakseb.dev@gmail.com
State Accepted, archived
Headers show
Series pactree: Fix tree drawing for --optional | expand

Commit Message

Sebastian Jakubiak July 29, 2020, 11:30 p.m. UTC
Example of an incorrect behavior:

    $ pactree -a -d1 -o dia
    dia
    |-libxslt
    `-gtk2
    `-python2 (optional)

The last non-optional dependency, gtk2, has its limb drawn as if it were
the last child of a node, even though an optional dependency follows.

Signed-off-by: Sebastian Jakubiak <jakseb.dev@gmail.com>
---
 src/pactree.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

Comments

Daniel M. Capella July 30, 2020, 2:51 a.m. UTC | #1
On July 29, 2020 7:30:54 PM EDT, Sebastian Jakubiak <jakseb.dev@gmail.com> wrote:
> Example of an incorrect behavior:
> 
>     $ pactree -a -d1 -o dia
>     dia
>     |-libxslt
>     `-gtk2
>     `-python2 (optional)
> 
> The last non-optional dependency, gtk2, has its limb drawn as if it
> were
> the last child of a node, even though an optional dependency follows.
> 
> Signed-off-by: Sebastian Jakubiak <jakseb.dev@gmail.com>
> ---
>  src/pactree.c | 23 +++++++++++------------
>  1 file changed, 11 insertions(+), 12 deletions(-)
> 
> diff --git a/src/pactree.c b/src/pactree.c
> index 39328e7..e34ecc8 100644
> --- a/src/pactree.c
> +++ b/src/pactree.c
> @@ -505,7 +505,7 @@ static void walk_deps(alpm_list_t *dblist,
> alpm_pkg_t *pkg, tdepth *depth, int r
>  /**
> * print the dependency list given, passing the optional parameter when
> required
>   */
> -static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist,
> alpm_pkg_t *pkg, tdepth *depth, int rev, int optional, int opt_dep)
> +static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist,
> alpm_pkg_t *pkg, tdepth *depth, int rev, int optional, int opt_dep,
> int final)
>  {
>  	alpm_list_t *i;
>  	int new_optional;
> @@ -520,7 +520,7 @@ static void print_dep_list(alpm_list_t *deps,
> alpm_list_t *dblist, alpm_pkg_t *p
>  
>  	for(i = deps; i; i = alpm_list_next(i)) {
>  		const char *pkgname = i->data;
> -		int last = alpm_list_next(i) ? 0 : 1;
> +		int last = final && (alpm_list_next(i) ? 0 : 1);
>  
> 		alpm_pkg_t *dep_pkg = alpm_find_dbs_satisfier(handle, dblist,
> pkgname);
>  
> @@ -561,7 +561,7 @@ static void print_dep_list(alpm_list_t *deps,
> alpm_list_t *dblist, alpm_pkg_t *p
>   */
> static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth
> *depth, int rev, int optional)
>  {
> -	alpm_list_t *deps;
> +	alpm_list_t *deps, *optdeps;
>  
>  	if(!pkg || ((max_depth >= 0) && (depth->level > max_depth))) {
>  		return;
> @@ -575,21 +575,20 @@ static void walk_deps(alpm_list_t *dblist,
> alpm_pkg_t *pkg, tdepth *depth, int r
>  		deps = get_pkg_deps(pkg);
>  	}
>  
> -	print_dep_list(deps, dblist, pkg, depth, rev, optional, 0);
> -
> -	FREELIST(deps);
> -
> +	optdeps = NULL;
>  	if(optional){
>  		if(rev) {
> -			deps = alpm_pkg_compute_optionalfor(pkg);
> +			optdeps = alpm_pkg_compute_optionalfor(pkg);
>  		} else {
> -			deps = get_pkg_optdeps(pkg);
> +			optdeps = get_pkg_optdeps(pkg);
>  		}
> +	}
>  
> -		print_dep_list(deps, dblist, pkg, depth, rev, optional, 1);
> +	print_dep_list(deps, dblist, pkg, depth, rev, optional, 0,
> !optdeps);
> +	FREELIST(deps);
>  
> -		FREELIST(deps);
> -	}
> +	print_dep_list(optdeps, dblist, pkg, depth, rev, optional, 1, 1);
> +	FREELIST(optdeps);
>  }
>  
>  int main(int argc, char *argv[])

Merged, thanks for the quick fix.

--
Best,
Daniel <https://danielcapella.com>

Patch

diff --git a/src/pactree.c b/src/pactree.c
index 39328e7..e34ecc8 100644
--- a/src/pactree.c
+++ b/src/pactree.c
@@ -505,7 +505,7 @@  static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int r
 /**
  * print the dependency list given, passing the optional parameter when required
  */
-static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int rev, int optional, int opt_dep)
+static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int rev, int optional, int opt_dep, int final)
 {
 	alpm_list_t *i;
 	int new_optional;
@@ -520,7 +520,7 @@  static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist, alpm_pkg_t *p
 
 	for(i = deps; i; i = alpm_list_next(i)) {
 		const char *pkgname = i->data;
-		int last = alpm_list_next(i) ? 0 : 1;
+		int last = final && (alpm_list_next(i) ? 0 : 1);
 
 		alpm_pkg_t *dep_pkg = alpm_find_dbs_satisfier(handle, dblist, pkgname);
 
@@ -561,7 +561,7 @@  static void print_dep_list(alpm_list_t *deps, alpm_list_t *dblist, alpm_pkg_t *p
  */
 static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int rev, int optional)
 {
-	alpm_list_t *deps;
+	alpm_list_t *deps, *optdeps;
 
 	if(!pkg || ((max_depth >= 0) && (depth->level > max_depth))) {
 		return;
@@ -575,21 +575,20 @@  static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int r
 		deps = get_pkg_deps(pkg);
 	}
 
-	print_dep_list(deps, dblist, pkg, depth, rev, optional, 0);
-
-	FREELIST(deps);
-
+	optdeps = NULL;
 	if(optional){
 		if(rev) {
-			deps = alpm_pkg_compute_optionalfor(pkg);
+			optdeps = alpm_pkg_compute_optionalfor(pkg);
 		} else {
-			deps = get_pkg_optdeps(pkg);
+			optdeps = get_pkg_optdeps(pkg);
 		}
+	}
 
-		print_dep_list(deps, dblist, pkg, depth, rev, optional, 1);
+	print_dep_list(deps, dblist, pkg, depth, rev, optional, 0, !optdeps);
+	FREELIST(deps);
 
-		FREELIST(deps);
-	}
+	print_dep_list(optdeps, dblist, pkg, depth, rev, optional, 1, 1);
+	FREELIST(optdeps);
 }
 
 int main(int argc, char *argv[])