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