From c0353c78e80434b9385e134c69b54b048c382077 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:56:23 -0700 Subject: hash-object: read --stdin-paths with strbuf_getline() The list of paths could have been written with a DOS editor. Signed-off-by: Junio C Hamano diff --git a/builtin/hash-object.c b/builtin/hash-object.c index 3bc5ec1..ff20395 100644 --- a/builtin/hash-object.c +++ b/builtin/hash-object.c @@ -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_lf(&buf, stdin) != EOF) { + while (strbuf_getline(&buf, stdin) != EOF) { if (buf.buf[0] == '"') { strbuf_reset(&nbuf); if (unquote_c_style(&nbuf, buf.buf, NULL)) -- cgit v0.10.2-6-g49f6 From 6e8d46f9d4bb3643b8fdf4e1b0856f7910dbc948 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 14:05:53 -0700 Subject: revision: read --stdin with strbuf_getline() Reading with getwholeline() and manually stripping the terminating '\n' would leave CR at the end of the line if the input comes from a DOS editor. Constrasting this with the other changes around "--stdin" in this series, one may realize that the way "log" family of commands read the paths with "--stdin" looks inconsistent and sloppy. It does not allow us to C-quote a textual input, neither does it accept records that are NUL-terminated. These are unfortunately way too late to fix X-<. Signed-off-by: Junio C Hamano diff --git a/revision.c b/revision.c index 0a282f5..8df4e11 100644 --- a/revision.c +++ b/revision.c @@ -1635,10 +1635,7 @@ static void append_prune_data(struct cmdline_pathspec *prune, const char **av) static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, struct cmdline_pathspec *prune) { - while (strbuf_getwholeline(sb, stdin, '\n') != EOF) { - int len = sb->len; - if (len && sb->buf[len - 1] == '\n') - sb->buf[--len] = '\0'; + while (strbuf_getline(sb, stdin) != EOF) { ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc); prune->path[prune->nr++] = xstrdup(sb->buf); } @@ -1655,10 +1652,8 @@ static void read_revisions_from_stdin(struct rev_info *revs, warn_on_object_refname_ambiguity = 0; strbuf_init(&sb, 1000); - while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) { + while (strbuf_getline(&sb, stdin) != EOF) { int len = sb.len; - if (len && sb.buf[len - 1] == '\n') - sb.buf[--len] = '\0'; if (!len) break; if (sb.buf[0] == '-') { -- cgit v0.10.2-6-g49f6 From 72e37b6ac851c3926956c9d11a40260f08bf1c5e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:59:44 -0700 Subject: rev-parse: read parseopt spec with strbuf_getline() "rev-parse --parseopt" specification is clearly text and we should anticipate that we may be fed CRLF lines. Signed-off-by: Junio C Hamano diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 0324abb..bd16876 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -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_lf(&sb, stdin) == EOF) + if (strbuf_getline(&sb, stdin) == EOF) die("premature end of input"); ALLOC_GROW(usage, unb + 1, usz); if (!strcmp("--", sb.buf)) { @@ -396,7 +396,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) } /* parse: (|,|)[*=?!]*? SP+ */ - while (strbuf_getline_lf(&sb, stdin) != EOF) { + while (strbuf_getline(&sb, stdin) != EOF) { const char *s; const char *help; struct option *o; -- cgit v0.10.2-6-g49f6 From 1f3b1efd18a935fed41431132c67cde5a94833ae Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:24:41 -0700 Subject: ident.c: read /etc/mailname with strbuf_getline() Just in case /etc/mailname file was edited with a DOS editor, read it with strbuf_getline() so that a stray CR is not included as the last character of the mail hostname. We _might_ want to more aggressively discard whitespace characters around the line with strbuf_trim(), but that is a bit outside the scope of this series. Signed-off-by: Junio C Hamano diff --git a/ident.c b/ident.c index 9dd3ae3..3da5556 100644 --- a/ident.c +++ b/ident.c @@ -76,7 +76,7 @@ static int add_mailname_host(struct strbuf *buf) strerror(errno)); return -1; } - if (strbuf_getline_lf(&mailnamebuf, mailname) == EOF) { + if (strbuf_getline(&mailnamebuf, mailname) == EOF) { if (ferror(mailname)) warning("cannot read /etc/mailname: %s", strerror(errno)); -- cgit v0.10.2-6-g49f6 From 18814d0e2d7d5924a799bcf0cae3a0aaba569613 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:27:33 -0700 Subject: remote.c: read $GIT_DIR/remotes/* with strbuf_getline() These files can be edited with a DOS editor, leaving CR at the end of the line if read with strbuf_getline(). Signed-off-by: Junio C Hamano diff --git a/remote.c b/remote.c index f195693..7d61dab 100644 --- a/remote.c +++ b/remote.c @@ -256,7 +256,7 @@ static void read_remotes_file(struct remote *remote) if (!f) return; remote->origin = REMOTE_REMOTES; - while (strbuf_getline_lf(&buf, f) != EOF) { + while (strbuf_getline(&buf, f) != EOF) { const char *v; strbuf_rtrim(&buf); -- cgit v0.10.2-6-g49f6 From 3f163962282d2d8bea914c32d81ad38544044f78 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:29:24 -0700 Subject: clone/sha1_file: read info/alternates with strbuf_getline() $GIT_OBJECT_DIRECTORY/info/alternates is a text file that can be edited with a DOS editor. We do not want to use the real path with CR appended at the end. Signed-off-by: Junio C Hamano diff --git a/builtin/clone.c b/builtin/clone.c index 29741f4..43b4c99 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -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_lf(&line, in) != EOF) { + while (strbuf_getline(&line, in) != EOF) { char *abs_path; if (!line.len || line.buf[0] == '#') continue; diff --git a/sha1_file.c b/sha1_file.c index 86b5e8c..aab1872 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -396,7 +396,7 @@ void add_to_alternates_file(const char *reference) struct strbuf line = STRBUF_INIT; int found = 0; - while (strbuf_getline_lf(&line, in) != EOF) { + while (strbuf_getline(&line, in) != EOF) { if (!strcmp(reference, line.buf)) { found = 1; break; -- cgit v0.10.2-6-g49f6 From 692dfdfa622c6286999609a4fef59724124ca794 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:36:00 -0700 Subject: transport-helper: read helper response with strbuf_getline() Our implementation of helpers never use CRLF line endings, and they do not depend on the ability to place a CR as payload at the end of the line, so this is essentially a no-op for in-tree users. However, this allows third-party implementation of helpers to give us their line with CRLF line ending (they cannot expect us to feed CRLF to them, though). Signed-off-by: Junio C Hamano diff --git a/transport-helper.c b/transport-helper.c index e45d88f..a6bff8b 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -54,7 +54,7 @@ static int recvline_fh(FILE *helper, struct strbuf *buffer, const char *name) strbuf_reset(buffer); if (debug) fprintf(stderr, "Debug: Remote helper: Waiting...\n"); - if (strbuf_getline_lf(buffer, helper) == EOF) { + if (strbuf_getline(buffer, helper) == EOF) { if (debug) fprintf(stderr, "Debug: Remote helper quit.\n"); return 1; -- cgit v0.10.2-6-g49f6 From b42ca3dd0f157d0c23c9a034bc68257e1748238a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:38:56 -0700 Subject: cat-file: read batch stream with strbuf_getline() It is possible to prepare a text file with a DOS editor and feed it as a batch command stream to the command. Signed-off-by: Junio C Hamano diff --git a/builtin/cat-file.c b/builtin/cat-file.c index d2ebaf1..54db118 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -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_lf(&buf, stdin) != EOF) { + while (strbuf_getline(&buf, stdin) != EOF) { if (data.split_on_whitespace) { /* * Split at first whitespace, tying off the beginning -- cgit v0.10.2-6-g49f6 From 1536dd9c1df0b7167b139f6666080cc4774ef63f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:52:11 -0700 Subject: column: read lines with strbuf_getline() Multiple lines read here are concatenated on a single line to form a multi-column output line. We do not want to have a CR at the end, even if the input file consists of CRLF terminated lines. Signed-off-by: Junio C Hamano diff --git a/builtin/column.c b/builtin/column.c index 40eab08..33314b4 100644 --- a/builtin/column.c +++ b/builtin/column.c @@ -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_lf(&sb, stdin)) + while (!strbuf_getline(&sb, stdin)) string_list_append(&list, sb.buf); print_columns(&list, colopts, &copts); -- cgit v0.10.2-6-g49f6 From 933bea922c202a7ff9b133a5cca4a805a7013aa2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 14:00:59 -0700 Subject: send-pack: read list of refs with strbuf_getline() Signed-off-by: Junio C Hamano diff --git a/builtin/send-pack.c b/builtin/send-pack.c index 8f9f4f1..5b9dd6a 100644 --- a/builtin/send-pack.c +++ b/builtin/send-pack.c @@ -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_lf(&line, stdin) != EOF) + while (strbuf_getline(&line, stdin) != EOF) argv_array_push(&all_refspecs, line.buf); strbuf_release(&line); } -- cgit v0.10.2-6-g49f6 From a551843129dc4d329d4f7915a9c10120963acd7d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:53:47 -0700 Subject: grep: read -f file with strbuf_getline() List of patterns file could come from a DOS editor. This is iffy; you may actually be trying to find a line with ^M in it on a system whose line ending is LF. You can of course work it around by having a line that has "^M^M^J", let the strbuf_getline() eat the last "^M^J", leaving just the single "^M" as the pattern. Signed-off-by: Junio C Hamano diff --git a/builtin/grep.c b/builtin/grep.c index 5a5beb8..801ce60 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -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_lf(&sb, patterns) == 0) { + while (strbuf_getline(&sb, patterns) == 0) { /* ignore empty line like grep does */ if (sb.len == 0) continue; -- cgit v0.10.2-6-g49f6 From f06068c96181f6212e15e32957a3d40c7c7bd43d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:32:10 -0700 Subject: test-sha1-array: read command stream with strbuf_getline() The input to this command comes from a pipeline in t0064, whose upstream has bunch of "echo"s. It is not unreasonable to expect that it may be fed CRLF lines on DOSsy systems. Signed-off-by: Junio C Hamano diff --git a/test-sha1-array.c b/test-sha1-array.c index 700f3f3..60ea1d5 100644 --- a/test-sha1-array.c +++ b/test-sha1-array.c @@ -11,7 +11,7 @@ int main(int argc, char **argv) struct sha1_array array = SHA1_ARRAY_INIT; struct strbuf line = STRBUF_INIT; - while (strbuf_getline_lf(&line, stdin) != EOF) { + while (strbuf_getline(&line, stdin) != EOF) { const char *arg; unsigned char sha1[20]; -- cgit v0.10.2-6-g49f6