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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---

 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
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]