From c04cec128f486c75e27171c10510867367fadf69 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 17 Oct 2023 19:11:44 +0200 Subject: [PATCH] shared/pretty-print: skip redundant section headers with --tldr If the same section appears consecutively in a given file, subsequent occurenced are not printed. [Slice] Foo=bar [Slice] # this is not printed Bar=bar Requested in https://github.com/systemd/systemd/pull/29553#pullrequestreview-1677310352. --- src/shared/pretty-print.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c index 1544b422da8..e31edb88b02 100644 --- a/src/shared/pretty-print.c +++ b/src/shared/pretty-print.c @@ -149,7 +149,7 @@ static LineType classify_line_type(const char *line, CatFlags flags) { static int cat_file(const char *filename, bool newline, CatFlags flags) { _cleanup_fclose_ FILE *f = NULL; - _cleanup_free_ char *urlified = NULL, *section = NULL; + _cleanup_free_ char *urlified = NULL, *section = NULL, *old_section = NULL; int r; f = fopen(filename, "re"); @@ -189,11 +189,14 @@ static int cat_file(const char *filename, bool newline, CatFlags flags) { /* Before we print the actual line, print the last section header */ if (section) { - printf("%s%s%s\n", - ansi_highlight_cyan(), - section, - ansi_normal()); - section = mfree(section); + /* Do not print redundant section headers */ + if (!streq_ptr(section, old_section)) + printf("%s%s%s\n", + ansi_highlight_cyan(), + section, + ansi_normal()); + + free_and_replace(old_section, section); } } -- 2.39.5