Skip to content

Commit 819b929

Browse files
peffgitster
authored andcommitted
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>
1 parent 0380942 commit 819b929

File tree

13 files changed

+22
-35
lines changed

13 files changed

+22
-35
lines changed

builtin/archive.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ static int run_remote_archiver(int argc, const char **argv,
5656
len = packet_read_line(fd[0], buf, sizeof(buf));
5757
if (!len)
5858
die(_("git archive: expected ACK/NAK, got EOF"));
59-
if (buf[len-1] == '\n')
60-
buf[--len] = 0;
6159
if (strcmp(buf, "ACK")) {
6260
if (len > 5 && !prefixcmp(buf, "NACK "))
6361
die(_("git archive: NACK %s"), buf + 5);

builtin/fetch-pack.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
105105
int n = packet_read_line(0, line, sizeof(line));
106106
if (!n)
107107
break;
108-
if (line[n-1] == '\n')
109-
n--;
110108
string_list_append(&sought, xmemdupz(line, n));
111109
}
112110
}

builtin/receive-pack.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,6 @@ static struct command *read_head_info(void)
763763
len = packet_read_line(0, line, sizeof(line));
764764
if (!len)
765765
break;
766-
if (line[len-1] == '\n')
767-
line[--len] = 0;
768766
if (len < 83 ||
769767
line[40] != ' ' ||
770768
line[81] != ' ' ||

builtin/upload-archive.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
4040
if (sent_argv.argc > MAX_ARGS)
4141
die("Too many options (>%d)", MAX_ARGS - 1);
4242

43-
if (buf[len-1] == '\n') {
44-
buf[--len] = 0;
45-
}
46-
4743
if (prefixcmp(buf, arg_cmd))
4844
die("'argument' token or flush expected");
4945
argv_array_push(&sent_argv, buf + strlen(arg_cmd));

connect.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ struct ref **get_remote_heads(int in, struct ref **list,
7777
int len, name_len;
7878

7979
len = packet_read(in, buffer, sizeof(buffer),
80-
PACKET_READ_GENTLE_ON_EOF);
80+
PACKET_READ_GENTLE_ON_EOF |
81+
PACKET_READ_CHOMP_NEWLINE);
8182
if (len < 0)
8283
die_initial_contact(got_at_least_one_head);
8384

8485
if (!len)
8586
break;
86-
if (buffer[len-1] == '\n')
87-
buffer[--len] = 0;
8887

8988
if (len > 4 && !prefixcmp(buffer, "ERR "))
9089
die("remote error: %s", buffer + 4);

daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static int execute(void)
612612
loginfo("Connection from %s:%s", addr, port);
613613

614614
alarm(init_timeout ? init_timeout : timeout);
615-
pktlen = packet_read_line(0, line, sizeof(line));
615+
pktlen = packet_read(0, line, sizeof(line), 0);
616616
alarm(0);
617617

618618
len = strlen(line);

fetch-pack.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
220220

221221
if (!len)
222222
die("git fetch-pack: expected ACK/NAK, got EOF");
223-
if (line[len-1] == '\n')
224-
line[--len] = 0;
225223
if (!strcmp(line, "NAK"))
226224
return NAK;
227225
if (!prefixcmp(line, "ACK ")) {

pkt-line.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,19 @@ int packet_read(int fd, char *buffer, unsigned size, int options)
164164
ret = safe_read(fd, buffer, len, options);
165165
if (ret < 0)
166166
return ret;
167+
168+
if ((options & PACKET_READ_CHOMP_NEWLINE) &&
169+
len && buffer[len-1] == '\n')
170+
len--;
171+
167172
buffer[len] = 0;
168173
packet_trace(buffer, len, 0);
169174
return len;
170175
}
171176

172177
int packet_read_line(int fd, char *buffer, unsigned size)
173178
{
174-
return packet_read(fd, buffer, size, 0);
179+
return packet_read(fd, buffer, size, PACKET_READ_CHOMP_NEWLINE);
175180
}
176181

177182
int packet_get_line(struct strbuf *out,

pkt-line.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((f
4444
* If options does contain PACKET_READ_GENTLE_ON_EOF, we will not die on
4545
* condition 4 (truncated input), but instead return -1. However, we will still
4646
* 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.
4750
*/
4851
#define PACKET_READ_GENTLE_ON_EOF (1u<<0)
52+
#define PACKET_READ_CHOMP_NEWLINE (1u<<1)
4953
int packet_read(int fd, char *buffer, unsigned size, int options);
5054

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+
*/
5259
int packet_read_line(int fd, char *buffer, unsigned size);
5360

5461
int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);

remote-curl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static size_t rpc_out(void *ptr, size_t eltsize,
308308

309309
if (!avail) {
310310
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);
312312
if (!avail)
313313
return 0;
314314
rpc->pos = 0;
@@ -425,7 +425,7 @@ static int post_rpc(struct rpc_state *rpc)
425425
break;
426426
}
427427

428-
n = packet_read_line(rpc->out, buf, left);
428+
n = packet_read(rpc->out, buf, left, 0);
429429
if (!n)
430430
break;
431431
rpc->len += n;
@@ -579,7 +579,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
579579
rpc->hdr_accept = strbuf_detach(&buf, NULL);
580580

581581
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);
583583
if (!n)
584584
break;
585585
rpc->pos = 0;

0 commit comments

Comments
 (0)