]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/pretty-print: skip redundant section headers with --tldr 29553/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 17 Oct 2023 17:11:44 +0000 (19:11 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 24 Oct 2023 16:32:18 +0000 (18:32 +0200)
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

index 1544b422da824c3110bb4441ff2ce66040799715..e31edb88b02c92a6b55a40ecfff35c4279ab1b05 100644 (file)
@@ -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);
                         }
                 }