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