t0410: make test description clearer
[git/gitster.git] / builtin / stripspace.c
blobe147f3ff92c37afe79a26808b5dfd83476fc3366
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "parse-options.h"
7 #include "setup.h"
8 #include "strbuf.h"
9 #include "write-or-die.h"
11 static void comment_lines(struct strbuf *buf)
13 char *msg;
14 size_t len;
16 msg = strbuf_detach(buf, &len);
17 strbuf_add_commented_lines(buf, msg, len, comment_line_str);
18 free(msg);
21 static const char * const stripspace_usage[] = {
22 "git stripspace [-s | --strip-comments]",
23 "git stripspace [-c | --comment-lines]",
24 NULL
27 enum stripspace_mode {
28 STRIP_DEFAULT = 0,
29 STRIP_COMMENTS,
30 COMMENT_LINES
33 int cmd_stripspace(int argc,
34 const char **argv,
35 const char *prefix,
36 struct repository *repo UNUSED)
38 struct strbuf buf = STRBUF_INIT;
39 enum stripspace_mode mode = STRIP_DEFAULT;
40 int nongit;
42 const struct option options[] = {
43 OPT_CMDMODE('s', "strip-comments", &mode,
44 N_("skip and remove all lines starting with comment character"),
45 STRIP_COMMENTS),
46 OPT_CMDMODE('c', "comment-lines", &mode,
47 N_("prepend comment character and space to each line"),
48 COMMENT_LINES),
49 OPT_END()
52 argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
53 if (argc)
54 usage_with_options(stripspace_usage, options);
56 if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
57 setup_git_directory_gently(&nongit);
58 git_config(git_default_config, NULL);
61 if (strbuf_read(&buf, 0, 1024) < 0)
62 die_errno("could not read the input");
64 if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
65 strbuf_stripspace(&buf,
66 mode == STRIP_COMMENTS ? comment_line_str : NULL);
67 else
68 comment_lines(&buf);
70 write_or_die(1, buf.buf, buf.len);
71 strbuf_release(&buf);
72 return 0;