Skip to content

Commit e6c019d

Browse files
MadCodergitster
authored andcommitted
Drop strbuf's 'eof' marker, and make read_line a first class citizen.
read_line is now strbuf_getline, and is a first class citizen, it returns 0 when reading a line worked, EOF else. The ->eof marker was used non-locally by fast-import.c, mimic the same behaviour using a static int in "read_next_command", that now returns -1 on EOF, and avoids to call strbuf_getline when it's in EOF state. Also no longer automagically strbuf_release the buffer, it's counter intuitive and breaks fast-import in a very subtle way. Note: being at EOF implies that command_buf.len == 0. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8b6087f commit e6c019d

File tree

7 files changed

+39
-41
lines changed

7 files changed

+39
-41
lines changed

builtin-checkout-index.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
277277
while (1) {
278278
char *path_name;
279279
const char *p;
280-
281-
read_line(&buf, stdin, line_termination);
282-
if (buf.eof)
280+
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
283281
break;
284282
if (line_termination && buf.buf[0] == '"')
285283
path_name = unquote_c_style(buf.buf, NULL);
@@ -292,6 +290,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
292290
if (path_name != buf.buf)
293291
free(path_name);
294292
}
293+
strbuf_release(&buf);
295294
}
296295

297296
if (all)

builtin-update-index.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ static void read_index_info(int line_termination)
327327
* This format is to put higher order stages into the
328328
* index file and matches git-ls-files --stage output.
329329
*/
330-
read_line(&buf, stdin, line_termination);
331-
if (buf.eof)
330+
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
332331
break;
333332

334333
errno = 0;
@@ -391,6 +390,7 @@ static void read_index_info(int line_termination)
391390
bad_line:
392391
die("malformed index info %s", buf.buf);
393392
}
393+
strbuf_release(&buf);
394394
}
395395

396396
static const char update_index_usage[] =
@@ -719,8 +719,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
719719
while (1) {
720720
char *path_name;
721721
const char *p;
722-
read_line(&buf, stdin, line_termination);
723-
if (buf.eof)
722+
if (strbuf_getline(&buf, stdin, line_termination) == EOF)
724723
break;
725724
if (line_termination && buf.buf[0] == '"')
726725
path_name = unquote_c_style(buf.buf, NULL);
@@ -735,6 +734,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
735734
if (path_name != buf.buf)
736735
free(path_name);
737736
}
737+
strbuf_release(&buf);
738738
}
739739

740740
finish:

fast-import.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,20 +1584,25 @@ static void dump_marks(void)
15841584
mark_file, strerror(errno));
15851585
}
15861586

1587-
static void read_next_command(void)
1587+
static int read_next_command(void)
15881588
{
1589+
static int stdin_eof = 0;
1590+
1591+
if (stdin_eof) {
1592+
unread_command_buf = 0;
1593+
return EOF;
1594+
}
1595+
15891596
do {
15901597
if (unread_command_buf) {
15911598
unread_command_buf = 0;
1592-
if (command_buf.eof)
1593-
return;
15941599
} else {
15951600
struct recent_command *rc;
15961601

15971602
strbuf_detach(&command_buf);
1598-
read_line(&command_buf, stdin, '\n');
1599-
if (command_buf.eof)
1600-
return;
1603+
stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
1604+
if (stdin_eof)
1605+
return EOF;
16011606

16021607
rc = rc_free;
16031608
if (rc)
@@ -1616,6 +1621,8 @@ static void read_next_command(void)
16161621
cmd_tail = rc;
16171622
}
16181623
} while (command_buf.buf[0] == '#');
1624+
1625+
return 0;
16191626
}
16201627

16211628
static void skip_optional_lf(void)
@@ -1648,8 +1655,7 @@ static void *cmd_data (size_t *size)
16481655
size_t term_len = command_buf.len - 5 - 2;
16491656

16501657
for (;;) {
1651-
read_line(&command_buf, stdin, '\n');
1652-
if (command_buf.eof)
1658+
if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
16531659
die("EOF in data (terminator '%s' not found)", term);
16541660
if (term_len == command_buf.len
16551661
&& !strcmp(term, command_buf.buf))
@@ -2095,7 +2101,7 @@ static void cmd_new_commit(void)
20952101
}
20962102

20972103
/* file_change* */
2098-
while (!command_buf.eof && command_buf.len > 0) {
2104+
while (command_buf.len > 0) {
20992105
if (!prefixcmp(command_buf.buf, "M "))
21002106
file_change_m(b);
21012107
else if (!prefixcmp(command_buf.buf, "D "))
@@ -2110,7 +2116,8 @@ static void cmd_new_commit(void)
21102116
unread_command_buf = 1;
21112117
break;
21122118
}
2113-
read_next_command();
2119+
if (read_next_command() == EOF)
2120+
break;
21142121
}
21152122

21162123
/* build the tree and the commit */
@@ -2375,11 +2382,8 @@ int main(int argc, const char **argv)
23752382
prepare_packed_git();
23762383
start_packfile();
23772384
set_die_routine(die_nicely);
2378-
for (;;) {
2379-
read_next_command();
2380-
if (command_buf.eof)
2381-
break;
2382-
else if (!strcmp("blob", command_buf.buf))
2385+
while (read_next_command() != EOF) {
2386+
if (!strcmp("blob", command_buf.buf))
23832387
cmd_new_blob();
23842388
else if (!prefixcmp(command_buf.buf, "commit "))
23852389
cmd_new_commit();

fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
222222
char *rf_one = NULL;
223223
char *tg_one;
224224

225-
read_line(&buf, stdin, '\n');
226-
if (buf.eof)
225+
if (strbuf_getline(&buf, stdin, '\n') == EOF)
227226
break;
228227
tg_one = buf.buf;
229228
rf_one = strchr(tg_one, '\t');
@@ -239,6 +238,7 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
239238
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
240239
targets++;
241240
}
241+
strbuf_release(&buf);
242242
return targets;
243243
}
244244

mktree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ int main(int ac, char **av)
8888
enum object_type type;
8989
char *path;
9090

91-
read_line(&sb, stdin, line_termination);
92-
if (sb.eof)
91+
if (strbuf_getline(&sb, stdin, line_termination) == EOF)
9392
break;
9493
ptr = sb.buf;
9594
/* Input is non-recursive ls-tree output format
@@ -121,6 +120,7 @@ int main(int ac, char **av)
121120
if (path != ntr)
122121
free(path);
123122
}
123+
strbuf_release(&sb);
124124
write_tree(sha1);
125125
puts(sha1_to_hex(sha1));
126126
exit(0);

strbuf.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ void strbuf_reset(struct strbuf *sb)
1717
{
1818
if (sb->len)
1919
strbuf_setlen(sb, 0);
20-
sb->eof = 0;
2120
}
2221

2322
char *strbuf_detach(struct strbuf *sb)
@@ -145,14 +144,13 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
145144
return sb->len - oldlen;
146145
}
147146

148-
void read_line(struct strbuf *sb, FILE *fp, int term)
147+
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
149148
{
150149
int ch;
151-
if (feof(fp)) {
152-
strbuf_release(sb);
153-
sb->eof = 1;
154-
return;
155-
}
150+
151+
strbuf_grow(sb, 0);
152+
if (feof(fp))
153+
return EOF;
156154

157155
strbuf_reset(sb);
158156
while ((ch = fgetc(fp)) != EOF) {
@@ -161,11 +159,9 @@ void read_line(struct strbuf *sb, FILE *fp, int term)
161159
strbuf_grow(sb, 1);
162160
sb->buf[sb->len++] = ch;
163161
}
164-
if (ch == EOF && sb->len == 0) {
165-
strbuf_release(sb);
166-
sb->eof = 1;
167-
}
162+
if (ch == EOF && sb->len == 0)
163+
return EOF;
168164

169-
strbuf_grow(sb, 1);
170165
sb->buf[sb->len] = '\0';
166+
return 0;
171167
}

strbuf.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@
4444
struct strbuf {
4545
size_t alloc;
4646
size_t len;
47-
int eof;
4847
char *buf;
4948
};
5049

51-
#define STRBUF_INIT { 0, 0, 0, NULL }
50+
#define STRBUF_INIT { 0, 0, NULL }
5251

5352
/*----- strbuf life cycle -----*/
5453
extern void strbuf_init(struct strbuf *, size_t);
@@ -101,6 +100,6 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
101100
/* XXX: if read fails, any partial read is undone */
102101
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
103102

104-
extern void read_line(struct strbuf *, FILE *, int);
103+
extern int strbuf_getline(struct strbuf *, FILE *, int);
105104

106105
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)