summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8b6087f)
raw | patch | inline | side by side (parent: 8b6087f)
author | Pierre Habouzit <madcoder@debian.org> | |
Mon, 17 Sep 2007 09:19:04 +0000 (11:19 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Tue, 18 Sep 2007 07:55:10 +0000 (00:55 -0700) |
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>
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>
builtin-checkout-index.c | patch | blob | history | |
builtin-update-index.c | patch | blob | history | |
fast-import.c | patch | blob | history | |
fetch.c | patch | blob | history | |
mktree.c | patch | blob | history | |
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
index 85e8efe22f6446e358427907466278d69b3bca79..a18ecc4bab7541477144402d4d680277d95c4e9a 100644 (file)
--- a/builtin-checkout-index.c
+++ b/builtin-checkout-index.c
while (1) {
char *path_name;
const char *p;
-
- read_line(&buf, stdin, line_termination);
- if (buf.eof)
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;
if (line_termination && buf.buf[0] == '"')
path_name = unquote_c_style(buf.buf, NULL);
if (path_name != buf.buf)
free(path_name);
}
+ strbuf_release(&buf);
}
if (all)
diff --git a/builtin-update-index.c b/builtin-update-index.c
index 1091f1b26f0c19930ccf8b615d1c0eeeaf586f47..45e33f5584b6c61cb64d7b408c89917320487f43 100644 (file)
--- a/builtin-update-index.c
+++ b/builtin-update-index.c
* This format is to put higher order stages into the
* index file and matches git-ls-files --stage output.
*/
- read_line(&buf, stdin, line_termination);
- if (buf.eof)
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;
errno = 0;
bad_line:
die("malformed index info %s", buf.buf);
}
+ strbuf_release(&buf);
}
static const char update_index_usage[] =
while (1) {
char *path_name;
const char *p;
- read_line(&buf, stdin, line_termination);
- if (buf.eof)
+ if (strbuf_getline(&buf, stdin, line_termination) == EOF)
break;
if (line_termination && buf.buf[0] == '"')
path_name = unquote_c_style(buf.buf, NULL);
if (path_name != buf.buf)
free(path_name);
}
+ strbuf_release(&buf);
}
finish:
diff --git a/fast-import.c b/fast-import.c
index 1866d346bfb62cd80870edb0a7c595218c9f81c4..da045662ac16883ed28a3c783b4d7c6d10a130cc 100644 (file)
--- a/fast-import.c
+++ b/fast-import.c
mark_file, strerror(errno));
}
-static void read_next_command(void)
+static int read_next_command(void)
{
+ static int stdin_eof = 0;
+
+ if (stdin_eof) {
+ unread_command_buf = 0;
+ return EOF;
+ }
+
do {
if (unread_command_buf) {
unread_command_buf = 0;
- if (command_buf.eof)
- return;
} else {
struct recent_command *rc;
strbuf_detach(&command_buf);
- read_line(&command_buf, stdin, '\n');
- if (command_buf.eof)
- return;
+ stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
+ if (stdin_eof)
+ return EOF;
rc = rc_free;
if (rc)
cmd_tail = rc;
}
} while (command_buf.buf[0] == '#');
+
+ return 0;
}
static void skip_optional_lf(void)
size_t term_len = command_buf.len - 5 - 2;
for (;;) {
- read_line(&command_buf, stdin, '\n');
- if (command_buf.eof)
+ if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
die("EOF in data (terminator '%s' not found)", term);
if (term_len == command_buf.len
&& !strcmp(term, command_buf.buf))
}
/* file_change* */
- while (!command_buf.eof && command_buf.len > 0) {
+ while (command_buf.len > 0) {
if (!prefixcmp(command_buf.buf, "M "))
file_change_m(b);
else if (!prefixcmp(command_buf.buf, "D "))
unread_command_buf = 1;
break;
}
- read_next_command();
+ if (read_next_command() == EOF)
+ break;
}
/* build the tree and the commit */
prepare_packed_git();
start_packfile();
set_die_routine(die_nicely);
- for (;;) {
- read_next_command();
- if (command_buf.eof)
- break;
- else if (!strcmp("blob", command_buf.buf))
+ while (read_next_command() != EOF) {
+ if (!strcmp("blob", command_buf.buf))
cmd_new_blob();
else if (!prefixcmp(command_buf.buf, "commit "))
cmd_new_commit();
index c256e6f6b45212a1dc99646d0ed3d234c087d96c..b1c1f07b2a91b6abebe29ceb1d5f2f7afe2e452e 100644 (file)
--- a/fetch.c
+++ b/fetch.c
char *rf_one = NULL;
char *tg_one;
- read_line(&buf, stdin, '\n');
- if (buf.eof)
+ if (strbuf_getline(&buf, stdin, '\n') == EOF)
break;
tg_one = buf.buf;
rf_one = strchr(tg_one, '\t');
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
targets++;
}
+ strbuf_release(&buf);
return targets;
}
diff --git a/mktree.c b/mktree.c
index 5dab4bd367c3d62b288b34abb23c083326fc41b9..9c137dec4599c97d2827abda68692dc714f2d295 100644 (file)
--- a/mktree.c
+++ b/mktree.c
enum object_type type;
char *path;
- read_line(&sb, stdin, line_termination);
- if (sb.eof)
+ if (strbuf_getline(&sb, stdin, line_termination) == EOF)
break;
ptr = sb.buf;
/* Input is non-recursive ls-tree output format
if (path != ntr)
free(path);
}
+ strbuf_release(&sb);
write_tree(sha1);
puts(sha1_to_hex(sha1));
exit(0);
diff --git a/strbuf.c b/strbuf.c
index c5f9e2a28f1441e0ae79e3cac3a3640bf943ed52..59383ac7763dd58ba8da1c1daea6906740affea0 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
{
if (sb->len)
strbuf_setlen(sb, 0);
- sb->eof = 0;
}
char *strbuf_detach(struct strbuf *sb)
return sb->len - oldlen;
}
-void read_line(struct strbuf *sb, FILE *fp, int term)
+int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
{
int ch;
- if (feof(fp)) {
- strbuf_release(sb);
- sb->eof = 1;
- return;
- }
+
+ strbuf_grow(sb, 0);
+ if (feof(fp))
+ return EOF;
strbuf_reset(sb);
while ((ch = fgetc(fp)) != EOF) {
strbuf_grow(sb, 1);
sb->buf[sb->len++] = ch;
}
- if (ch == EOF && sb->len == 0) {
- strbuf_release(sb);
- sb->eof = 1;
- }
+ if (ch == EOF && sb->len == 0)
+ return EOF;
- strbuf_grow(sb, 1);
sb->buf[sb->len] = '\0';
+ return 0;
}
diff --git a/strbuf.h b/strbuf.h
index f163c63e6f405464c343e54f35bab53f53ea6980..b2cbd976f4d93a326a292e95c245eaeeaa4b73dc 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
struct strbuf {
size_t alloc;
size_t len;
- int eof;
char *buf;
};
-#define STRBUF_INIT { 0, 0, 0, NULL }
+#define STRBUF_INIT { 0, 0, NULL }
/*----- strbuf life cycle -----*/
extern void strbuf_init(struct strbuf *, size_t);
/* XXX: if read fails, any partial read is undone */
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
-extern void read_line(struct strbuf *, FILE *, int);
+extern int strbuf_getline(struct strbuf *, FILE *, int);
#endif /* STRBUF_H */