builtin / stripspace.con commit Merge branch 'pw/sequence-rerere-autoupdate' into next (010cbb0)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "config.h"
   4#include "parse-options.h"
   5#include "strbuf.h"
   6
   7static void comment_lines(struct strbuf *buf)
   8{
   9        char *msg;
  10        size_t len;
  11
  12        msg = strbuf_detach(buf, &len);
  13        strbuf_add_commented_lines(buf, msg, len);
  14        free(msg);
  15}
  16
  17static const char * const stripspace_usage[] = {
  18        N_("git stripspace [-s | --strip-comments]"),
  19        N_("git stripspace [-c | --comment-lines]"),
  20        NULL
  21};
  22
  23enum stripspace_mode {
  24        STRIP_DEFAULT = 0,
  25        STRIP_COMMENTS,
  26        COMMENT_LINES
  27};
  28
  29int cmd_stripspace(int argc, const char **argv, const char *prefix)
  30{
  31        struct strbuf buf = STRBUF_INIT;
  32        enum stripspace_mode mode = STRIP_DEFAULT;
  33
  34        const struct option options[] = {
  35                OPT_CMDMODE('s', "strip-comments", &mode,
  36                            N_("skip and remove all lines starting with comment character"),
  37                            STRIP_COMMENTS),
  38                OPT_CMDMODE('c', "comment-lines", &mode,
  39                            N_("prepend comment character and space to each line"),
  40                            COMMENT_LINES),
  41                OPT_END()
  42        };
  43
  44        argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
  45        if (argc)
  46                usage_with_options(stripspace_usage, options);
  47
  48        if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
  49                setup_git_directory_gently(NULL);
  50                git_config(git_default_config, NULL);
  51        }
  52
  53        if (strbuf_read(&buf, 0, 1024) < 0)
  54                die_errno("could not read the input");
  55
  56        if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
  57                strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
  58        else
  59                comment_lines(&buf);
  60
  61        write_or_die(1, buf.buf, buf.len);
  62        strbuf_release(&buf);
  63        return 0;
  64}