Merge branch 'rs/status-detached-head-memcmp' into maint
[git/debian.git] / builtin / stripspace.c
blob7ff8434f7c3cf71dbd77cbf868d5dc56dd943fdb
1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "strbuf.h"
6 static void comment_lines(struct strbuf *buf)
8 char *msg;
9 size_t len;
11 msg = strbuf_detach(buf, &len);
12 strbuf_add_commented_lines(buf, msg, len);
13 free(msg);
16 static const char * const stripspace_usage[] = {
17 N_("git stripspace [-s | --strip-comments]"),
18 N_("git stripspace [-c | --comment-lines]"),
19 NULL
22 enum stripspace_mode {
23 STRIP_DEFAULT = 0,
24 STRIP_COMMENTS,
25 COMMENT_LINES
28 int cmd_stripspace(int argc, const char **argv, const char *prefix)
30 struct strbuf buf = STRBUF_INIT;
31 enum stripspace_mode mode = STRIP_DEFAULT;
33 const struct option options[] = {
34 OPT_CMDMODE('s', "strip-comments", &mode,
35 N_("skip and remove all lines starting with comment character"),
36 STRIP_COMMENTS),
37 OPT_CMDMODE('c', "comment-lines", &mode,
38 N_("prepend comment character and blank to each line"),
39 COMMENT_LINES),
40 OPT_END()
43 argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
44 if (argc)
45 usage_with_options(stripspace_usage, options);
47 if (mode == STRIP_COMMENTS || mode == COMMENT_LINES)
48 git_config(git_default_config, NULL);
50 if (strbuf_read(&buf, 0, 1024) < 0)
51 die_errno("could not read the input");
53 if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
54 strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
55 else
56 comment_lines(&buf);
58 write_or_die(1, buf.buf, buf.len);
59 strbuf_release(&buf);
60 return 0;