]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-stripspace.c
5 * Returns the length of a line, without trailing spaces.
7 * If the line ends with newline, it will be removed too.
9 static size_t cleanup(char *line
, size_t len
)
12 unsigned char c
= line
[len
- 1];
22 * Remove empty lines from the beginning and end
23 * and also trailing spaces from every line.
25 * Note that the buffer will not be NUL-terminated.
27 * Turn multiple consecutive empty lines between paragraphs
28 * into just one empty line.
30 * If the input has only empty lines and spaces,
31 * no output will be produced.
33 * If last line does not have a newline at the end, one is added.
35 * Enable skip_comments to skip every line starting with "#".
37 void stripspace(struct strbuf
*sb
, int skip_comments
)
40 size_t i
, j
, len
, newlen
;
43 /* We may have to add a newline. */
46 for (i
= j
= 0; i
< sb
->len
; i
+= len
, j
+= newlen
) {
47 eol
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
48 len
= eol
? eol
- (sb
->buf
+ i
) + 1 : sb
->len
- i
;
50 if (skip_comments
&& len
&& sb
->buf
[i
] == '#') {
54 newlen
= cleanup(sb
->buf
+ i
, len
);
56 /* Not just an empty line? */
58 if (empties
> 0 && j
> 0)
61 memmove(sb
->buf
+ j
, sb
->buf
+ i
, newlen
);
62 sb
->buf
[newlen
+ j
++] = '\n';
71 int cmd_stripspace(int argc
, const char **argv
, const char *prefix
)
73 struct strbuf buf
= STRBUF_INIT
;
74 int strip_comments
= 0;
76 if (argc
== 2 && (!strcmp(argv
[1], "-s") ||
77 !strcmp(argv
[1], "--strip-comments")))
80 usage("git stripspace [-s | --strip-comments] < <stream>");
82 if (strbuf_read(&buf
, 0, 1024) < 0)
83 die_errno("could not read the input");
85 stripspace(&buf
, strip_comments
);
87 write_or_die(1, buf
.buf
, buf
.len
);