2006-06-14 00:21:53 +04:00
|
|
|
#include "builtin.h"
|
2007-06-25 23:28:01 +04:00
|
|
|
#include "cache.h"
|
2015-10-16 18:16:43 +03:00
|
|
|
#include "parse-options.h"
|
2015-10-16 18:16:42 +03:00
|
|
|
#include "strbuf.h"
|
2006-06-14 00:21:53 +04:00
|
|
|
|
2013-01-16 23:18:48 +04: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 18:16:43 +03:00
|
|
|
static const char * const stripspace_usage[] = {
|
2015-10-27 01:55:20 +03:00
|
|
|
N_("git stripspace [-s | --strip-comments]"),
|
|
|
|
N_("git stripspace [-c | --comment-lines]"),
|
2015-10-16 18:16:43 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum stripspace_mode {
|
|
|
|
STRIP_DEFAULT = 0,
|
|
|
|
STRIP_COMMENTS,
|
|
|
|
COMMENT_LINES
|
|
|
|
};
|
2013-01-16 23:18:48 +04:00
|
|
|
|
2006-07-29 09:44:25 +04:00
|
|
|
int cmd_stripspace(int argc, const char **argv, const char *prefix)
|
2006-06-14 00:21:53 +04:00
|
|
|
{
|
2008-10-09 23:12:12 +04:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2015-10-16 18:16:43 +03:00
|
|
|
enum stripspace_mode mode = STRIP_DEFAULT;
|
|
|
|
|
|
|
|
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 06:10:56 +03:00
|
|
|
N_("prepend comment character and space to each line"),
|
2015-10-16 18:16:43 +03: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 17:18:24 +03:00
|
|
|
if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
|
|
|
|
setup_git_directory_gently(NULL);
|
2013-01-16 23:18:48 +04:00
|
|
|
git_config(git_default_config, NULL);
|
2016-11-21 17:18:24 +03:00
|
|
|
}
|
2007-07-11 22:50:34 +04:00
|
|
|
|
2007-09-10 14:35:09 +04:00
|
|
|
if (strbuf_read(&buf, 0, 1024) < 0)
|
2009-06-27 19:58:47 +04:00
|
|
|
die_errno("could not read the input");
|
2007-07-11 22:50:34 +04:00
|
|
|
|
2015-10-16 18:16:43 +03:00
|
|
|
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
|
|
|
|
strbuf_stripspace(&buf, mode == STRIP_COMMENTS);
|
2013-01-16 23:18:48 +04:00
|
|
|
else
|
|
|
|
comment_lines(&buf);
|
2007-07-11 22:50:34 +04:00
|
|
|
|
2007-09-10 14:35:09 +04:00
|
|
|
write_or_die(1, buf.buf, buf.len);
|
|
|
|
strbuf_release(&buf);
|
2005-05-30 23:51:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|