]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/stripspace.c
usage: do not insist that standard input must come from a file
[thirdparty/git.git] / builtin / stripspace.c
CommitLineData
7499c996 1#include "builtin.h"
9690c118 2#include "cache.h"
a3e870f2
LT
3
4/*
975e0daf 5 * Returns the length of a line, without trailing spaces.
a3e870f2 6 *
9690c118 7 * If the line ends with newline, it will be removed too.
a3e870f2 8 */
975e0daf 9static size_t cleanup(char *line, size_t len)
a3e870f2 10{
6d69b6f6
KH
11 while (len) {
12 unsigned char c = line[len - 1];
13 if (!isspace(c))
14 break;
15 len--;
a3e870f2 16 }
6d69b6f6 17
9690c118 18 return len;
a3e870f2
LT
19}
20
9690c118
CR
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 *
6d69b6f6 31 * If last line does not have a newline at the end, one is added.
975e0daf 32 *
eff80a9f
JH
33 * Enable skip_comments to skip every line starting with comment
34 * character.
9690c118 35 */
6d69b6f6 36void stripspace(struct strbuf *sb, int skip_comments)
a3e870f2 37{
6d69b6f6 38 int empties = 0;
975e0daf
CR
39 size_t i, j, len, newlen;
40 char *eol;
9690c118 41
6d69b6f6
KH
42 /* We may have to add a newline. */
43 strbuf_grow(sb, 1);
a3e870f2 44
6d69b6f6
KH
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
eff80a9f 49 if (skip_comments && len && sb->buf[i] == comment_line_char) {
975e0daf 50 newlen = 0;
9690c118 51 continue;
975e0daf 52 }
6d69b6f6 53 newlen = cleanup(sb->buf + i, len);
a3e870f2
LT
54
55 /* Not just an empty line? */
975e0daf 56 if (newlen) {
6d69b6f6
KH
57 if (empties > 0 && j > 0)
58 sb->buf[j++] = '\n';
a3e870f2 59 empties = 0;
6d69b6f6
KH
60 memmove(sb->buf + j, sb->buf + i, newlen);
61 sb->buf[newlen + j++] = '\n';
62 } else {
63 empties++;
a3e870f2 64 }
a3e870f2 65 }
975e0daf 66
6d69b6f6 67 strbuf_setlen(sb, j);
7499c996
LS
68}
69
eff80a9f
JH
70static void comment_lines(struct strbuf *buf)
71{
72 char *msg;
73 size_t len;
74
75 msg = strbuf_detach(buf, &len);
76 strbuf_add_commented_lines(buf, msg, len);
77 free(msg);
78}
79
80static const char *usage_msg = "\n"
33e8fc87
JH
81" git stripspace [-s | --strip-comments]\n"
82" git stripspace [-c | --comment-lines]";
eff80a9f 83
a633fca0 84int cmd_stripspace(int argc, const char **argv, const char *prefix)
7499c996 85{
f285a2d7 86 struct strbuf buf = STRBUF_INIT;
f653aee5 87 int strip_comments = 0;
eff80a9f
JH
88 enum { INVAL = 0, STRIP_SPACE = 1, COMMENT_LINES = 2 } mode = STRIP_SPACE;
89
90 if (argc == 2) {
91 if (!strcmp(argv[1], "-s") ||
1686e2cc
MM
92 !strcmp(argv[1], "--strip-comments")) {
93 strip_comments = 1;
eff80a9f 94 } else if (!strcmp(argv[1], "-c") ||
1686e2cc
MM
95 !strcmp(argv[1], "--comment-lines")) {
96 mode = COMMENT_LINES;
eff80a9f
JH
97 } else {
98 mode = INVAL;
99 }
100 } else if (argc > 1) {
101 mode = INVAL;
102 }
103
104 if (mode == INVAL)
105 usage(usage_msg);
f653aee5 106
eff80a9f
JH
107 if (strip_comments || mode == COMMENT_LINES)
108 git_config(git_default_config, NULL);
975e0daf 109
fd17f5b5 110 if (strbuf_read(&buf, 0, 1024) < 0)
0721c314 111 die_errno("could not read the input");
975e0daf 112
eff80a9f
JH
113 if (mode == STRIP_SPACE)
114 stripspace(&buf, strip_comments);
115 else
116 comment_lines(&buf);
975e0daf 117
fd17f5b5
PH
118 write_or_die(1, buf.buf, buf.len);
119 strbuf_release(&buf);
a3e870f2
LT
120 return 0;
121}