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

[PATCH] stripspace: add --line-count flag

From
Sidhant Sharma [:tk] <tigerkid001@gmail.com>
Date
Mar 4, 2016, 18:38 UTC
Message-ID
<1457116723-20206-1-git-send-email-tigerkid001@gmail.com>

When used, this flag outputs number of lines after stripspace has removed trailing whitespace. With `--line-count`, git-rebase--interactive.sh need not rely on `wc -l` for line count.

Signed-off-by: Sidhant Sharma [:tk] <tigerkid001@gmail.com>
---
 This the first version of the patch for the small project listed here:
 https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27
 builtin/stripspace.c       | 22 +++++++++++++++++++++-
 git-rebase--interactive.sh |  6 +++---
 2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 15e716e..e08da03 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -13,22 +13,38 @@ static void comment_lines(struct strbuf *buf)
 	free(msg);
 }

+static void count_lines(struct strbuf *buf)
+{
+	size_t len = 0;
+	int i;
+
+	for (i = 0; i < buf->len; i++)
+		if (buf->buf[i] == '\n')
+			len++;
+
+	sprintf(buf->buf, "%zu", len);
+	buf->len = strlen(buf->buf);
+}
+
 static const char * const stripspace_usage[] = {
 	N_("git stripspace [-s | --strip-comments]"),
 	N_("git stripspace [-c | --comment-lines]"),
+	N_("git stripspace [-l | --line-count]"),
 	NULL
 };

 enum stripspace_mode {
 	STRIP_DEFAULT = 0,
 	STRIP_COMMENTS,
-	COMMENT_LINES
+	COMMENT_LINES,
+	LINE_COUNT
 };

 int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
 	struct strbuf buf = STRBUF_INIT;
 	enum stripspace_mode mode = STRIP_DEFAULT;
+	int count = 0;

 	const struct option options[] = {
 		OPT_CMDMODE('s', "strip-comments", &mode,
@@ -37,6 +53,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 		OPT_CMDMODE('c', "comment-lines", &mode,
 			    N_("prepend comment character and space to each line"),
 			    COMMENT_LINES),
+		OPT_BOOL('l', "line-count", &count, N_("count number of lines")),
 		OPT_END()
 	};

@@ -55,6 +72,9 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 	else
 		comment_lines(&buf);

+	if (count)
+		count_lines(&buf);
+
 	write_or_die(1, buf.buf, buf.len);
 	strbuf_release(&buf);
 	return 0;
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index c0cfe88..e8bef37 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -120,9 +120,9 @@ mark_action_done () {
 	sed -e 1q < "$todo" >> "$done"
 	sed -e 1d < "$todo" >> "$todo".new
 	mv -f "$todo".new "$todo"
-	new_count=$(git stripspace --strip-comments <"$done" | wc -l)
+	new_count=$(git stripspace --strip-comments --line-count <"$done")
 	echo $new_count >"$msgnum"
-	total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
+	total=$(($new_count + $(git stripspace --strip-comments --line-count <"$todo")))
 	echo $total >"$end"
 	if test "$last_count" != "$new_count"
 	then
@@ -1251,7 +1251,7 @@ test -s "$todo" || echo noop >> "$todo"
 test -n "$autosquash" && rearrange_squash "$todo"
 test -n "$cmd" && add_exec_commands "$todo"

-todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
+todocount=$(git stripspace --strip-comments --line-count <"$todo")
 todocount=${todocount##* }

 cat >>"$todo" <<EOF
--
2.7.2
Next: Sidhant Sharma
Message 1 of 5 in “stripspace: add --line-count flag”
  1. stripspace: add --line-count flagSidhant Sharma [:tk], Mar 4, 2016
  2. Sidhant SharmaMar 4, 2016
  3. Junio C HamanoMar 4, 2016
  4. Sidhant SharmaMar 4, 2016
  5. Junio C HamanoMar 4, 2016

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.