Skip to content

Commit 8f309ae

Browse files
committed
strbuf: introduce strbuf_getline_{lf,nul}()
The strbuf_getline() interface allows a byte other than LF or NUL as the line terminator, but this is only because I wrote these codepaths anticipating that there might be a value other than NUL and LF that could be useful when I introduced line_termination long time ago. No useful caller that uses other value has emerged. By now, it is clear that the interface is overly broad without a good reason. Many codepaths have hardcoded preference to read either LF terminated or NUL terminated records from their input, and then call strbuf_getline() with LF or NUL as the third parameter. This step introduces two thin wrappers around strbuf_getline(), namely, strbuf_getline_lf() and strbuf_getline_nul(), and mechanically rewrites these call sites to call either one of them. The changes contained in this patch are: * introduction of these two functions in strbuf.[ch] * mechanical conversion of all callers to strbuf_getline() with either '\n' or '\0' as the third parameter to instead call the respective thin wrapper. After this step, output from "git grep 'strbuf_getline('" would become a lot smaller. An interim goal of this series is to make this an empty set, so that we can have strbuf_getline_crlf() take over the shorter name strbuf_getline(). Signed-off-by: Junio C Hamano <[email protected]>
1 parent c8aa9fd commit 8f309ae

36 files changed

+81
-59
lines changed

bisect.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static void read_bisect_paths(struct argv_array *array)
440440
if (!fp)
441441
die_errno("Could not open file '%s'", filename);
442442

443-
while (strbuf_getline(&str, fp, '\n') != EOF) {
443+
while (strbuf_getline_lf(&str, fp) != EOF) {
444444
strbuf_trim(&str);
445445
if (sq_dequote_to_argv_array(str.buf, array))
446446
die("Badly quoted content in file '%s': %s",
@@ -668,7 +668,7 @@ static int is_expected_rev(const struct object_id *oid)
668668
if (!fp)
669669
return 0;
670670

671-
if (strbuf_getline(&str, fp, '\n') != EOF)
671+
if (strbuf_getline_lf(&str, fp) != EOF)
672672
res = !strcmp(str.buf, oid_to_hex(oid));
673673

674674
strbuf_release(&str);
@@ -914,9 +914,9 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
914914
strerror(errno));
915915
}
916916
} else {
917-
strbuf_getline(&str, fp, '\n');
917+
strbuf_getline_lf(&str, fp);
918918
*read_bad = strbuf_detach(&str, NULL);
919-
strbuf_getline(&str, fp, '\n');
919+
strbuf_getline_lf(&str, fp);
920920
*read_good = strbuf_detach(&str, NULL);
921921
}
922922
strbuf_release(&str);

builtin/am.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static char *read_shell_var(FILE *fp, const char *key)
269269
struct strbuf sb = STRBUF_INIT;
270270
const char *str;
271271

272-
if (strbuf_getline(&sb, fp, '\n'))
272+
if (strbuf_getline_lf(&sb, fp))
273273
goto fail;
274274

275275
if (!skip_prefix(sb.buf, key, &str))
@@ -558,7 +558,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
558558

559559
fp = xfopen(am_path(state, "rewritten"), "r");
560560

561-
while (!strbuf_getline(&sb, fp, '\n')) {
561+
while (!strbuf_getline_lf(&sb, fp)) {
562562
unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
563563

564564
if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
@@ -802,7 +802,7 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
802802
struct strbuf sb = STRBUF_INIT;
803803
int subject_printed = 0;
804804

805-
while (!strbuf_getline(&sb, in, '\n')) {
805+
while (!strbuf_getline_lf(&sb, in)) {
806806
const char *str;
807807

808808
if (str_isspace(sb.buf))
@@ -860,7 +860,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
860860
return error(_("could not open '%s' for reading: %s"), *paths,
861861
strerror(errno));
862862

863-
while (!strbuf_getline(&sb, fp, '\n')) {
863+
while (!strbuf_getline_lf(&sb, fp)) {
864864
if (*sb.buf == '#')
865865
continue; /* skip comment lines */
866866

@@ -885,7 +885,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
885885
{
886886
struct strbuf sb = STRBUF_INIT;
887887

888-
while (!strbuf_getline(&sb, in, '\n')) {
888+
while (!strbuf_getline_lf(&sb, in)) {
889889
const char *str;
890890

891891
if (skip_prefix(sb.buf, "# User ", &str))
@@ -1302,7 +1302,7 @@ static int parse_mail(struct am_state *state, const char *mail)
13021302

13031303
/* Extract message and author information */
13041304
fp = xfopen(am_path(state, "info"), "r");
1305-
while (!strbuf_getline(&sb, fp, '\n')) {
1305+
while (!strbuf_getline_lf(&sb, fp)) {
13061306
const char *x;
13071307

13081308
if (skip_prefix(sb.buf, "Subject: ", &x)) {
@@ -1368,7 +1368,7 @@ static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
13681368
FILE *fp = xfopen(mail, "r");
13691369
const char *x;
13701370

1371-
if (strbuf_getline(&sb, fp, '\n'))
1371+
if (strbuf_getline_lf(&sb, fp))
13721372
return -1;
13731373

13741374
if (!skip_prefix(sb.buf, "From ", &x))

builtin/cat-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ static int batch_objects(struct batch_options *opt)
401401
save_warning = warn_on_object_refname_ambiguity;
402402
warn_on_object_refname_ambiguity = 0;
403403

404-
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
404+
while (strbuf_getline_lf(&buf, stdin) != EOF) {
405405
if (data.split_on_whitespace) {
406406
/*
407407
* Split at first whitespace, tying off the beginning

builtin/check-mailmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
5454

5555
if (use_stdin) {
5656
struct strbuf buf = STRBUF_INIT;
57-
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
57+
while (strbuf_getline_lf(&buf, stdin) != EOF) {
5858
check_mailmap(&mailmap, buf.buf);
5959
maybe_flush_or_die(stdout, "stdout");
6060
}

builtin/clean.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
594594
clean_get_color(CLEAN_COLOR_RESET));
595595
}
596596

597-
if (strbuf_getline(&choice, stdin, '\n') != EOF) {
597+
if (strbuf_getline_lf(&choice, stdin) != EOF) {
598598
strbuf_trim(&choice);
599599
} else {
600600
eof = 1;
@@ -676,7 +676,7 @@ static int filter_by_patterns_cmd(void)
676676
clean_print_color(CLEAN_COLOR_PROMPT);
677677
printf(_("Input ignore patterns>> "));
678678
clean_print_color(CLEAN_COLOR_RESET);
679-
if (strbuf_getline(&confirm, stdin, '\n') != EOF)
679+
if (strbuf_getline_lf(&confirm, stdin) != EOF)
680680
strbuf_trim(&confirm);
681681
else
682682
putchar('\n');
@@ -774,7 +774,7 @@ static int ask_each_cmd(void)
774774
qname = quote_path_relative(item->string, NULL, &buf);
775775
/* TRANSLATORS: Make sure to keep [y/N] as is */
776776
printf(_("Remove %s [y/N]? "), qname);
777-
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
777+
if (strbuf_getline_lf(&confirm, stdin) != EOF) {
778778
strbuf_trim(&confirm);
779779
} else {
780780
putchar('\n');

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static void copy_alternates(struct strbuf *src, struct strbuf *dst,
339339
FILE *in = fopen(src->buf, "r");
340340
struct strbuf line = STRBUF_INIT;
341341

342-
while (strbuf_getline(&line, in, '\n') != EOF) {
342+
while (strbuf_getline_lf(&line, in) != EOF) {
343343
char *abs_path;
344344
if (!line.len || line.buf[0] == '#')
345345
continue;

builtin/column.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
5151
die(_("--command must be the first argument"));
5252
}
5353
finalize_colopts(&colopts, -1);
54-
while (!strbuf_getline(&sb, stdin, '\n'))
54+
while (!strbuf_getline_lf(&sb, stdin))
5555
string_list_append(&list, sb.buf);
5656

5757
print_columns(&list, colopts, &copts);

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16901690
if (fp == NULL)
16911691
die_errno(_("could not open '%s' for reading"),
16921692
git_path_merge_head());
1693-
while (strbuf_getline(&m, fp, '\n') != EOF) {
1693+
while (strbuf_getline_lf(&m, fp) != EOF) {
16941694
struct commit *parent;
16951695

16961696
parent = get_merge_parent(m.buf);

builtin/fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
158158
else {
159159
/* read from stdin one ref per line, until EOF */
160160
struct strbuf line = STRBUF_INIT;
161-
while (strbuf_getline(&line, stdin, '\n') != EOF)
161+
while (strbuf_getline_lf(&line, stdin) != EOF)
162162
add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
163163
strbuf_release(&line);
164164
}

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
562562
patterns = from_stdin ? stdin : fopen(arg, "r");
563563
if (!patterns)
564564
die_errno(_("cannot open '%s'"), arg);
565-
while (strbuf_getline(&sb, patterns, '\n') == 0) {
565+
while (strbuf_getline_lf(&sb, patterns) == 0) {
566566
/* ignore empty line like grep does */
567567
if (sb.len == 0)
568568
continue;

builtin/hash-object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
6060
{
6161
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
6262

63-
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
63+
while (strbuf_getline_lf(&buf, stdin) != EOF) {
6464
if (buf.buf[0] == '"') {
6565
strbuf_reset(&nbuf);
6666
if (unquote_c_style(&nbuf, buf.buf, NULL))

builtin/notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
290290
t = &default_notes_tree;
291291
}
292292

293-
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
293+
while (strbuf_getline_lf(&buf, stdin) != EOF) {
294294
unsigned char from_obj[20], to_obj[20];
295295
struct strbuf **split;
296296
int err;

builtin/pull.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static void get_merge_heads(struct sha1_array *merge_heads)
378378

379379
if (!(fp = fopen(filename, "r")))
380380
die_errno(_("could not open '%s' for reading"), filename);
381-
while (strbuf_getline(&sb, fp, '\n') != EOF) {
381+
while (strbuf_getline_lf(&sb, fp) != EOF) {
382382
if (get_sha1_hex(sb.buf, sha1))
383383
continue; /* invalid line: does not start with SHA1 */
384384
if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
266266
return ret;
267267

268268
out = xfdopen(cmd.out, "r");
269-
while (strbuf_getline(&line, out, '\n') != EOF) {
269+
while (strbuf_getline_lf(&line, out) != EOF) {
270270
if (line.len != 40)
271271
die("repack: Expecting 40 character sha1 lines only from pack-objects.");
272272
string_list_append(&names, line.buf);

builtin/rev-parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
383383

384384
/* get the usage up to the first line with a -- on it */
385385
for (;;) {
386-
if (strbuf_getline(&sb, stdin, '\n') == EOF)
386+
if (strbuf_getline_lf(&sb, stdin) == EOF)
387387
die("premature end of input");
388388
ALLOC_GROW(usage, unb + 1, usz);
389389
if (!strcmp("--", sb.buf)) {
@@ -396,7 +396,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
396396
}
397397

398398
/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
399-
while (strbuf_getline(&sb, stdin, '\n') != EOF) {
399+
while (strbuf_getline_lf(&sb, stdin) != EOF) {
400400
const char *s;
401401
const char *help;
402402
struct option *o;

builtin/send-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
212212
argv_array_push(&all_refspecs, buf);
213213
} else {
214214
struct strbuf line = STRBUF_INIT;
215-
while (strbuf_getline(&line, stdin, '\n') != EOF)
215+
while (strbuf_getline_lf(&line, stdin) != EOF)
216216
argv_array_push(&all_refspecs, line.buf);
217217
strbuf_release(&line);
218218
}

compat/terminal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ char *git_terminal_prompt(const char *prompt, int echo)
122122
fputs(prompt, output_fh);
123123
fflush(output_fh);
124124

125-
r = strbuf_getline(&buf, input_fh, '\n');
125+
r = strbuf_getline_lf(&buf, input_fh);
126126
if (!echo) {
127127
putc('\n', output_fh);
128128
fflush(output_fh);

credential-cache--daemon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ static int read_request(FILE *fh, struct credential *c,
9696
static struct strbuf item = STRBUF_INIT;
9797
const char *p;
9898

99-
strbuf_getline(&item, fh, '\n');
99+
strbuf_getline_lf(&item, fh);
100100
if (!skip_prefix(item.buf, "action=", &p))
101101
return error("client sent bogus action line: %s", item.buf);
102102
strbuf_addstr(action, p);
103103

104-
strbuf_getline(&item, fh, '\n');
104+
strbuf_getline_lf(&item, fh);
105105
if (!skip_prefix(item.buf, "timeout=", &p))
106106
return error("client sent bogus timeout line: %s", item.buf);
107107
*timeout = atoi(p);

credential-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static int parse_credential_file(const char *fn,
2323
return found_credential;
2424
}
2525

26-
while (strbuf_getline(&line, fh, '\n') != EOF) {
26+
while (strbuf_getline_lf(&line, fh) != EOF) {
2727
credential_from_url(&entry, line.buf);
2828
if (entry.username && entry.password &&
2929
credential_match(c, &entry)) {

credential.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int credential_read(struct credential *c, FILE *fp)
142142
{
143143
struct strbuf line = STRBUF_INIT;
144144

145-
while (strbuf_getline(&line, fp, '\n') != EOF) {
145+
while (strbuf_getline_lf(&line, fp) != EOF) {
146146
char *key = line.buf;
147147
char *value = strchr(key, '=');
148148

daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static void copy_to_log(int fd)
424424
return;
425425
}
426426

427-
while (strbuf_getline(&line, fp, '\n') != EOF) {
427+
while (strbuf_getline_lf(&line, fp) != EOF) {
428428
logerror("%s", line.buf);
429429
strbuf_setlen(&line, 0);
430430
}

fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ static int read_next_command(void)
18881888
struct recent_command *rc;
18891889

18901890
strbuf_detach(&command_buf, NULL);
1891-
stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
1891+
stdin_eof = strbuf_getline_lf(&command_buf, stdin);
18921892
if (stdin_eof)
18931893
return EOF;
18941894

@@ -1960,7 +1960,7 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
19601960

19611961
strbuf_detach(&command_buf, NULL);
19621962
for (;;) {
1963-
if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
1963+
if (strbuf_getline_lf(&command_buf, stdin) == EOF)
19641964
die("EOF in data (terminator '%s' not found)", term);
19651965
if (term_len == command_buf.len
19661966
&& !strcmp(term, command_buf.buf))

ident.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static int add_mailname_host(struct strbuf *buf)
7676
strerror(errno));
7777
return -1;
7878
}
79-
if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
79+
if (strbuf_getline_lf(&mailnamebuf, mailname) == EOF) {
8080
if (ferror(mailname))
8181
warning("cannot read /etc/mailname: %s",
8282
strerror(errno));

mailinfo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
732732
struct strbuf continuation = STRBUF_INIT;
733733

734734
/* Get the first part of the line. */
735-
if (strbuf_getline(line, in, '\n'))
735+
if (strbuf_getline_lf(line, in))
736736
return 0;
737737

738738
/*
@@ -756,7 +756,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
756756
peek = fgetc(in); ungetc(peek, in);
757757
if (peek != ' ' && peek != '\t')
758758
break;
759-
if (strbuf_getline(&continuation, in, '\n'))
759+
if (strbuf_getline_lf(&continuation, in))
760760
break;
761761
continuation.buf[0] = ' ';
762762
strbuf_rtrim(&continuation);
@@ -769,7 +769,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
769769

770770
static int find_boundary(struct mailinfo *mi, struct strbuf *line)
771771
{
772-
while (!strbuf_getline(line, mi->input, '\n')) {
772+
while (!strbuf_getline_lf(line, mi->input)) {
773773
if (*(mi->content_top) && is_multipart_boundary(mi, line))
774774
return 1;
775775
}
@@ -820,7 +820,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
820820

821821
strbuf_release(&newline);
822822
/* replenish line */
823-
if (strbuf_getline(line, mi->input, '\n'))
823+
if (strbuf_getline_lf(line, mi->input))
824824
return 0;
825825
strbuf_addch(line, '\n');
826826
return 1;

remote-curl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ static void parse_fetch(struct strbuf *buf)
827827
die("http transport does not support %s", buf->buf);
828828

829829
strbuf_reset(buf);
830-
if (strbuf_getline(buf, stdin, '\n') == EOF)
830+
if (strbuf_getline_lf(buf, stdin) == EOF)
831831
return;
832832
if (!*buf->buf)
833833
break;
@@ -940,7 +940,7 @@ static void parse_push(struct strbuf *buf)
940940
die("http transport does not support %s", buf->buf);
941941

942942
strbuf_reset(buf);
943-
if (strbuf_getline(buf, stdin, '\n') == EOF)
943+
if (strbuf_getline_lf(buf, stdin) == EOF)
944944
goto free_specs;
945945
if (!*buf->buf)
946946
break;
@@ -990,7 +990,7 @@ int main(int argc, const char **argv)
990990
do {
991991
const char *arg;
992992

993-
if (strbuf_getline(&buf, stdin, '\n') == EOF) {
993+
if (strbuf_getline_lf(&buf, stdin) == EOF) {
994994
if (ferror(stdin))
995995
error("remote-curl: error reading command stream from git");
996996
return 1;

0 commit comments

Comments
 (0)