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