Skip to content

Commit b62624b

Browse files
committed
Merge branch 'jc/strbuf-getline'
The preliminary clean-up for jc/peace-with-crlf topic. * jc/strbuf-getline: strbuf: give strbuf_getline() to the "most text friendly" variant checkout-index: there are only two possible line terminations update-index: there are only two possible line terminations check-ignore: there are only two possible line terminations check-attr: there are only two possible line terminations mktree: there are only two possible line terminations strbuf: introduce strbuf_getline_{lf,nul}() strbuf: make strbuf_getline_crlf() global strbuf: miniscule style fix
2 parents 116a866 + 1a0c8df commit b62624b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+150
-116
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: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ static int is_empty_file(const char *filename)
4545
return !st.st_size;
4646
}
4747

48-
/**
49-
* Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
50-
*/
51-
static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
52-
{
53-
if (strbuf_getwholeline(sb, fp, '\n'))
54-
return EOF;
55-
if (sb->buf[sb->len - 1] == '\n') {
56-
strbuf_setlen(sb, sb->len - 1);
57-
if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
58-
strbuf_setlen(sb, sb->len - 1);
59-
}
60-
return 0;
61-
}
62-
6348
/**
6449
* Returns the length of the first line of msg.
6550
*/
@@ -284,7 +269,7 @@ static char *read_shell_var(FILE *fp, const char *key)
284269
struct strbuf sb = STRBUF_INIT;
285270
const char *str;
286271

287-
if (strbuf_getline(&sb, fp, '\n'))
272+
if (strbuf_getline_lf(&sb, fp))
288273
goto fail;
289274

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

574559
fp = xfopen(am_path(state, "rewritten"), "r");
575560

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

579564
if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
@@ -628,7 +613,7 @@ static int is_mail(FILE *fp)
628613
if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
629614
die("invalid pattern: %s", header_regex);
630615

631-
while (!strbuf_getline_crlf(&sb, fp)) {
616+
while (!strbuf_getline(&sb, fp)) {
632617
if (!sb.len)
633618
break; /* End of header */
634619

@@ -675,7 +660,7 @@ static int detect_patch_format(const char **paths)
675660

676661
fp = xfopen(*paths, "r");
677662

678-
while (!strbuf_getline_crlf(&l1, fp)) {
663+
while (!strbuf_getline(&l1, fp)) {
679664
if (l1.len)
680665
break;
681666
}
@@ -696,9 +681,9 @@ static int detect_patch_format(const char **paths)
696681
}
697682

698683
strbuf_reset(&l2);
699-
strbuf_getline_crlf(&l2, fp);
684+
strbuf_getline(&l2, fp);
700685
strbuf_reset(&l3);
701-
strbuf_getline_crlf(&l3, fp);
686+
strbuf_getline(&l3, fp);
702687

703688
/*
704689
* If the second line is empty and the third is a From, Author or Date
@@ -817,7 +802,7 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
817802
struct strbuf sb = STRBUF_INIT;
818803
int subject_printed = 0;
819804

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

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

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

@@ -900,7 +885,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
900885
{
901886
struct strbuf sb = STRBUF_INIT;
902887

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

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

13181303
/* Extract message and author information */
13191304
fp = xfopen(am_path(state, "info"), "r");
1320-
while (!strbuf_getline(&sb, fp, '\n')) {
1305+
while (!strbuf_getline_lf(&sb, fp)) {
13211306
const char *x;
13221307

13231308
if (skip_prefix(sb.buf, "Subject: ", &x)) {
@@ -1383,7 +1368,7 @@ static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
13831368
FILE *fp = xfopen(mail, "r");
13841369
const char *x;
13851370

1386-
if (strbuf_getline(&sb, fp, '\n'))
1371+
if (strbuf_getline_lf(&sb, fp))
13871372
return -1;
13881373

13891374
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-attr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ static void check_attr_stdin_paths(const char *prefix, int cnt,
7373
struct git_attr_check *check)
7474
{
7575
struct strbuf buf, nbuf;
76-
int line_termination = nul_term_line ? 0 : '\n';
76+
strbuf_getline_fn getline_fn;
7777

78+
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
7879
strbuf_init(&buf, 0);
7980
strbuf_init(&nbuf, 0);
80-
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
81-
if (line_termination && buf.buf[0] == '"') {
81+
while (getline_fn(&buf, stdin) != EOF) {
82+
if (!nul_term_line && buf.buf[0] == '"') {
8283
strbuf_reset(&nbuf);
8384
if (unquote_c_style(&nbuf, buf.buf, NULL))
8485
die("line is badly quoted");

builtin/check-ignore.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
117117
{
118118
struct strbuf buf, nbuf;
119119
char *pathspec[2] = { NULL, NULL };
120-
int line_termination = nul_term_line ? 0 : '\n';
120+
strbuf_getline_fn getline_fn;
121121
int num_ignored = 0;
122122

123+
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
123124
strbuf_init(&buf, 0);
124125
strbuf_init(&nbuf, 0);
125-
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
126-
if (line_termination && buf.buf[0] == '"') {
126+
while (getline_fn(&buf, stdin) != EOF) {
127+
if (!nul_term_line && buf.buf[0] == '"') {
127128
strbuf_reset(&nbuf);
128129
if (unquote_c_style(&nbuf, buf.buf, NULL))
129130
die("line is badly quoted");

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/checkout-index.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "parse-options.h"
1212

1313
#define CHECKOUT_ALL 4
14-
static int line_termination = '\n';
14+
static int nul_term_line;
1515
static int checkout_stage; /* default to checkout stage0 */
1616
static int to_tempfile;
1717
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
@@ -35,7 +35,8 @@ static void write_tempfile_record(const char *name, const char *prefix)
3535
fputs(topath[checkout_stage], stdout);
3636

3737
putchar('\t');
38-
write_name_quoted_relative(name, prefix, stdout, line_termination);
38+
write_name_quoted_relative(name, prefix, stdout,
39+
nul_term_line ? '\0' : '\n');
3940

4041
for (i = 0; i < 4; i++) {
4142
topath[i][0] = 0;
@@ -144,10 +145,7 @@ static int option_parse_u(const struct option *opt,
144145
static int option_parse_z(const struct option *opt,
145146
const char *arg, int unset)
146147
{
147-
if (unset)
148-
line_termination = '\n';
149-
else
150-
line_termination = 0;
148+
nul_term_line = !unset;
151149
return 0;
152150
}
153151

@@ -254,13 +252,15 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
254252

255253
if (read_from_stdin) {
256254
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
255+
strbuf_getline_fn getline_fn;
257256

258257
if (all)
259258
die("git checkout-index: don't mix '--all' and '--stdin'");
260259

261-
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
260+
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
261+
while (getline_fn(&buf, stdin) != EOF) {
262262
char *p;
263-
if (line_termination && buf.buf[0] == '"') {
263+
if (!nul_term_line && buf.buf[0] == '"') {
264264
strbuf_reset(&nbuf);
265265
if (unquote_c_style(&nbuf, buf.buf, NULL))
266266
die("line is badly quoted");

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);

0 commit comments

Comments
 (0)