Re: [PATCH v3 14/25] sequencer: introduce a helper to read files written by scripts
- From
Johannes Schindelin <johannes.schindelin@gmx.de>
- Date
- Oct 18, 2016, 11:42 UTC
- Message-ID
- <alpine.DEB.2.20.1610181339550.197091@virtualbox>
- In-Reply-To
- <xmqqr37ex5ck.fsf@gitster.mtv.corp.google.com>
Hi Junio,
On Mon, 17 Oct 2016, Junio C Hamano wrote:
Show 23 quoted lines
> Johannes Schindelin <johannes.schindelin@gmx.de> writes:
>
> > +/*
> > + * Reads a file that was presumably written by a shell script, i.e.
> > + * with an end-of-line marker that needs to be stripped.
> > + *
> > + * Returns 1 if the file was read, 0 if it could not be read or does not exist.
> > + */
> > +static int read_oneliner(struct strbuf *buf,
> > + const char *path, int skip_if_empty)
> > +...
> > + if (strbuf_read_file(buf, path, 0) < 0) {
> > + warning_errno(_("could not read '%s'"), path);
> > + return 0;
> > + }
> > + if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') {
> > + if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
> > + --buf->len;
> > + buf->buf[buf->len] = '\0';
> > + }
>
> The name says "oneliner" but this reads the whole thing and trims
> only the last line of the input. Which is correct?The latter. Basically, `read_oneliner()` is short-hand for "that thing that shell does when you use `cat file` with backticks.
I do not like `read_stripping_last_eol()`, `read_what_the_shell_wrote()` nor `read_skipping_last_lf()`. So if you come up with any brilliant idea, I am all ears.
In the meantime, I'd be happy to just add a comment that this function is intended for oneliners, but that it will also read multi-line files and only strip off the EOL marker from the last line.
Would that work for you? Dscho