git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Sidhant Sharma [:tk]" <tigerkid001@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH] stripspace: add --line-count flag
Date: Sat,  5 Mar 2016 00:08:43 +0530	[thread overview]
Message-ID: <1457116723-20206-1-git-send-email-tigerkid001@gmail.com> (raw)

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

             reply	other threads:[~2016-03-04 18:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-04 18:38 Sidhant Sharma [:tk] [this message]
2016-03-04 18:43 ` [PATCH] stripspace: add --line-count flag Sidhant Sharma
2016-03-04 18:49   ` Junio C Hamano
2016-03-04 18:56     ` Sidhant Sharma
2016-03-04 18:57     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1457116723-20206-1-git-send-email-tigerkid001@gmail.com \
    --to=tigerkid001@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).