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