Merge branch 'tk/stripspace'
authorJunio C Hamano <gitster@pobox.com>
Mon, 26 Oct 2015 22:55:20 +0000 (15:55 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Oct 2015 22:55:20 +0000 (15:55 -0700)
The internal stripspace() function has been moved to where it
logically belongs to, i.e. strbuf API, and the command line parser
of "git stripspace" has been updated to use the parse_options API.

* tk/stripspace:
stripspace: use parse-options for command-line parsing
strbuf: make stripspace() part of strbuf

builtin/am.c
builtin/branch.c
builtin/commit.c
builtin/merge.c
builtin/notes.c
builtin/stripspace.c
builtin/tag.c
strbuf.c
strbuf.h
index fc6d3d21aac54f9d820e3cae60e08d81366787be..4e396c832139aa3c4ae4e68e9d265b0107817aa9 100644 (file)
@@ -1343,7 +1343,7 @@ static int parse_mail(struct am_state *state, const char *mail)
        strbuf_addstr(&msg, "\n\n");
        if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
                die_errno(_("could not read '%s'"), am_path(state, "msg"));
-       stripspace(&msg, 0);
+       strbuf_stripspace(&msg, 0);
 
        if (state->signoff)
                am_signoff(&msg);
index 01f9530822d9f4c060e2bcfa6d80b03618f526d9..b99a436ef36ecfcc70636741642fdcf07b5a5810 100644 (file)
@@ -592,7 +592,7 @@ static int edit_branch_description(const char *branch_name)
                strbuf_release(&buf);
                return -1;
        }
-       stripspace(&buf, 1);
+       strbuf_stripspace(&buf, 1);
 
        strbuf_addf(&name, "branch.%s.description", branch_name);
        status = git_config_set(name.buf, buf.len ? buf.buf : NULL);
index 63772d016a23905d2e58bff0ad117673ed6651e7..dca09e2c3bd8a839b27e279c427ebba71e88966e 100644 (file)
@@ -775,7 +775,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        s->hints = 0;
 
        if (clean_message_contents)
-               stripspace(&sb, 0);
+               strbuf_stripspace(&sb, 0);
 
        if (signoff)
                append_signoff(&sb, ignore_non_trailer(&sb), 0);
@@ -1014,7 +1014,7 @@ static int template_untouched(struct strbuf *sb)
        if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
                return 0;
 
-       stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
+       strbuf_stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
        if (!skip_prefix(sb->buf, tmpl.buf, &start))
                start = sb->buf;
        strbuf_release(&tmpl);
@@ -1726,7 +1726,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
                wt_status_truncate_message_at_cut_line(&sb);
 
        if (cleanup_mode != CLEANUP_NONE)
-               stripspace(&sb, cleanup_mode == CLEANUP_ALL);
+               strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
        if (template_untouched(&sb) && !allow_empty_message) {
                rollback_index_files();
                fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
index a0a93282922ebb8c58b79ff365d859500041351a..977ffff2876de4d817025bbf6d11ec251b674141 100644 (file)
@@ -806,7 +806,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                        abort_commit(remoteheads, NULL);
        }
        read_merge_msg(&msg);
-       stripspace(&msg, 0 < option_edit);
+       strbuf_stripspace(&msg, 0 < option_edit);
        if (!msg.len)
                abort_commit(remoteheads, _("Empty commit message."));
        strbuf_release(&merge_msg);
index 13c0af9155052499c15ecf2a147acd83bc042f42..515cebbeb8a33eb982a1ddccdc9d69e09beefb1d 100644 (file)
@@ -192,7 +192,7 @@ static void prepare_note_data(const unsigned char *object, struct note_data *d,
                if (launch_editor(d->edit_path, &d->buf, NULL)) {
                        die(_("Please supply the note contents using either -m or -F option"));
                }
-               stripspace(&d->buf, 1);
+               strbuf_stripspace(&d->buf, 1);
        }
 }
 
@@ -215,7 +215,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
        if (d->buf.len)
                strbuf_addch(&d->buf, '\n');
        strbuf_addstr(&d->buf, arg);
-       stripspace(&d->buf, 0);
+       strbuf_stripspace(&d->buf, 0);
 
        d->given = 1;
        return 0;
@@ -232,7 +232,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
                        die_errno(_("cannot read '%s'"), arg);
        } else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
                die_errno(_("could not open or read '%s'"), arg);
-       stripspace(&d->buf, 0);
+       strbuf_stripspace(&d->buf, 0);
 
        d->given = 1;
        return 0;
index 958567a0cd0f3b676f2a46a699b34a67d14f129a..7ff8434f7c3cf71dbd77cbf868d5dc56dd943fdb 100644 (file)
@@ -1,71 +1,7 @@
 #include "builtin.h"
 #include "cache.h"
-
-/*
- * Returns the length of a line, without trailing spaces.
- *
- * If the line ends with newline, it will be removed too.
- */
-static size_t cleanup(char *line, size_t len)
-{
-       while (len) {
-               unsigned char c = line[len - 1];
-               if (!isspace(c))
-                       break;
-               len--;
-       }
-
-       return len;
-}
-
-/*
- * Remove empty lines from the beginning and end
- * and also trailing spaces from every line.
- *
- * Turn multiple consecutive empty lines between paragraphs
- * into just one empty line.
- *
- * If the input has only empty lines and spaces,
- * no output will be produced.
- *
- * If last line does not have a newline at the end, one is added.
- *
- * Enable skip_comments to skip every line starting with comment
- * character.
- */
-void stripspace(struct strbuf *sb, int skip_comments)
-{
-       int empties = 0;
-       size_t i, j, len, newlen;
-       char *eol;
-
-       /* We may have to add a newline. */
-       strbuf_grow(sb, 1);
-
-       for (i = j = 0; i < sb->len; i += len, j += newlen) {
-               eol = memchr(sb->buf + i, '\n', sb->len - i);
-               len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
-
-               if (skip_comments && len && sb->buf[i] == comment_line_char) {
-                       newlen = 0;
-                       continue;
-               }
-               newlen = cleanup(sb->buf + i, len);
-
-               /* Not just an empty line? */
-               if (newlen) {
-                       if (empties > 0 && j > 0)
-                               sb->buf[j++] = '\n';
-                       empties = 0;
-                       memmove(sb->buf + j, sb->buf + i, newlen);
-                       sb->buf[newlen + j++] = '\n';
-               } else {
-                       empties++;
-               }
-       }
-
-       strbuf_setlen(sb, j);
-}
+#include "parse-options.h"
+#include "strbuf.h"
 
 static void comment_lines(struct strbuf *buf)
 {
@@ -77,41 +13,45 @@ static void comment_lines(struct strbuf *buf)
        free(msg);
 }
 
-static const char *usage_msg = "\n"
-"  git stripspace [-s | --strip-comments]\n"
-"  git stripspace [-c | --comment-lines]";
+static const char * const stripspace_usage[] = {
+       N_("git stripspace [-s | --strip-comments]"),
+       N_("git stripspace [-c | --comment-lines]"),
+       NULL
+};
+
+enum stripspace_mode {
+       STRIP_DEFAULT = 0,
+       STRIP_COMMENTS,
+       COMMENT_LINES
+};
 
 int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
        struct strbuf buf = STRBUF_INIT;
-       int strip_comments = 0;
-       enum { INVAL = 0, STRIP_SPACE = 1, COMMENT_LINES = 2 } mode = STRIP_SPACE;
-
-       if (argc == 2) {
-               if (!strcmp(argv[1], "-s") ||
-                   !strcmp(argv[1], "--strip-comments")) {
-                       strip_comments = 1;
-               } else if (!strcmp(argv[1], "-c") ||
-                          !strcmp(argv[1], "--comment-lines")) {
-                       mode = COMMENT_LINES;
-               } else {
-                       mode = INVAL;
-               }
-       } else if (argc > 1) {
-               mode = INVAL;
-       }
-
-       if (mode == INVAL)
-               usage(usage_msg);
-
-       if (strip_comments || mode == COMMENT_LINES)
+       enum stripspace_mode mode = STRIP_DEFAULT;
+
+       const struct option options[] = {
+               OPT_CMDMODE('s', "strip-comments", &mode,
+                           N_("skip and remove all lines starting with comment character"),
+                           STRIP_COMMENTS),
+               OPT_CMDMODE('c', "comment-lines", &mode,
+                           N_("prepend comment character and blank to each line"),
+                           COMMENT_LINES),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
+       if (argc)
+               usage_with_options(stripspace_usage, options);
+
+       if (mode == STRIP_COMMENTS || mode == COMMENT_LINES)
                git_config(git_default_config, NULL);
 
        if (strbuf_read(&buf, 0, 1024) < 0)
                die_errno("could not read the input");
 
-       if (mode == STRIP_SPACE)
-               stripspace(&buf, strip_comments);
+       if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
+               strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
        else
                comment_lines(&buf);
 
index 9e17dca7cac30738c966d0e23258f56c0cb9bc70..566078773fe82a51d850a4def8d63835d6310b2e 100644 (file)
@@ -268,7 +268,7 @@ static void create_tag(const unsigned char *object, const char *tag,
        }
 
        if (opt->cleanup_mode != CLEANUP_NONE)
-               stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
+               strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
 
        if (!opt->message_given && !buf->len)
                die(_("no tag message?"));
index 107c45d29111a011a04daaa8fd0323ed9d973398..d76f0aed85c4ec6eafdcfd1a8ee2b22d3d20df96 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -752,3 +752,69 @@ void strbuf_add_unique_abbrev(struct strbuf *sb, const unsigned char *sha1,
        r = find_unique_abbrev_r(sb->buf + sb->len, sha1, abbrev_len);
        strbuf_setlen(sb, sb->len + r);
 }
+
+/*
+ * Returns the length of a line, without trailing spaces.
+ *
+ * If the line ends with newline, it will be removed too.
+ */
+static size_t cleanup(char *line, size_t len)
+{
+       while (len) {
+               unsigned char c = line[len - 1];
+               if (!isspace(c))
+                       break;
+               len--;
+       }
+
+       return len;
+}
+
+/*
+ * Remove empty lines from the beginning and end
+ * and also trailing spaces from every line.
+ *
+ * Turn multiple consecutive empty lines between paragraphs
+ * into just one empty line.
+ *
+ * If the input has only empty lines and spaces,
+ * no output will be produced.
+ *
+ * If last line does not have a newline at the end, one is added.
+ *
+ * Enable skip_comments to skip every line starting with comment
+ * character.
+ */
+void strbuf_stripspace(struct strbuf *sb, int skip_comments)
+{
+       int empties = 0;
+       size_t i, j, len, newlen;
+       char *eol;
+
+       /* We may have to add a newline. */
+       strbuf_grow(sb, 1);
+
+       for (i = j = 0; i < sb->len; i += len, j += newlen) {
+               eol = memchr(sb->buf + i, '\n', sb->len - i);
+               len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
+
+               if (skip_comments && len && sb->buf[i] == comment_line_char) {
+                       newlen = 0;
+                       continue;
+               }
+               newlen = cleanup(sb->buf + i, len);
+
+               /* Not just an empty line? */
+               if (newlen) {
+                       if (empties > 0 && j > 0)
+                               sb->buf[j++] = '\n';
+                       empties = 0;
+                       memmove(sb->buf + j, sb->buf + i, newlen);
+                       sb->buf[newlen + j++] = '\n';
+               } else {
+                       empties++;
+               }
+       }
+
+       strbuf_setlen(sb, j);
+}
index 0f9c8a72ba7cdb7adb34c96a8e04db776c18d743..7123fca7aff5182b66af6d76991552081139467c 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -418,7 +418,16 @@ extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
  * Strip whitespace from a buffer. The second parameter controls if
  * comments are considered contents to be removed or not.
  */
-extern void stripspace(struct strbuf *buf, int skip_comments);
+extern void strbuf_stripspace(struct strbuf *buf, int skip_comments);
+
+/**
+ * Temporary alias until all topic branches have switched to use
+ * strbuf_stripspace directly.
+ */
+static inline void stripspace(struct strbuf *buf, int skip_comments)
+{
+       strbuf_stripspace(buf, skip_comments);
+}
 
 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
 {