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