Git fork
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"
10
11static void comment_lines(struct strbuf *buf)
12{
13 char *msg;
14 size_t len;
15
16 msg = strbuf_detach(buf, &len);
17 strbuf_add_commented_lines(buf, msg, len, comment_line_str);
18 free(msg);
19}
20
21static const char * const stripspace_usage[] = {
22 "git stripspace [-s | --strip-comments]",
23 "git stripspace [-c | --comment-lines]",
24 NULL
25};
26
27enum stripspace_mode {
28 STRIP_DEFAULT = 0,
29 STRIP_COMMENTS,
30 COMMENT_LINES
31};
32
33int cmd_stripspace(int argc,
34 const char **argv,
35 const char *prefix,
36 struct repository *repo UNUSED)
37{
38 struct strbuf buf = STRBUF_INIT;
39 enum stripspace_mode mode = STRIP_DEFAULT;
40 int nongit;
41
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()
50 };
51
52 argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
53 if (argc)
54 usage_with_options(stripspace_usage, options);
55
56 if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
57 setup_git_directory_gently(&nongit);
58 repo_config(the_repository, git_default_config, NULL);
59 }
60
61 if (strbuf_read(&buf, 0, 1024) < 0)
62 die_errno("could not read the input");
63
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);
69
70 write_or_die(1, buf.buf, buf.len);
71 strbuf_release(&buf);
72 return 0;
73}