blob: bdf032886912bb768cf3528efbffab4fe3029a58 [file] [log] [blame]
Lukas Sandström7499c992006-06-13 22:21:53 +02001#include "builtin.h"
Carlos Rica9690c112007-06-25 21:28:01 +02002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Tobias Klauserbed44522015-10-16 17:16:43 +02004#include "parse-options.h"
Tobias Klauser63af4a82015-10-16 17:16:42 +02005#include "strbuf.h"
Lukas Sandström7499c992006-06-13 22:21:53 +02006
Junio C Hamanoeff80a92013-01-16 20:18:48 +01007static 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
Tobias Klauserbed44522015-10-16 17:16:43 +020017static const char * const stripspace_usage[] = {
Junio C Hamano1ad7c0f2015-10-26 15:55:20 -070018 N_("git stripspace [-s | --strip-comments]"),
19 N_("git stripspace [-c | --comment-lines]"),
Tobias Klauserbed44522015-10-16 17:16:43 +020020 NULL
21};
22
23enum stripspace_mode {
24 STRIP_DEFAULT = 0,
25 STRIP_COMMENTS,
26 COMMENT_LINES
27};
Junio C Hamanoeff80a92013-01-16 20:18:48 +010028
Linus Torvaldsa633fca2006-07-28 22:44:25 -070029int cmd_stripspace(int argc, const char **argv, const char *prefix)
Lukas Sandström7499c992006-06-13 22:21:53 +020030{
Brandon Caseyf285a2d2008-10-09 14:12:12 -050031 struct strbuf buf = STRBUF_INIT;
Tobias Klauserbed44522015-10-16 17:16:43 +020032 enum stripspace_mode mode = STRIP_DEFAULT;
Johannes Schindelinf653aee2007-07-23 12:58:27 +010033
Tobias Klauserbed44522015-10-16 17:16:43 +020034 const struct option options[] = {
35 OPT_CMDMODE('s', "strip-comments", &mode,
36 N_("skip and remove all lines starting with comment character"),
37 STRIP_COMMENTS),
38 OPT_CMDMODE('c', "comment-lines", &mode,
Alex Henrief562d7d2016-01-28 20:10:56 -070039 N_("prepend comment character and space to each line"),
Tobias Klauserbed44522015-10-16 17:16:43 +020040 COMMENT_LINES),
41 OPT_END()
42 };
Junio C Hamanoeff80a92013-01-16 20:18:48 +010043
Tobias Klauserbed44522015-10-16 17:16:43 +020044 argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
45 if (argc)
46 usage_with_options(stripspace_usage, options);
Junio C Hamanoeff80a92013-01-16 20:18:48 +010047
Johannes Schindelin92068ae2016-11-21 15:18:24 +010048 if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
49 setup_git_directory_gently(NULL);
Junio C Hamanoeff80a92013-01-16 20:18:48 +010050 git_config(git_default_config, NULL);
Johannes Schindelin92068ae2016-11-21 15:18:24 +010051 }
Carlos Rica975e0da2007-07-11 20:50:34 +020052
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +020053 if (strbuf_read(&buf, 0, 1024) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020054 die_errno("could not read the input");
Carlos Rica975e0da2007-07-11 20:50:34 +020055
Tobias Klauserbed44522015-10-16 17:16:43 +020056 if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
57 strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
Junio C Hamanoeff80a92013-01-16 20:18:48 +010058 else
59 comment_lines(&buf);
Carlos Rica975e0da2007-07-11 20:50:34 +020060
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +020061 write_or_die(1, buf.buf, buf.len);
62 strbuf_release(&buf);
Linus Torvaldsa3e870f2005-05-30 12:51:00 -070063 return 0;
64}