git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/4] stripspace: Implement --count-lines option

From
Tobias Klauser <tklauser@distanz.ch>
Date
Oct 16, 2015, 15:16 UTC
Message-ID
<1445008605-16534-4-git-send-email-tklauser@distanz.ch>
In-Reply-To
<1445008605-16534-1-git-send-email-tklauser@distanz.ch>

Implement the --count-lines options for git stripspace to be able to omit calling:

  git stripspace --strip-comments < infile | wc -l

e.g. in git-rebase--interactive.sh. The above command can now be replaced by:

  git stripspace --strip-comments --count-lines < infile

This will make it easier to port git-rebase--interactive.sh to C later on.

Furthermore, add the corresponding documentation and tests.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---

Implements the small project idea from https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27

 Documentation/git-stripspace.txt | 14 ++++++++++++--
 builtin/stripspace.c             | 18 +++++++++++++++---
 t/t0030-stripspace.sh            | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-stripspace.txt b/Documentation/git-stripspace.txt
index 60328d5..79900b8 100644
--- a/Documentation/git-stripspace.txt
+++ b/Documentation/git-stripspace.txt
@@ -9,8 +9,8 @@ git-stripspace - Remove unnecessary whitespace
 SYNOPSIS
 --------
 [verse]
-'git stripspace' [-s | --strip-comments] < input
-'git stripspace' [-c | --comment-lines] < input
+'git stripspace' [-s | --strip-comments] [--count-lines] < input
+'git stripspace' [-c | --comment-lines] [--count-lines] < input
 
 DESCRIPTION
 -----------
@@ -44,6 +44,10 @@ OPTIONS
 	be terminated with a newline. On empty lines, only the comment character
 	will be prepended.
 
+--count-lines::
+	Output the number of resulting lines after stripping. This is equivalent
+	to calling 'git stripspace | wc -l'.
+
 EXAMPLES
 --------
 
@@ -88,6 +92,12 @@ Use 'git stripspace --strip-comments' to obtain:
 |The end.$
 ---------
 
+Use 'git stripspace --count-lines' to obtain:
+
+---------
+5
+---------
+
 GIT
 ---
 Part of the linkgit:git[1] suite
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index ac1ab3d..487523f 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -14,8 +14,8 @@ static void comment_lines(struct strbuf *buf)
 }
 
 static const char * const stripspace_usage[] = {
-	N_("git stripspace [-s | --strip-comments] < input"),
-	N_("git stripspace [-c | --comment-lines] < input"),
+	N_("git stripspace [-s | --strip-comments] [--count-lines] < input"),
+	N_("git stripspace [-c | --comment-lines] [--count-lines] < input"),
 	NULL
 };
 
@@ -29,6 +29,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
 	struct strbuf buf = STRBUF_INIT;
 	enum stripspace_mode mode = STRIP_DEFAULT;
+	int count_lines = 0;
 
 	const struct option options[] = {
 		OPT_CMDMODE('s', "strip-comments", &mode,
@@ -37,6 +38,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		OPT_CMDMODE('c', "comment-lines", &mode,
 			    N_("prepend comment character and blank to each line"),
 			    COMMENT_LINES),
+		OPT_BOOL(0, "count-lines", &count_lines, N_("print line count")),
 		OPT_END()
 	};
 
@@ -54,7 +56,17 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 	else
 		comment_lines(&buf);
 
-	write_or_die(1, buf.buf, buf.len);
+	if (!count_lines)
+		write_or_die(1, buf.buf, buf.len);
+	else {
+		size_t i, lines;
+
+		for (i = lines = 0; i < buf.len; i++) {
+			if (buf.buf[i] == '\n')
+				lines++;
+		}
+		printf("%zu\n", lines);
+	}
 	strbuf_release(&buf);
 	return 0;
 }
diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
index 29e91d8..9c00cb9 100755
--- a/t/t0030-stripspace.sh
+++ b/t/t0030-stripspace.sh
@@ -438,4 +438,40 @@ test_expect_success 'avoid SP-HT sequence in commented line' '
 	test_cmp expect actual
 '
 
+test_expect_success '--count-lines with newline only' '
+	printf "0\n" >expect &&
+	printf "\n" | git stripspace --count-lines >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--count-lines with single line' '
+	printf "1\n" >expect &&
+	printf "foo\n" | git stripspace --count-lines >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--count-lines with single line preceeded by empty line' '
+	printf "1\n" >expect &&
+	printf "\nfoo" | git stripspace --count-lines >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--count-lines with single line followed by empty line' '
+	printf "1\n" >expect &&
+	printf "foo\n\n" | git stripspace --count-lines >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--count-lines with multiple lines and consecutive newlines' '
+	printf "5\n" >expect &&
+	printf "\none\n\n\nthree\nfour\nfive\n" | git stripspace --count-lines >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--count-lines combined with --strip-comments' '
+	printf "5\n" >expect &&
+	printf "\n# stripped\none\n#stripped\n\nthree\nfour\nfive\n" | git stripspace -s --count-lines >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.6.1.148.g7927db1
Previous: Tobias KlauserNext: Eric Sunshine
Message 11 of 23 in “stripspace: Implement and use --count-lines option”
  1. 0/4 stripspace: Implement and use --count-lines optionTobias Klauser, Oct 16, 2015
  2. 1/4 strbuf: make stripspace() part of strbufTobias Klauser, Oct 16, 2015
  3. 2/4 stripspace: Use parse-options for command-line parsingTobias Klauser, Oct 16, 2015
  4. Junio C HamanoOct 16, 2015
  5. Junio C HamanoOct 16, 2015
  6. Tobias KlauserOct 17, 2015
  7. Junio C HamanoOct 17, 2015
  8. Tobias KlauserOct 20, 2015
  9. Junio C HamanoOct 20, 2015
  10. Tobias KlauserOct 17, 2015
  11. 3/4 stripspace: Implement --count-lines optionTobias Klauser, Oct 16, 2015
  12. Eric SunshineOct 17, 2015
  13. Junio C HamanoOct 18, 2015
  14. Tobias KlauserOct 19, 2015
  15. Christian CouderOct 19, 2015
  16. Eric SunshineOct 19, 2015
  17. Matthieu MoyOct 19, 2015
  18. Tobias KlauserOct 19, 2015
  19. 4/4 git rebase -i: Use newly added --count-lines option for stripspaceTobias Klauser, Oct 16, 2015
  20. Junio C HamanoOct 16, 2015
  21. Tobias KlauserOct 17, 2015
  22. Matthieu MoyOct 16, 2015
  23. Tobias KlauserOct 17, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.