Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jan 15, 2016
1 parent c8aa9fd commit 8f309ae
Show file tree
Hide file tree
Showing 36 changed files with 81 additions and 59 deletions.
8 changes: 4 additions & 4 deletions bisect.c
Expand Up @@ -440,7 +440,7 @@ static void read_bisect_paths(struct argv_array *array)
if (!fp)
die_errno("Could not open file '%s'", filename);

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

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

strbuf_release(&str);
Expand Down Expand Up @@ -914,9 +914,9 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
strerror(errno));
}
} else {
strbuf_getline(&str, fp, '\n');
strbuf_getline_lf(&str, fp);
*read_bad = strbuf_detach(&str, NULL);
strbuf_getline(&str, fp, '\n');
strbuf_getline_lf(&str, fp);
*read_good = strbuf_detach(&str, NULL);
}
strbuf_release(&str);
Expand Down
14 changes: 7 additions & 7 deletions builtin/am.c
Expand Up @@ -269,7 +269,7 @@ static char *read_shell_var(FILE *fp, const char *key)
struct strbuf sb = STRBUF_INIT;
const char *str;

if (strbuf_getline(&sb, fp, '\n'))
if (strbuf_getline_lf(&sb, fp))
goto fail;

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

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

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

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

while (!strbuf_getline(&sb, in, '\n')) {
while (!strbuf_getline_lf(&sb, in)) {
const char *str;

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

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

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

while (!strbuf_getline(&sb, in, '\n')) {
while (!strbuf_getline_lf(&sb, in)) {
const char *str;

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

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

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

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

if (!skip_prefix(sb.buf, "From ", &x))
Expand Down
2 changes: 1 addition & 1 deletion builtin/cat-file.c
Expand Up @@ -401,7 +401,7 @@ static int batch_objects(struct batch_options *opt)
save_warning = warn_on_object_refname_ambiguity;
warn_on_object_refname_ambiguity = 0;

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
if (data.split_on_whitespace) {
/*
* Split at first whitespace, tying off the beginning
Expand Down
2 changes: 1 addition & 1 deletion builtin/check-mailmap.c
Expand Up @@ -54,7 +54,7 @@ int cmd_check_mailmap(int argc, const char **argv, const char *prefix)

if (use_stdin) {
struct strbuf buf = STRBUF_INIT;
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
check_mailmap(&mailmap, buf.buf);
maybe_flush_or_die(stdout, "stdout");
}
Expand Down
6 changes: 3 additions & 3 deletions builtin/clean.c
Expand Up @@ -594,7 +594,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
clean_get_color(CLEAN_COLOR_RESET));
}

if (strbuf_getline(&choice, stdin, '\n') != EOF) {
if (strbuf_getline_lf(&choice, stdin) != EOF) {
strbuf_trim(&choice);
} else {
eof = 1;
Expand Down Expand Up @@ -676,7 +676,7 @@ static int filter_by_patterns_cmd(void)
clean_print_color(CLEAN_COLOR_PROMPT);
printf(_("Input ignore patterns>> "));
clean_print_color(CLEAN_COLOR_RESET);
if (strbuf_getline(&confirm, stdin, '\n') != EOF)
if (strbuf_getline_lf(&confirm, stdin) != EOF)
strbuf_trim(&confirm);
else
putchar('\n');
Expand Down Expand Up @@ -774,7 +774,7 @@ static int ask_each_cmd(void)
qname = quote_path_relative(item->string, NULL, &buf);
/* TRANSLATORS: Make sure to keep [y/N] as is */
printf(_("Remove %s [y/N]? "), qname);
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
if (strbuf_getline_lf(&confirm, stdin) != EOF) {
strbuf_trim(&confirm);
} else {
putchar('\n');
Expand Down
2 changes: 1 addition & 1 deletion builtin/clone.c
Expand Up @@ -339,7 +339,7 @@ static void copy_alternates(struct strbuf *src, struct strbuf *dst,
FILE *in = fopen(src->buf, "r");
struct strbuf line = STRBUF_INIT;

while (strbuf_getline(&line, in, '\n') != EOF) {
while (strbuf_getline_lf(&line, in) != EOF) {
char *abs_path;
if (!line.len || line.buf[0] == '#')
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/column.c
Expand Up @@ -51,7 +51,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
die(_("--command must be the first argument"));
}
finalize_colopts(&colopts, -1);
while (!strbuf_getline(&sb, stdin, '\n'))
while (!strbuf_getline_lf(&sb, stdin))
string_list_append(&list, sb.buf);

print_columns(&list, colopts, &copts);
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Expand Up @@ -1690,7 +1690,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (fp == NULL)
die_errno(_("could not open '%s' for reading"),
git_path_merge_head());
while (strbuf_getline(&m, fp, '\n') != EOF) {
while (strbuf_getline_lf(&m, fp) != EOF) {
struct commit *parent;

parent = get_merge_parent(m.buf);
Expand Down
2 changes: 1 addition & 1 deletion builtin/fetch-pack.c
Expand Up @@ -158,7 +158,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
else {
/* read from stdin one ref per line, until EOF */
struct strbuf line = STRBUF_INIT;
while (strbuf_getline(&line, stdin, '\n') != EOF)
while (strbuf_getline_lf(&line, stdin) != EOF)
add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
strbuf_release(&line);
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/grep.c
Expand Up @@ -562,7 +562,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
patterns = from_stdin ? stdin : fopen(arg, "r");
if (!patterns)
die_errno(_("cannot open '%s'"), arg);
while (strbuf_getline(&sb, patterns, '\n') == 0) {
while (strbuf_getline_lf(&sb, patterns) == 0) {
/* ignore empty line like grep does */
if (sb.len == 0)
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/hash-object.c
Expand Up @@ -60,7 +60,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
{
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
if (buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
Expand Down
2 changes: 1 addition & 1 deletion builtin/notes.c
Expand Up @@ -290,7 +290,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
t = &default_notes_tree;
}

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
unsigned char from_obj[20], to_obj[20];
struct strbuf **split;
int err;
Expand Down
2 changes: 1 addition & 1 deletion builtin/pull.c
Expand Up @@ -378,7 +378,7 @@ static void get_merge_heads(struct sha1_array *merge_heads)

if (!(fp = fopen(filename, "r")))
die_errno(_("could not open '%s' for reading"), filename);
while (strbuf_getline(&sb, fp, '\n') != EOF) {
while (strbuf_getline_lf(&sb, fp) != EOF) {
if (get_sha1_hex(sb.buf, sha1))
continue; /* invalid line: does not start with SHA1 */
if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
Expand Down
2 changes: 1 addition & 1 deletion builtin/repack.c
Expand Up @@ -266,7 +266,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
return ret;

out = xfdopen(cmd.out, "r");
while (strbuf_getline(&line, out, '\n') != EOF) {
while (strbuf_getline_lf(&line, out) != EOF) {
if (line.len != 40)
die("repack: Expecting 40 character sha1 lines only from pack-objects.");
string_list_append(&names, line.buf);
Expand Down
4 changes: 2 additions & 2 deletions builtin/rev-parse.c
Expand Up @@ -383,7 +383,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)

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

/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
while (strbuf_getline(&sb, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&sb, stdin) != EOF) {
const char *s;
const char *help;
struct option *o;
Expand Down
2 changes: 1 addition & 1 deletion builtin/send-pack.c
Expand Up @@ -212,7 +212,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
argv_array_push(&all_refspecs, buf);
} else {
struct strbuf line = STRBUF_INIT;
while (strbuf_getline(&line, stdin, '\n') != EOF)
while (strbuf_getline_lf(&line, stdin) != EOF)
argv_array_push(&all_refspecs, line.buf);
strbuf_release(&line);
}
Expand Down
2 changes: 1 addition & 1 deletion compat/terminal.c
Expand Up @@ -122,7 +122,7 @@ char *git_terminal_prompt(const char *prompt, int echo)
fputs(prompt, output_fh);
fflush(output_fh);

r = strbuf_getline(&buf, input_fh, '\n');
r = strbuf_getline_lf(&buf, input_fh);
if (!echo) {
putc('\n', output_fh);
fflush(output_fh);
Expand Down
4 changes: 2 additions & 2 deletions credential-cache--daemon.c
Expand Up @@ -96,12 +96,12 @@ static int read_request(FILE *fh, struct credential *c,
static struct strbuf item = STRBUF_INIT;
const char *p;

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

strbuf_getline(&item, fh, '\n');
strbuf_getline_lf(&item, fh);
if (!skip_prefix(item.buf, "timeout=", &p))
return error("client sent bogus timeout line: %s", item.buf);
*timeout = atoi(p);
Expand Down
2 changes: 1 addition & 1 deletion credential-store.c
Expand Up @@ -23,7 +23,7 @@ static int parse_credential_file(const char *fn,
return found_credential;
}

while (strbuf_getline(&line, fh, '\n') != EOF) {
while (strbuf_getline_lf(&line, fh) != EOF) {
credential_from_url(&entry, line.buf);
if (entry.username && entry.password &&
credential_match(c, &entry)) {
Expand Down
2 changes: 1 addition & 1 deletion credential.c
Expand Up @@ -142,7 +142,7 @@ int credential_read(struct credential *c, FILE *fp)
{
struct strbuf line = STRBUF_INIT;

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

Expand Down
2 changes: 1 addition & 1 deletion daemon.c
Expand Up @@ -424,7 +424,7 @@ static void copy_to_log(int fd)
return;
}

while (strbuf_getline(&line, fp, '\n') != EOF) {
while (strbuf_getline_lf(&line, fp) != EOF) {
logerror("%s", line.buf);
strbuf_setlen(&line, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions fast-import.c
Expand Up @@ -1888,7 +1888,7 @@ static int read_next_command(void)
struct recent_command *rc;

strbuf_detach(&command_buf, NULL);
stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
stdin_eof = strbuf_getline_lf(&command_buf, stdin);
if (stdin_eof)
return EOF;

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

strbuf_detach(&command_buf, NULL);
for (;;) {
if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
if (strbuf_getline_lf(&command_buf, stdin) == EOF)
die("EOF in data (terminator '%s' not found)", term);
if (term_len == command_buf.len
&& !strcmp(term, command_buf.buf))
Expand Down
2 changes: 1 addition & 1 deletion ident.c
Expand Up @@ -76,7 +76,7 @@ static int add_mailname_host(struct strbuf *buf)
strerror(errno));
return -1;
}
if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
if (strbuf_getline_lf(&mailnamebuf, mailname) == EOF) {
if (ferror(mailname))
warning("cannot read /etc/mailname: %s",
strerror(errno));
Expand Down
8 changes: 4 additions & 4 deletions mailinfo.c
Expand Up @@ -732,7 +732,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
struct strbuf continuation = STRBUF_INIT;

/* Get the first part of the line. */
if (strbuf_getline(line, in, '\n'))
if (strbuf_getline_lf(line, in))
return 0;

/*
Expand All @@ -756,7 +756,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
peek = fgetc(in); ungetc(peek, in);
if (peek != ' ' && peek != '\t')
break;
if (strbuf_getline(&continuation, in, '\n'))
if (strbuf_getline_lf(&continuation, in))
break;
continuation.buf[0] = ' ';
strbuf_rtrim(&continuation);
Expand All @@ -769,7 +769,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)

static int find_boundary(struct mailinfo *mi, struct strbuf *line)
{
while (!strbuf_getline(line, mi->input, '\n')) {
while (!strbuf_getline_lf(line, mi->input)) {
if (*(mi->content_top) && is_multipart_boundary(mi, line))
return 1;
}
Expand Down Expand Up @@ -820,7 +820,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)

strbuf_release(&newline);
/* replenish line */
if (strbuf_getline(line, mi->input, '\n'))
if (strbuf_getline_lf(line, mi->input))
return 0;
strbuf_addch(line, '\n');
return 1;
Expand Down
6 changes: 3 additions & 3 deletions remote-curl.c
Expand Up @@ -827,7 +827,7 @@ static void parse_fetch(struct strbuf *buf)
die("http transport does not support %s", buf->buf);

strbuf_reset(buf);
if (strbuf_getline(buf, stdin, '\n') == EOF)
if (strbuf_getline_lf(buf, stdin) == EOF)
return;
if (!*buf->buf)
break;
Expand Down Expand Up @@ -940,7 +940,7 @@ static void parse_push(struct strbuf *buf)
die("http transport does not support %s", buf->buf);

strbuf_reset(buf);
if (strbuf_getline(buf, stdin, '\n') == EOF)
if (strbuf_getline_lf(buf, stdin) == EOF)
goto free_specs;
if (!*buf->buf)
break;
Expand Down Expand Up @@ -990,7 +990,7 @@ int main(int argc, const char **argv)
do {
const char *arg;

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

0 comments on commit 8f309ae

Please sign in to comment.