Skip to content

Commit bd021f3

Browse files
pks-tgitster
authored andcommitted
strbuf: provide function to append whole lines
While the strbuf interface already provides functions to read a line into it that completely replaces its current contents, we do not have an interface that allows for appending lines without discarding current contents. Add a new function `strbuf_appendwholeline` that reads a line including its terminating character into a strbuf non-destructively. This is a preparatory step for git-update-ref(1) reading standard input line-wise instead of as a block. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent faa35ee commit bd021f3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

strbuf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,16 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
690690
}
691691
#endif
692692

693+
int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
694+
{
695+
struct strbuf line = STRBUF_INIT;
696+
if (strbuf_getwholeline(&line, fp, term))
697+
return EOF;
698+
strbuf_addbuf(sb, &line);
699+
strbuf_release(&line);
700+
return 0;
701+
}
702+
693703
static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
694704
{
695705
if (strbuf_getwholeline(sb, fp, term))

strbuf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ int strbuf_getline(struct strbuf *sb, FILE *file);
502502
*/
503503
int strbuf_getwholeline(struct strbuf *sb, FILE *file, int term);
504504

505+
/**
506+
* Like `strbuf_getwholeline`, but appends the line instead of
507+
* resetting the buffer first.
508+
*/
509+
int strbuf_appendwholeline(struct strbuf *sb, FILE *file, int term);
510+
505511
/**
506512
* Like `strbuf_getwholeline`, but operates on a file descriptor.
507513
* It reads one character at a time, so it is very slow. Do not

0 commit comments

Comments
 (0)