Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 2 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 4 | #include "parse-options.h" |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 5 | #include "strbuf.h" |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 6 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 7 | static void comment_lines(struct strbuf *buf) |
| 8 | { |
| 9 | char *msg; |
| 10 | size_t len; |
| 11 | |
| 12 | msg = strbuf_detach(buf, &len); |
| 13 | strbuf_add_commented_lines(buf, msg, len); |
| 14 | free(msg); |
| 15 | } |
| 16 | |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 17 | static const char * const stripspace_usage[] = { |
Junio C Hamano | 1ad7c0f | 2015-10-26 15:55:20 -0700 | [diff] [blame] | 18 | N_("git stripspace [-s | --strip-comments]"), |
| 19 | N_("git stripspace [-c | --comment-lines]"), |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 20 | NULL |
| 21 | }; |
| 22 | |
| 23 | enum stripspace_mode { |
| 24 | STRIP_DEFAULT = 0, |
| 25 | STRIP_COMMENTS, |
| 26 | COMMENT_LINES |
| 27 | }; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 28 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 29 | int cmd_stripspace(int argc, const char **argv, const char *prefix) |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 30 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 31 | struct strbuf buf = STRBUF_INIT; |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 32 | enum stripspace_mode mode = STRIP_DEFAULT; |
Johannes Schindelin | f653aee | 2007-07-23 12:58:27 +0100 | [diff] [blame] | 33 | |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 34 | const struct option options[] = { |
| 35 | OPT_CMDMODE('s', "strip-comments", &mode, |
| 36 | N_("skip and remove all lines starting with comment character"), |
| 37 | STRIP_COMMENTS), |
| 38 | OPT_CMDMODE('c', "comment-lines", &mode, |
Alex Henrie | f562d7d | 2016-01-28 20:10:56 -0700 | [diff] [blame] | 39 | N_("prepend comment character and space to each line"), |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 40 | COMMENT_LINES), |
| 41 | OPT_END() |
| 42 | }; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 43 | |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 44 | argc = parse_options(argc, argv, prefix, options, stripspace_usage, 0); |
| 45 | if (argc) |
| 46 | usage_with_options(stripspace_usage, options); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 47 | |
Johannes Schindelin | 92068ae | 2016-11-21 15:18:24 +0100 | [diff] [blame] | 48 | if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) { |
| 49 | setup_git_directory_gently(NULL); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 50 | git_config(git_default_config, NULL); |
Johannes Schindelin | 92068ae | 2016-11-21 15:18:24 +0100 | [diff] [blame] | 51 | } |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 52 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 53 | if (strbuf_read(&buf, 0, 1024) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 54 | die_errno("could not read the input"); |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 55 | |
Tobias Klauser | bed4452 | 2015-10-16 17:16:43 +0200 | [diff] [blame] | 56 | if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS) |
| 57 | strbuf_stripspace(&buf, mode == STRIP_COMMENTS); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 58 | else |
| 59 | comment_lines(&buf); |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 60 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 61 | write_or_die(1, buf.buf, buf.len); |
| 62 | strbuf_release(&buf); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |