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;

0 commit comments

Comments
 (0)