]>
Commit | Line | Data |
---|---|---|
7499c996 | 1 | #include "builtin.h" |
9690c118 | 2 | #include "cache.h" |
bed44524 | 3 | #include "parse-options.h" |
63af4a84 | 4 | #include "strbuf.h" |
7499c996 | 5 | |
eff80a9f JH |
6 | static void comment_lines(struct strbuf *buf) |
7 | { | |
8 | char *msg; | |
9 | size_t len; | |
10 | ||
11 | msg = strbuf_detach(buf, &len); | |
12 | strbuf_add_commented_lines(buf, msg, len); | |
13 | free(msg); | |
14 | } | |
15 | ||
bed44524 | 16 | static const char * const stripspace_usage[] = { |
1ad7c0f6 JH |
17 | N_("git stripspace [-s | --strip-comments]"), |
18 | N_("git stripspace [-c | --comment-lines]"), | |
bed44524 TK |
19 | NULL |
20 | }; | |
21 | ||
22 | enum stripspace_mode { | |
23 | STRIP_DEFAULT = 0, | |
24 | STRIP_COMMENTS, | |
25 | COMMENT_LINES | |
26 | }; | |
eff80a9f | 27 | |
a633fca0 | 28 | int cmd_stripspace(int argc, const char **argv, const char *prefix) |
7499c996 | 29 | { |
f285a2d7 | 30 | struct strbuf buf = STRBUF_INIT; |
bed44524 TK |
31 | enum stripspace_mode mode = STRIP_DEFAULT; |
32 | ||
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, | |
f562d7de | 38 | N_("prepend comment character and space to each line"), |
bed44524 TK |
39 | COMMENT_LINES), |
40 | OPT_END() | |
41 | }; | |
42 | ||
43 | argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0); | |
44 | if (argc) | |
45 | usage_with_options(stripspace_usage, options); | |
46 | ||
92068ae8 JS |
47 | if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) { |
48 | setup_git_directory_gently(NULL); | |
eff80a9f | 49 | git_config(git_default_config, NULL); |
92068ae8 | 50 | } |
975e0daf | 51 | |
fd17f5b5 | 52 | if (strbuf_read(&buf, 0, 1024) < 0) |
0721c314 | 53 | die_errno("could not read the input"); |
975e0daf | 54 | |
bed44524 TK |
55 | if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS) |
56 | strbuf_stripspace(&buf, mode == STRIP_COMMENTS); | |
eff80a9f JH |
57 | else |
58 | comment_lines(&buf); | |
975e0daf | 59 | |
fd17f5b5 PH |
60 | write_or_die(1, buf.buf, buf.len); |
61 | strbuf_release(&buf); | |
a3e870f2 LT |
62 | return 0; |
63 | } |