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