1#include "builtin.h"
2#include "cache.h"
3#include "strbuf.h"
4
5static void comment_lines(struct strbuf *buf)
6{
7 char *msg;
8 size_t len;
9
10 msg = strbuf_detach(buf, &len);
11 strbuf_add_commented_lines(buf, msg, len);
12 free(msg);
13}
14
15static const char *usage_msg = "\n"
16" git stripspace [-s | --strip-comments] < input\n"
17" git stripspace [-c | --comment-lines] < input";
18
19int cmd_stripspace(int argc, const char **argv, const char *prefix)
20{
21 struct strbuf buf = STRBUF_INIT;
22 int strip_comments = 0;
23 enum { INVAL = 0, STRIP_SPACE = 1, COMMENT_LINES = 2 } mode = STRIP_SPACE;
24
25 if (argc == 2) {
26 if (!strcmp(argv[1], "-s") ||
27 !strcmp(argv[1], "--strip-comments")) {
28 strip_comments = 1;
29 } else if (!strcmp(argv[1], "-c") ||
30 !strcmp(argv[1], "--comment-lines")) {
31 mode = COMMENT_LINES;
32 } else {
33 mode = INVAL;
34 }
35 } else if (argc > 1) {
36 mode = INVAL;
37 }
38
39 if (mode == INVAL)
40 usage(usage_msg);
41
42 if (strip_comments || mode == COMMENT_LINES)
43 git_config(git_default_config, NULL);
44
45 if (strbuf_read(&buf, 0, 1024) < 0)
46 die_errno("could not read the input");
47
48 if (mode == STRIP_SPACE)
49 strbuf_stripspace(&buf, strip_comments);
50 else
51 comment_lines(&buf);
52
53 write_or_die(1, buf.buf, buf.len);
54 strbuf_release(&buf);
55 return 0;
56}