From patchwork Mon May 4 18:11:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frederick Zhang X-Patchwork-Id: 1616 X-Patchwork-Delegate: johannes@kyriasis.com Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 4FB631894CD42 for ; Mon, 4 May 2020 18:14:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_ADSP_ALL=1.1, DKIM_INVALID=1,DKIM_SIGNED=0.1,MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,SPF_HELO_NONE=0.001,T_DMARC_POLICY_QUAR=0.01 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-BL-Results: [127.0.9.2] [127.0.0.11] Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Mon, 4 May 2020 18:14:47 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 2941B1B83D3DE5; Mon, 4 May 2020 18:14:44 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [5.9.250.164]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: luna) by orion.archlinux.org (Postfix) with ESMTPSA id E21CA1B83D3DE1; Mon, 4 May 2020 18:14:43 +0000 (UTC) Authentication-Results: orion.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=tsundere.moe header.i=@tsundere.moe header.b=Gpbhq754 Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id D5C1A29CE1; Mon, 4 May 2020 18:14:43 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=tsundere.moe header.i=@tsundere.moe header.b=Gpbhq754 Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 5D03A29CE0 for ; Mon, 4 May 2020 18:14:40 +0000 (UTC) Received: from smtp.tsundere.moe (li1581-109.members.linode.com [139.162.97.109]) by luna.archlinux.org (Postfix) with ESMTPS for ; Mon, 4 May 2020 18:14:39 +0000 (UTC) Received: from FredArch.lan (n175-33-179-138.meb2.vic.optusnet.com.au [175.33.179.138]) by smtp.tsundere.moe (Postfix) with ESMTPSA id 5404A180F1; Mon, 4 May 2020 18:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tsundere.moe; s=202005; t=1588616071; bh=at6soK9O3yUZt4S3V8XcfZFFry6LOluB8Y0sEgMx8EI=; h=From:To:Cc:Subject:Date:From; b=Gpbhq7543gU4Jq4nFe0n95hQcP1G1DHW8DO6KOk5DN1LEx7uZtOyC82C4ZQYgpMDH QFE608kf0x3nA8sNFJp50hMqB3uzvsAAX/vel2TsCvdv9BDXmf1+IKuAsyHjpwYbbK BUevjwEwvlk6jk2+O1qivpDwmZlv8ql1oUMqnxw7lZfirOrJTF+vbdptxOmJ9+IlTS 3FebtgPTCe3htXSM0/AvJEPjD07XEy6frFZkuuZnSmb2ULf4erXtbi+kXGQDF4aszs 6MlkCyER9/B/P4sUU38eEsOJCUgWxrLE0gvUurdm7KVSHffSV6xI7FoSRRxft2xeem pDOZigiLc5hbA== From: Frederick Zhang To: pacman-contrib@lists.archlinux.org Subject: [PATCH] pactree: Add support for getting optional dependencies Date: Tue, 5 May 2020 04:11:22 +1000 Message-Id: <20200504181120.17766-1-frederick888@tsundere.moe> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.11 (smtp.tsundere.moe [0.0.0.0]); Mon, 04 May 2020 18:14:31 +0000 (UTC) X-Virus-Scanned: clamav-milter 0.102.2 at Fred-Linode X-Virus-Status: Clean X-BeenThere: pacman-contrib@lists.archlinux.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for pacman-contrib development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pacman-contrib-bounces@lists.archlinux.org Sender: "pacman-contrib" Patch by Johannes Löthberg [1] with minor modifications [1] https://lists.archlinux.org/pipermail/pacman-dev/2016-February/020922.html Signed-off-by: Frederick Zhang --- It probably got lost and was never merged according to the discussion I just had with Eli in pacman-dev. Credits to Johannes. I made a few minor changes to make it compile. doc/pactree.8.txt | 3 +++ src/pactree.c | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/doc/pactree.8.txt b/doc/pactree.8.txt index 296d04e..c8666ad 100644 --- a/doc/pactree.8.txt +++ b/doc/pactree.8.txt @@ -48,8 +48,11 @@ Options *-l, \--linear*:: Prints package names at the start of each line, one per line. +*-o, \--optdeps*:: + Show packages that are optionally depended upon by the named package. + *-r, \--reverse*:: Show packages that depend on the named package. *-s, \--sync*:: diff --git a/src/pactree.c b/src/pactree.c index cf83326..6da4294 100644 --- a/src/pactree.c +++ b/src/pactree.c @@ -119,8 +119,9 @@ static alpm_list_t *provisions = NULL; static struct color_choices *color = &no_color; static struct graph_style *style = &graph_utf8; static int graphviz = 0; static int max_depth = -1; +static int optdeps = 0; static int reverse = 0; static int unique = 0; static int searchsyncs = 0; static int debug = 0; @@ -249,8 +250,9 @@ static void usage(void) " -d, --depth <#> limit the depth of recursion\n" " -g, --graph generate output for graphviz's dot\n" " -h, --help display this help message\n" " -l, --linear enable linear output\n" + " -o, --optdeps list packages that are optionally depended upon by the named package\n" " -r, --reverse list packages that depend on the named package\n" " -s, --sync search sync databases instead of local\n" " -u, --unique show dependencies with no duplicates (implies -l)\n" " -v, --version display the version\n" @@ -276,8 +278,9 @@ static int parse_options(int argc, char *argv[]) {"depth", required_argument, 0, 'd'}, {"graph", no_argument, 0, 'g'}, {"help", no_argument, 0, 'h'}, {"linear", no_argument, 0, 'l'}, + {"optdeps", no_argument, 0, 'o'}, {"reverse", no_argument, 0, 'r'}, {"sync", no_argument, 0, 's'}, {"unique", no_argument, 0, 'u'}, {"version", no_argument, 0, 'v'}, @@ -293,9 +296,9 @@ static int parse_options(int argc, char *argv[]) if(strcmp(nl_langinfo(CODESET), "UTF-8") == 0) { style = &graph_utf8; } - while((opt = getopt_long(argc, argv, "ab:cd:ghlrsuv", opts, &option_index))) { + while((opt = getopt_long(argc, argv, "ab:cd:ghlorsuv", opts, &option_index))) { if(opt < 0) { break; } @@ -331,8 +334,11 @@ static int parse_options(int argc, char *argv[]) break; case 'l': style = &graph_linear; break; + case 'o': + optdeps = 1; + break; case 'r': reverse = 1; break; case 's': @@ -464,12 +470,23 @@ static alpm_list_t *get_pkg_deps(alpm_pkg_t *pkg) } return dep_strings; } +static alpm_list_t *get_pkg_optdeps(alpm_pkg_t *pkg) +{ + alpm_list_t *i, *dep_strings = NULL; + for(i = alpm_pkg_get_optdepends(pkg); i; i = alpm_list_next(i)) { + alpm_depend_t *dep = i->data; + char *ds = alpm_dep_compute_string(dep); + dep_strings = alpm_list_add(dep_strings, ds); + } + return dep_strings; +} + /** * walk dependencies, showing dependencies of the target */ -static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int rev) +static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int rev, int optdeps) { alpm_list_t *deps, *i; if(!pkg || ((max_depth >= 0) && (depth->level > max_depth))) { @@ -482,8 +499,17 @@ static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int r deps = alpm_pkg_compute_requiredby(pkg); } else { deps = get_pkg_deps(pkg); } + if(optdeps) { + alpm_list_t *odeps; + if(rev) { + odeps = alpm_pkg_compute_optionalfor(pkg); + } else { + odeps = get_pkg_optdeps(pkg); + } + deps = alpm_list_join(deps, odeps); + } for(i = deps; i; i = alpm_list_next(i)) { const char *pkgname = i->data; int last = alpm_list_next(i) ? 0 : 1; @@ -514,9 +540,9 @@ static void walk_deps(alpm_list_t *dblist, alpm_pkg_t *pkg, tdepth *depth, int r } else { d.prev = NULL; } } - walk_deps(dblist, dep_pkg, &d, rev); + walk_deps(dblist, dep_pkg, &d, rev, optdeps); depth->next = NULL; } } } @@ -575,9 +601,9 @@ int main(int argc, char *argv[]) NULL, NULL, 1 }; - walk_deps(dblist, pkg, &d, reverse); + walk_deps(dblist, pkg, &d, reverse, optdeps); print_end(); if(freelist) {