From c8aa9fdf5dc15e2c508acb22df03d431983569ed Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:17:29 -0700 Subject: strbuf: make strbuf_getline_crlf() global Often we read "text" files that are supplied by the end user (e.g. commit log message that was edited with $GIT_EDITOR upon 'git commit -e'), and in some environments lines in a text file are terminated with CRLF. Existing strbuf_getline() knows to read a single line and then strip the terminating byte from the result, but it is handy to have a version that is more tailored for a "text" input that takes both '\n' and '\r\n' as line terminator (aka in POSIX lingo) and returns the body of the line after stripping . Recently reimplemented "git am" uses such a function implemented privately; move it to strbuf.[ch] and make it available for others. Note that we do not blindly replace calls to strbuf_getline() that uses LF as the line terminator with calls to strbuf_getline_crlf() and this is very much deliberate. Some callers may want to treat an incoming line that ends with CR (and terminated with LF) to have a payload that includes the final CR, and such a blind replacement will result in misconversion when done without code audit. Signed-off-by: Junio C Hamano --- strbuf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index b165d044a..7ad5ea414 100644 --- a/strbuf.c +++ b/strbuf.c @@ -510,6 +510,18 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term) return 0; } +int strbuf_getline_crlf(struct strbuf *sb, FILE *fp) +{ + if (strbuf_getwholeline(sb, fp, '\n')) + return EOF; + if (sb->buf[sb->len - 1] == '\n') { + strbuf_setlen(sb, sb->len - 1); + if (sb->len && sb->buf[sb->len - 1] == '\r') + strbuf_setlen(sb, sb->len - 1); + } + return 0; +} + int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term) { strbuf_reset(sb); -- cgit v1.2.1