Skip to content

Commit e23469f

Browse files
committed
Merge branch 'tk/stripspace' into maint
The internal stripspace() function has been moved to where it logically belongs to, i.e. strbuf API, and the command line parser of "git stripspace" has been updated to use the parse_options API. * tk/stripspace: stripspace: use parse-options for command-line parsing strbuf: make stripspace() part of strbuf
2 parents f89baca + bed4452 commit e23469f

File tree

9 files changed

+118
-103
lines changed

9 files changed

+118
-103
lines changed

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ static int parse_mail(struct am_state *state, const char *mail)
13431343
strbuf_addstr(&msg, "\n\n");
13441344
if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
13451345
die_errno(_("could not read '%s'"), am_path(state, "msg"));
1346-
stripspace(&msg, 0);
1346+
strbuf_stripspace(&msg, 0);
13471347

13481348
if (state->signoff)
13491349
am_signoff(&msg);

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ static int edit_branch_description(const char *branch_name)
786786
strbuf_release(&buf);
787787
return -1;
788788
}
789-
stripspace(&buf, 1);
789+
strbuf_stripspace(&buf, 1);
790790

791791
strbuf_addf(&name, "branch.%s.description", branch_name);
792792
status = git_config_set(name.buf, buf.len ? buf.buf : NULL);

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
775775
s->hints = 0;
776776

777777
if (clean_message_contents)
778-
stripspace(&sb, 0);
778+
strbuf_stripspace(&sb, 0);
779779

780780
if (signoff)
781781
append_signoff(&sb, ignore_non_trailer(&sb), 0);
@@ -1014,7 +1014,7 @@ static int template_untouched(struct strbuf *sb)
10141014
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
10151015
return 0;
10161016

1017-
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1017+
strbuf_stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
10181018
if (!skip_prefix(sb->buf, tmpl.buf, &start))
10191019
start = sb->buf;
10201020
strbuf_release(&tmpl);
@@ -1726,7 +1726,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17261726
wt_status_truncate_message_at_cut_line(&sb);
17271727

17281728
if (cleanup_mode != CLEANUP_NONE)
1729-
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1729+
strbuf_stripspace(&sb, cleanup_mode == CLEANUP_ALL);
17301730
if (template_untouched(&sb) && !allow_empty_message) {
17311731
rollback_index_files();
17321732
fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
806806
abort_commit(remoteheads, NULL);
807807
}
808808
read_merge_msg(&msg);
809-
stripspace(&msg, 0 < option_edit);
809+
strbuf_stripspace(&msg, 0 < option_edit);
810810
if (!msg.len)
811811
abort_commit(remoteheads, _("Empty commit message."));
812812
strbuf_release(&merge_msg);

builtin/notes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static void prepare_note_data(const unsigned char *object, struct note_data *d,
192192
if (launch_editor(d->edit_path, &d->buf, NULL)) {
193193
die(_("Please supply the note contents using either -m or -F option"));
194194
}
195-
stripspace(&d->buf, 1);
195+
strbuf_stripspace(&d->buf, 1);
196196
}
197197
}
198198

@@ -215,7 +215,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
215215
if (d->buf.len)
216216
strbuf_addch(&d->buf, '\n');
217217
strbuf_addstr(&d->buf, arg);
218-
stripspace(&d->buf, 0);
218+
strbuf_stripspace(&d->buf, 0);
219219

220220
d->given = 1;
221221
return 0;
@@ -232,7 +232,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
232232
die_errno(_("cannot read '%s'"), arg);
233233
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
234234
die_errno(_("could not open or read '%s'"), arg);
235-
stripspace(&d->buf, 0);
235+
strbuf_stripspace(&d->buf, 0);
236236

237237
d->given = 1;
238238
return 0;

builtin/stripspace.c

Lines changed: 32 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,7 @@
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

706
static 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

8428
int 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

builtin/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void create_tag(const unsigned char *object, const char *tag,
498498
}
499499

500500
if (opt->cleanup_mode != CLEANUP_NONE)
501-
stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
501+
strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
502502

503503
if (!opt->message_given && !buf->len)
504504
die(_("no tag message?"));

strbuf.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,69 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
743743
}
744744
strbuf_setlen(sb, sb->len + len);
745745
}
746+
747+
/*
748+
* Returns the length of a line, without trailing spaces.
749+
*
750+
* If the line ends with newline, it will be removed too.
751+
*/
752+
static size_t cleanup(char *line, size_t len)
753+
{
754+
while (len) {
755+
unsigned char c = line[len - 1];
756+
if (!isspace(c))
757+
break;
758+
len--;
759+
}
760+
761+
return len;
762+
}
763+
764+
/*
765+
* Remove empty lines from the beginning and end
766+
* and also trailing spaces from every line.
767+
*
768+
* Turn multiple consecutive empty lines between paragraphs
769+
* into just one empty line.
770+
*
771+
* If the input has only empty lines and spaces,
772+
* no output will be produced.
773+
*
774+
* If last line does not have a newline at the end, one is added.
775+
*
776+
* Enable skip_comments to skip every line starting with comment
777+
* character.
778+
*/
779+
void strbuf_stripspace(struct strbuf *sb, int skip_comments)
780+
{
781+
int empties = 0;
782+
size_t i, j, len, newlen;
783+
char *eol;
784+
785+
/* We may have to add a newline. */
786+
strbuf_grow(sb, 1);
787+
788+
for (i = j = 0; i < sb->len; i += len, j += newlen) {
789+
eol = memchr(sb->buf + i, '\n', sb->len - i);
790+
len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
791+
792+
if (skip_comments && len && sb->buf[i] == comment_line_char) {
793+
newlen = 0;
794+
continue;
795+
}
796+
newlen = cleanup(sb->buf + i, len);
797+
798+
/* Not just an empty line? */
799+
if (newlen) {
800+
if (empties > 0 && j > 0)
801+
sb->buf[j++] = '\n';
802+
empties = 0;
803+
memmove(sb->buf + j, sb->buf + i, newlen);
804+
sb->buf[newlen + j++] = '\n';
805+
} else {
806+
empties++;
807+
}
808+
}
809+
810+
strbuf_setlen(sb, j);
811+
}

strbuf.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,16 @@ extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
418418
* Strip whitespace from a buffer. The second parameter controls if
419419
* comments are considered contents to be removed or not.
420420
*/
421-
extern void stripspace(struct strbuf *buf, int skip_comments);
421+
extern void strbuf_stripspace(struct strbuf *buf, int skip_comments);
422+
423+
/**
424+
* Temporary alias until all topic branches have switched to use
425+
* strbuf_stripspace directly.
426+
*/
427+
static inline void stripspace(struct strbuf *buf, int skip_comments)
428+
{
429+
strbuf_stripspace(buf, skip_comments);
430+
}
422431

423432
static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
424433
{

0 commit comments

Comments
 (0)