2006-06-14 04:21:53 +08:00
|
|
|
#include "builtin.h"
|
2007-06-26 03:28:01 +08:00
|
|
|
#include "cache.h"
|
2017-06-15 02:07:36 +08:00
|
|
|
#include "config.h"
|
2015-10-16 23:16:43 +08:00
|
|
|
#include "parse-options.h"
|
2015-10-16 23:16:42 +08:00
|
|
|
#include "strbuf.h"
|
2006-06-14 04:21:53 +08:00
|
|
|
|
2013-01-17 03:18:48 +08:00
|
|
|
static void comment_lines(struct strbuf *buf)
|
|
|
|
{
|
|
|
|
char *msg;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
msg = strbuf_detach(buf, &len);
|
|
|
|
strbuf_add_commented_lines(buf, msg, len);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
|
2015-10-16 23:16:43 +08:00
|
|
|
static const char * const stripspace_usage[] = {
|
2015-10-27 06:55:20 +08:00
|
|
|
N_("git stripspace [-s | --strip-comments]"),
|
|
|
|
N_("git stripspace [-c | --comment-lines]"),
|
2015-10-16 23:16:43 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum stripspace_mode {
|
|
|
|
STRIP_DEFAULT = 0,
|
|
|
|
STRIP_COMMENTS,
|
|
|
|
COMMENT_LINES
|
|
|
|
};
|
2013-01-17 03:18:48 +08:00
|
|
|
|
2006-07-29 13:44:25 +08:00
|
|
|
int cmd_stripspace(int argc, const char **argv, const char *prefix)
|
2006-06-14 04:21:53 +08:00
|
|
|
{
|
2008-10-10 03:12:12 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2015-10-16 23:16:43 +08:00
|
|
|
enum stripspace_mode mode = STRIP_DEFAULT;
|
2018-12-18 00:59:57 +08:00
|
|
|
int nongit;
|
2015-10-16 23:16:43 +08:00
|
|
|
|
|
|
|
const struct option options[] = {
|
|
|
|
OPT_CMDMODE('s', "strip-comments", &mode,
|
|
|
|
N_("skip and remove all lines starting with comment character"),
|
|
|
|
STRIP_COMMENTS),
|
|
|
|
OPT_CMDMODE('c', "comment-lines", &mode,
|
2016-01-29 11:10:56 +08:00
|
|
|
N_("prepend comment character and space to each line"),
|
2015-10-16 23:16:43 +08:00
|
|
|
COMMENT_LINES),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0);
|
|
|
|
if (argc)
|
|
|
|
usage_with_options(stripspace_usage, options);
|
|
|
|
|
2016-11-21 22:18:24 +08:00
|
|
|
if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
|
2018-12-18 00:59:57 +08:00
|
|
|
setup_git_directory_gently(&nongit);
|
2013-01-17 03:18:48 +08:00
|
|
|
git_config(git_default_config, NULL);
|
2016-11-21 22:18:24 +08:00
|
|
|
}
|
2007-07-12 02:50:34 +08:00
|
|
|
|
2007-09-10 18:35:09 +08:00
|
|
|
if (strbuf_read(&buf, 0, 1024) < 0)
|
2009-06-27 23:58:47 +08:00
|
|
|
die_errno("could not read the input");
|
2007-07-12 02:50:34 +08:00
|
|
|
|
2015-10-16 23:16:43 +08:00
|
|
|
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
|
|
|
|
strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
|
2013-01-17 03:18:48 +08:00
|
|
|
else
|
|
|
|
comment_lines(&buf);
|
2007-07-12 02:50:34 +08:00
|
|
|
|
2007-09-10 18:35:09 +08:00
|
|
|
write_or_die(1, buf.buf, buf.len);
|
|
|
|
strbuf_release(&buf);
|
2005-05-31 03:51:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|