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>
Pierre Habouzit committed
Sep 17, 2007 at 11:19 UTC
e6c019d0b0140fae1cdfd661746cbe319b6c3670
7 files changed
+39
-41
builtin-checkout-index.c
+2
-3
@@ -277,9 +277,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
277
while (1) {
278
char *path_name;
279
const char *p;
280
-
281
- read_line(&buf, stdin, line_termination);
282
- if (buf.eof)
280
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
281
break;
282
if (line_termination && buf.buf[0] == '"')
283
path_name = unquote_c_style(buf.buf, NULL);
@@ -292,6 +290,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
290
if (path_name != buf.buf)
291
free(path_name);
292
}
293
+ strbuf_release(&buf);
294
}
295
296
if (all)
builtin-update-index.c
+4
-4
@@ -327,8 +327,7 @@ static void read_index_info(int line_termination)
327
* This format is to put higher order stages into the
328
* index file and matches git-ls-files --stage output.
329
*/
330
- read_line(&buf, stdin, line_termination);
331
- if (buf.eof)
330
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
331
break;
332
333
errno = 0;
@@ -391,6 +390,7 @@ static void read_index_info(int line_termination)
390
bad_line:
391
die("malformed index info %s", buf.buf);
392
}
393
+ strbuf_release(&buf);
394
}
395
396
static const char update_index_usage[] =
@@ -719,8 +719,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
719
while (1) {
720
char *path_name;
721
const char *p;
722
- read_line(&buf, stdin, line_termination);
723
- if (buf.eof)
722
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
723
break;
724
if (line_termination && buf.buf[0] == '"')
725
path_name = unquote_c_style(buf.buf, NULL);
@@ -735,6 +734,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
734
if (path_name != buf.buf)
735
free(path_name);
736
}
737
+ strbuf_release(&buf);
738
}
739
740
finish:
fast-import.c
+19
-15
@@ -1584,20 +1584,25 @@ static void dump_marks(void)
1584
mark_file, strerror(errno));
1585
}
1586
1587
-static void read_next_command(void)
1587
+static int read_next_command(void)
1588
{
1589
+ static int stdin_eof = 0;
1590
+
1591
+ if (stdin_eof) {
1592
+ unread_command_buf = 0;
1593
+ return EOF;
1594
+ }
1595
+
1596
do {
1597
if (unread_command_buf) {
1598
unread_command_buf = 0;
1592
- if (command_buf.eof)
1593
- return;
1599
} else {
1600
struct recent_command *rc;
1601
1602
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;
1606
1607
rc = rc_free;
1608
if (rc)
@@ -1616,6 +1621,8 @@ static void read_next_command(void)
1621
cmd_tail = rc;
1622
}
1623
} while (command_buf.buf[0] == '#');
1624
+
1625
+ return 0;
1626
}
1627
1628
static void skip_optional_lf(void)
@@ -1648,8 +1655,7 @@ static void *cmd_data (size_t *size)
1655
size_t term_len = command_buf.len - 5 - 2;
1656
1657
for (;;) {
1651
- read_line(&command_buf, stdin, '\n');
1652
- if (command_buf.eof)
1658
+ if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
1659
die("EOF in data (terminator '%s' not found)", term);
1660
if (term_len == command_buf.len
1661
&& !strcmp(term, command_buf.buf))
@@ -2095,7 +2101,7 @@ static void cmd_new_commit(void)
2101
}
2102
2103
/* file_change* */
2098
- while (!command_buf.eof && command_buf.len > 0) {
2104
+ while (command_buf.len > 0) {
2105
if (!prefixcmp(command_buf.buf, "M "))
2106
file_change_m(b);
2107
else if (!prefixcmp(command_buf.buf, "D "))
@@ -2110,7 +2116,8 @@ static void cmd_new_commit(void)
2116
unread_command_buf = 1;
2117
break;
2118
}
2113
- read_next_command();
2119
+ if (read_next_command() == EOF)
2120
+ break;
2121
}
2122
2123
/* build the tree and the commit */
@@ -2375,11 +2382,8 @@ int main(int argc, const char **argv)
2382
prepare_packed_git();
2383
start_packfile();
2384
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))
2387
cmd_new_blob();
2388
else if (!prefixcmp(command_buf.buf, "commit "))
2389
cmd_new_commit();
fetch.c
+2
-2
@@ -222,8 +222,7 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
222
char *rf_one = NULL;
223
char *tg_one;
224
225
- read_line(&buf, stdin, '\n');
226
- if (buf.eof)
225
+ if (strbuf_getline(&buf, stdin, '\n') == EOF)
226
break;
227
tg_one = buf.buf;
228
rf_one = strchr(tg_one, '\t');
@@ -239,6 +238,7 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
238
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
239
targets++;
240
}
241
+ strbuf_release(&buf);
242
return targets;
243
}
244
mktree.c
+2
-2
@@ -88,8 +88,7 @@ int main(int ac, char **av)
88
enum object_type type;
89
char *path;
90
91
- read_line(&sb, stdin, line_termination);
92
- if (sb.eof)
91
+ if (strbuf_getline(&sb, stdin, line_termination) == EOF)
92
break;
93
ptr = sb.buf;
94
/* Input is non-recursive ls-tree output format
@@ -121,6 +120,7 @@ int main(int ac, char **av)
120
if (path != ntr)
121
free(path);
122
}
123
+ strbuf_release(&sb);
124
write_tree(sha1);
125
puts(sha1_to_hex(sha1));
126
exit(0);
strbuf.c
+8
-12
@@ -17,7 +17,6 @@ void strbuf_reset(struct strbuf *sb)
17
{
18
if (sb->len)
19
strbuf_setlen(sb, 0);
20
- sb->eof = 0;
20
}
21
22
char *strbuf_detach(struct strbuf *sb)
@@ -145,14 +144,13 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
144
return sb->len - oldlen;
145
}
146
148
-void read_line(struct strbuf *sb, FILE *fp, int term)
147
+int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
148
{
149
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;
154
155
strbuf_reset(sb);
156
while ((ch = fgetc(fp)) != EOF) {
@@ -161,11 +159,9 @@ void read_line(struct strbuf *sb, FILE *fp, int term)
159
strbuf_grow(sb, 1);
160
sb->buf[sb->len++] = ch;
161
}
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;
164
169
- strbuf_grow(sb, 1);
165
sb->buf[sb->len] = '\0';
166
+ return 0;
167
}
strbuf.h
+2
-3
@@ -44,11 +44,10 @@
44
struct strbuf {
45
size_t alloc;
46
size_t len;
47
- int eof;
47
char *buf;
48
};
49
51
-#define STRBUF_INIT { 0, 0, 0, NULL }
50
+#define STRBUF_INIT { 0, 0, NULL }
51
52
/*----- strbuf life cycle -----*/
53
extern void strbuf_init(struct strbuf *, size_t);
@@ -101,6 +100,6 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
100
/* XXX: if read fails, any partial read is undone */
101
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
102
104
-extern void read_line(struct strbuf *, FILE *, int);
103
+extern int strbuf_getline(struct strbuf *, FILE *, int);
104
105
#endif /* STRBUF_H */