11#include "builtin.h"
22#include "cache.h"
3-
4- /*
5- * Returns the length of a line, without trailing spaces.
6- *
7- * If the line ends with newline, it will be removed too.
8- */
9- static size_t cleanup (char * line , size_t len )
10- {
11- while (len ) {
12- unsigned char c = line [len - 1 ];
13- if (!isspace (c ))
14- break ;
15- len -- ;
16- }
17-
18- return len ;
19- }
20-
21- /*
22- * Remove empty lines from the beginning and end
23- * and also trailing spaces from every line.
24- *
25- * Turn multiple consecutive empty lines between paragraphs
26- * into just one empty line.
27- *
28- * If the input has only empty lines and spaces,
29- * no output will be produced.
30- *
31- * If last line does not have a newline at the end, one is added.
32- *
33- * Enable skip_comments to skip every line starting with comment
34- * character.
35- */
36- void stripspace (struct strbuf * sb , int skip_comments )
37- {
38- int empties = 0 ;
39- size_t i , j , len , newlen ;
40- char * eol ;
41-
42- /* We may have to add a newline. */
43- strbuf_grow (sb , 1 );
44-
45- for (i = j = 0 ; i < sb -> len ; i += len , j += newlen ) {
46- eol = memchr (sb -> buf + i , '\n' , sb -> len - i );
47- len = eol ? eol - (sb -> buf + i ) + 1 : sb -> len - i ;
48-
49- if (skip_comments && len && sb -> buf [i ] == comment_line_char ) {
50- newlen = 0 ;
51- continue ;
52- }
53- newlen = cleanup (sb -> buf + i , len );
54-
55- /* Not just an empty line? */
56- if (newlen ) {
57- if (empties > 0 && j > 0 )
58- sb -> buf [j ++ ] = '\n' ;
59- empties = 0 ;
60- memmove (sb -> buf + j , sb -> buf + i , newlen );
61- sb -> buf [newlen + j ++ ] = '\n' ;
62- } else {
63- empties ++ ;
64- }
65- }
66-
67- strbuf_setlen (sb , j );
68- }
3+ #include "parse-options.h"
4+ #include "strbuf.h"
695
706static void comment_lines (struct strbuf * buf )
717{
@@ -77,41 +13,45 @@ static void comment_lines(struct strbuf *buf)
7713 free (msg );
7814}
7915
80- static const char * usage_msg = "\n"
81- " git stripspace [-s | --strip-comments] < input\n"
82- " git stripspace [-c | --comment-lines] < input" ;
16+ static const char * const stripspace_usage [] = {
17+ N_ ("git stripspace [-s | --strip-comments] < input" ),
18+ N_ ("git stripspace [-c | --comment-lines] < input" ),
19+ NULL
20+ };
21+
22+ enum stripspace_mode {
23+ STRIP_DEFAULT = 0 ,
24+ STRIP_COMMENTS ,
25+ COMMENT_LINES
26+ };
8327
8428int cmd_stripspace (int argc , const char * * argv , const char * prefix )
8529{
8630 struct strbuf buf = STRBUF_INIT ;
87- int strip_comments = 0 ;
88- enum { INVAL = 0 , STRIP_SPACE = 1 , COMMENT_LINES = 2 } mode = STRIP_SPACE ;
89-
90- if (argc == 2 ) {
91- if (!strcmp (argv [1 ], "-s" ) ||
92- !strcmp (argv [1 ], "--strip-comments" )) {
93- strip_comments = 1 ;
94- } else if (!strcmp (argv [1 ], "-c" ) ||
95- !strcmp (argv [1 ], "--comment-lines" )) {
96- mode = COMMENT_LINES ;
97- } else {
98- mode = INVAL ;
99- }
100- } else if (argc > 1 ) {
101- mode = INVAL ;
102- }
103-
104- if (mode == INVAL )
105- usage (usage_msg );
106-
107- if (strip_comments || mode == COMMENT_LINES )
31+ enum stripspace_mode mode = STRIP_DEFAULT ;
32+
33+ const struct option options [] = {
34+ OPT_CMDMODE ('s' , "strip-comments" , & mode ,
35+ N_ ("skip and remove all lines starting with comment character" ),
36+ STRIP_COMMENTS ),
37+ OPT_CMDMODE ('c' , "comment-lines" , & mode ,
38+ N_ ("prepend comment character and blank to each line" ),
39+ COMMENT_LINES ),
40+ OPT_END ()
41+ };
42+
43+ argc = parse_options (argc , argv , prefix , options , stripspace_usage , 0 );
44+ if (argc )
45+ usage_with_options (stripspace_usage , options );
46+
47+ if (mode == STRIP_COMMENTS || mode == COMMENT_LINES )
10848 git_config (git_default_config , NULL );
10949
11050 if (strbuf_read (& buf , 0 , 1024 ) < 0 )
11151 die_errno ("could not read the input" );
11252
113- if (mode == STRIP_SPACE )
114- stripspace (& buf , strip_comments );
53+ if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS )
54+ strbuf_stripspace (& buf , mode == STRIP_COMMENTS );
11555 else
11656 comment_lines (& buf );
11757
0 commit comments