pkt-line: teach packet_read_line to chomp newlines

The packets sent during ref negotiation are all terminated by newline; even though the code to chomp these newlines is short, we end up doing it in a lot of places. This patch teaches packet_read_line to auto-chomp the trailing newline; this lets us get rid of a lot of inline chomping code. As a result, some call-sites which are not reading line-oriented data (e.g., when reading chunks of packfiles alongside sideband) transition away from packet_read_line to the generic packet_read interface. This patch converts all of the existing callsites. Since the function signature of packet_read_line does not change (but its behavior does), there is a possibility of new callsites being introduced in later commits, silently introducing an incompatibility. However, since a later patch in this series will change the signature, such a commit would have to be merged directly into this commit, not to the tip of the series; we can therefore ignore the issue. This is an internal cleanup and should produce no change of behavior in the normal case. However, there is one corner case to note. Callers of packet_read_line have never been able to tell the difference between a flush packet ("0000") and an empty packet ("0004"), as both cause packet_read_line to return a length of 0. Readers treat them identically, even though Documentation/technical/protocol-common.txt says we must not; it also says that implementations should not send an empty pkt-line. By stripping out the newline before the result gets to the caller, we will now treat the newline-only packet ("0005\n") the same as an empty packet, which in turn gets treated like a flush packet. In practice this doesn't matter, as neither empty nor newline-only packets are part of git's protocols (at least not for the line-oriented bits, and readers who are not expecting line-oriented packets will be calling packet_read directly, anyway). But even if we do decide to care about the distinction later, it is orthogonal to this patch. The right place to tighten would be to stop treating empty packets as flush packets, and this change does not make doing so any harder. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 20, 2013 at 15:02 UTC 819b929d3389f6007e1c469d9060e7876caeb97f
13 files changed +22 -35
builtin/archive.c
-2
@@ -56,8 +56,6 @@ static int run_remote_archiver(int argc, const char **argv,
56 len = packet_read_line(fd[0], buf, sizeof(buf));
57 if (!len)
58 die(_("git archive: expected ACK/NAK, got EOF"));
59 - if (buf[len-1] == '\n')
60 - buf[--len] = 0;
59 if (strcmp(buf, "ACK")) {
60 if (len > 5 && !prefixcmp(buf, "NACK "))
61 die(_("git archive: NACK %s"), buf + 5);
builtin/fetch-pack.c
-2
@@ -105,8 +105,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
105 int n = packet_read_line(0, line, sizeof(line));
106 if (!n)
107 break;
108 - if (line[n-1] == '\n')
109 - n--;
108 string_list_append(&sought, xmemdupz(line, n));
109 }
110 }
builtin/receive-pack.c
-2
@@ -763,8 +763,6 @@ static struct command *read_head_info(void)
763 len = packet_read_line(0, line, sizeof(line));
764 if (!len)
765 break;
766 - if (line[len-1] == '\n')
767 - line[--len] = 0;
766 if (len < 83 ||
767 line[40] != ' ' ||
768 line[81] != ' ' ||
builtin/upload-archive.c
-4
@@ -40,10 +40,6 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
40 if (sent_argv.argc > MAX_ARGS)
41 die("Too many options (>%d)", MAX_ARGS - 1);
42
43 - if (buf[len-1] == '\n') {
44 - buf[--len] = 0;
45 - }
46 -
43 if (prefixcmp(buf, arg_cmd))
44 die("'argument' token or flush expected");
45 argv_array_push(&sent_argv, buf + strlen(arg_cmd));
connect.c
+2 -3
@@ -77,14 +77,13 @@ struct ref **get_remote_heads(int in, struct ref **list,
77 int len, name_len;
78
79 len = packet_read(in, buffer, sizeof(buffer),
80 - PACKET_READ_GENTLE_ON_EOF);
80 + PACKET_READ_GENTLE_ON_EOF |
81 + PACKET_READ_CHOMP_NEWLINE);
82 if (len < 0)
83 die_initial_contact(got_at_least_one_head);
84
85 if (!len)
86 break;
86 - if (buffer[len-1] == '\n')
87 - buffer[--len] = 0;
87
88 if (len > 4 && !prefixcmp(buffer, "ERR "))
89 die("remote error: %s", buffer + 4);
daemon.c
+1 -1
@@ -612,7 +612,7 @@ static int execute(void)
612 loginfo("Connection from %s:%s", addr, port);
613
614 alarm(init_timeout ? init_timeout : timeout);
615 - pktlen = packet_read_line(0, line, sizeof(line));
615 + pktlen = packet_read(0, line, sizeof(line), 0);
616 alarm(0);
617
618 len = strlen(line);
fetch-pack.c
-2
@@ -220,8 +220,6 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
220
221 if (!len)
222 die("git fetch-pack: expected ACK/NAK, got EOF");
223 - if (line[len-1] == '\n')
224 - line[--len] = 0;
223 if (!strcmp(line, "NAK"))
224 return NAK;
225 if (!prefixcmp(line, "ACK ")) {
pkt-line.c
+6 -1
@@ -164,6 +164,11 @@ int packet_read(int fd, char *buffer, unsigned size, int options)
164 ret = safe_read(fd, buffer, len, options);
165 if (ret < 0)
166 return ret;
167 +
168 + if ((options & PACKET_READ_CHOMP_NEWLINE) &&
169 + len && buffer[len-1] == '\n')
170 + len--;
171 +
172 buffer[len] = 0;
173 packet_trace(buffer, len, 0);
174 return len;
@@ -171,7 +176,7 @@ int packet_read(int fd, char *buffer, unsigned size, int options)
176
177 int packet_read_line(int fd, char *buffer, unsigned size)
178 {
174 - return packet_read(fd, buffer, size, 0);
179 + return packet_read(fd, buffer, size, PACKET_READ_CHOMP_NEWLINE);
180 }
181
182 int packet_get_line(struct strbuf *out,
pkt-line.h
+8 -1
@@ -44,11 +44,18 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((f
44 * If options does contain PACKET_READ_GENTLE_ON_EOF, we will not die on
45 * condition 4 (truncated input), but instead return -1. However, we will still
46 * die for the other 3 conditions.
47 + *
48 + * If options contains PACKET_READ_CHOMP_NEWLINE, a trailing newline (if
49 + * present) is removed from the buffer before returning.
50 */
51 #define PACKET_READ_GENTLE_ON_EOF (1u<<0)
52 +#define PACKET_READ_CHOMP_NEWLINE (1u<<1)
53 int packet_read(int fd, char *buffer, unsigned size, int options);
54
51 -/* Historical convenience wrapper for packet_read that sets no options */
55 +/*
56 + * Convenience wrapper for packet_read that is not gentle, and sets the
57 + * CHOMP_NEWLINE option.
58 + */
59 int packet_read_line(int fd, char *buffer, unsigned size);
60
61 int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);
remote-curl.c
+3 -3
@@ -308,7 +308,7 @@ static size_t rpc_out(void *ptr, size_t eltsize,
308
309 if (!avail) {
310 rpc->initial_buffer = 0;
311 - avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
311 + avail = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
312 if (!avail)
313 return 0;
314 rpc->pos = 0;
@@ -425,7 +425,7 @@ static int post_rpc(struct rpc_state *rpc)
425 break;
426 }
427
428 - n = packet_read_line(rpc->out, buf, left);
428 + n = packet_read(rpc->out, buf, left, 0);
429 if (!n)
430 break;
431 rpc->len += n;
@@ -579,7 +579,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
579 rpc->hdr_accept = strbuf_detach(&buf, NULL);
580
581 while (!err) {
582 - int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
582 + int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
583 if (!n)
584 break;
585 rpc->pos = 0;
send-pack.c
+1 -5
@@ -111,10 +111,7 @@ static int receive_status(int in, struct ref *refs)
111 int len = packet_read_line(in, line, sizeof(line));
112 if (prefixcmp(line, "unpack "))
113 return error("did not receive remote status");
114 - if (strcmp(line, "unpack ok\n")) {
115 - char *p = line + strlen(line) - 1;
116 - if (*p == '\n')
117 - *p = '\0';
114 + if (strcmp(line, "unpack ok")) {
115 error("unpack failed: %s", line + 7);
116 ret = -1;
117 }
@@ -131,7 +128,6 @@ static int receive_status(int in, struct ref *refs)
128 break;
129 }
130
134 - line[strlen(line)-1] = '\0';
131 refname = line + 3;
132 msg = strchr(refname, ' ');
133 if (msg)
sideband.c
+1 -1
@@ -38,7 +38,7 @@ int recv_sideband(const char *me, int in_stream, int out)
38
39 while (1) {
40 int band, len;
41 - len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
41 + len = packet_read(in_stream, buf + pf, LARGE_PACKET_MAX, 0);
42 if (len == 0)
43 break;
44 if (len < 1) {
upload-pack.c
-8
@@ -50,13 +50,6 @@ static void reset_timeout(void)
50 alarm(timeout);
51 }
52
53 -static int strip(char *line, int len)
54 -{
55 - if (len && line[len-1] == '\n')
56 - line[--len] = 0;
57 - return len;
58 -}
59 -
53 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
54 {
55 if (use_sideband)
@@ -447,7 +440,6 @@ static int get_common_commits(void)
440 got_other = 0;
441 continue;
442 }
450 - strip(line, len);
443 if (!prefixcmp(line, "have ")) {
444 switch (got_sha1(line+5, sha1)) {
445 case -1: /* they have what we do not */