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

5 messages from 2016-03-04 to 2016-03-04. Participants: Sidhant Sharma [:tk], Sidhant Sharma, Junio C Hamano.
Thread: https://gitlist.dev/t/41609

## Sidhant Sharma [:tk], 2016-03-04 18:38

Subject: [PATCH] stripspace: add --line-count flag
Message-ID: <1457116723-20206-1-git-send-email-tigerkid001@gmail.com>
URL: https://gitlist.dev/e/1457116723-20206-1-git-send-email-tigerkid001%40gmail.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

```

## Sidhant Sharma, 2016-03-04 18:43

Subject: Re: [PATCH] stripspace: add --line-count flag
Message-ID: <56D9D750.1030703@gmail.com>
URL: https://gitlist.dev/e/56D9D750.1030703%40gmail.com
In-Reply-To: <1457116723-20206-1-git-send-email-tigerkid001@gmail.com>

```

>  builtin/stripspace.c       | 22 +++++++++++++++++++++-
>  git-rebase--interactive.sh |  6 +++---
>  2 files changed, 24 insertions(+), 4 deletions(-)
>
This is my first attempt at the small project listed here: https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27.
With this, --line-count can be used with stripspace, instead of having to pipe its output to `wc -l` in git-rebase--interactive.sh. I went with --line-count and not --count-lines since its short form (-c) is already in use, and I think -l is more apt for this.
Comments?


Thanks and regards,
Sidhant Sharma [:tk]

```

## Junio C Hamano, 2016-03-04 18:49

Subject: Re: [PATCH] stripspace: add --line-count flag
Message-ID: <xmqqk2liyt7j.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqk2liyt7j.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <56D9D750.1030703@gmail.com>

```
Sidhant Sharma <tigerkid001@gmail.com> writes:

> This is my first attempt at the small project listed here: https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27.

> Comments?

Isn't that page somewhat stale?

http://git.661346.n2.nabble.com/PATCH-0-3-stripspace-Implement-and-use-count-lines-option-tt7641360.html#none

```

## Sidhant Sharma, 2016-03-04 18:56

Subject: Re: [PATCH] stripspace: add --line-count flag
Message-ID: <56D9DA55.6040309@gmail.com>
URL: https://gitlist.dev/e/56D9DA55.6040309%40gmail.com
In-Reply-To: <xmqqk2liyt7j.fsf@gitster.mtv.corp.google.com>

```


On Saturday 05 March 2016 12:19 AM, Junio C Hamano wrote:
> Sidhant Sharma <tigerkid001@gmail.com> writes:
>
>> This is my first attempt at the small project listed here: https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27.
>> Comments?
> Isn't that page somewhat stale?
>
> http://git.661346.n2.nabble.com/PATCH-0-3-stripspace-Implement-and-use-count-lines-option-tt7641360.html#none
Oh, I should've checked first. My bad, I was just looking to get familiar with the codebase.


Thanks

Sidhant Sharma [:tk]

```

## Junio C Hamano, 2016-03-04 18:57

Subject: Re: [PATCH] stripspace: add --line-count flag
Message-ID: <xmqqfuw6yste.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqfuw6yste.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <xmqqk2liyt7j.fsf@gitster.mtv.corp.google.com>

```
Junio C Hamano <gitster@pobox.com> writes:

> Sidhant Sharma <tigerkid001@gmail.com> writes:
>
>> This is my first attempt at the small project listed here: https://git.wiki.kernel.org/index.php/SmallProjectsIdeas#implement_.27--count-lines.27_in_.27git_stripspace.27.
>
>> Comments?
>
> Isn't that page somewhat stale?
>
> http://git.661346.n2.nabble.com/PATCH-0-3-stripspace-Implement-and-use-count-lines-option-tt7641360.html#none

And its reroll:

  http://thread.gmane.org/gmane.comp.version-control.git/279742

The discussion seems to indicate that we weren't in favor of
addition of this feature at least back then, e.g.

http://thread.gmane.org/gmane.comp.version-control.git/279742/focus=279888

```
