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