diff --git a/builtin/archive.c b/builtin/archive.c index 9a1cfd3dac0123cb65d3db7597221a29e9010bd9..d381ac4147ff6dd3019d223eda2c3256884f2adf 100644 --- a/builtin/archive.c +++ b/builtin/archive.c @@ -56,8 +56,6 @@ static int run_remote_archiver(int argc, const char **argv, len = packet_read_line(fd[0], buf, sizeof(buf)); if (!len) die(_("git archive: expected ACK/NAK, got EOF")); - if (buf[len-1] == '\n') - buf[--len] = 0; if (strcmp(buf, "ACK")) { if (len > 5 && !prefixcmp(buf, "NACK ")) die(_("git archive: NACK %s"), buf + 5); diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index 940ae35dc2ce4923a248896eb764f145312d71b4..f73664f433c9b602515fe28456da8c86ef0fe4b8 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -105,8 +105,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) int n = packet_read_line(0, line, sizeof(line)); if (!n) break; - if (line[n-1] == '\n') - n--; string_list_append(&sought, xmemdupz(line, n)); } } diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 912956378247c4de5c33654cded00b2e13dca491..6679e636c721ec28ac818d5584813dbe8ddea785 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -763,8 +763,6 @@ static struct command *read_head_info(void) len = packet_read_line(0, line, sizeof(line)); if (!len) break; - if (line[len-1] == '\n') - line[--len] = 0; if (len < 83 || line[40] != ' ' || line[81] != ' ' || diff --git a/builtin/upload-archive.c b/builtin/upload-archive.c index 1517dec4063126170384e0a6f482c2f89e45122c..d90f0aba44fdac817ac48f4329d72e65f1e8f663 100644 --- a/builtin/upload-archive.c +++ b/builtin/upload-archive.c @@ -40,10 +40,6 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix) if (sent_argv.argc > MAX_ARGS) die("Too many options (>%d)", MAX_ARGS - 1); - if (buf[len-1] == '\n') { - buf[--len] = 0; - } - if (prefixcmp(buf, arg_cmd)) die("'argument' token or flush expected"); argv_array_push(&sent_argv, buf + strlen(arg_cmd)); diff --git a/connect.c b/connect.c index 0aa202f885dce3791d6c92bfa434c084f424b145..fe8eb01ae21c15f1013fd398825aeb20f9630336 100644 --- a/connect.c +++ b/connect.c @@ -77,14 +77,13 @@ struct ref **get_remote_heads(int in, struct ref **list, int len, name_len; len = packet_read(in, buffer, sizeof(buffer), - PACKET_READ_GENTLE_ON_EOF); + PACKET_READ_GENTLE_ON_EOF | + PACKET_READ_CHOMP_NEWLINE); if (len < 0) die_initial_contact(got_at_least_one_head); if (!len) break; - if (buffer[len-1] == '\n') - buffer[--len] = 0; if (len > 4 && !prefixcmp(buffer, "ERR ")) die("remote error: %s", buffer + 4); diff --git a/daemon.c b/daemon.c index 4602b46a5c39e1d501143ab4e95b55aff5c8f23b..4f5cd615589901c94c1439c9bff3e870d3d4af32 100644 --- a/daemon.c +++ b/daemon.c @@ -612,7 +612,7 @@ static int execute(void) loginfo("Connection from %s:%s", addr, port); alarm(init_timeout ? init_timeout : timeout); - pktlen = packet_read_line(0, line, sizeof(line)); + pktlen = packet_read(0, line, sizeof(line), 0); alarm(0); len = strlen(line); diff --git a/fetch-pack.c b/fetch-pack.c index b53a18f923758001e631da0b650dcca6663e6897..f830db224bd6e05534de232331034b204286af5b 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -220,8 +220,6 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1) if (!len) die("git fetch-pack: expected ACK/NAK, got EOF"); - if (line[len-1] == '\n') - line[--len] = 0; if (!strcmp(line, "NAK")) return NAK; if (!prefixcmp(line, "ACK ")) { diff --git a/pkt-line.c b/pkt-line.c index 8700cf8add94507c67959f1939b9710a7d5cbf52..dc11c407cd8513d423356d16c1b5b0f929264a39 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -164,6 +164,11 @@ int packet_read(int fd, char *buffer, unsigned size, int options) ret = safe_read(fd, buffer, len, options); if (ret < 0) return ret; + + if ((options & PACKET_READ_CHOMP_NEWLINE) && + len && buffer[len-1] == '\n') + len--; + buffer[len] = 0; packet_trace(buffer, len, 0); return len; @@ -171,7 +176,7 @@ int packet_read(int fd, char *buffer, unsigned size, int options) int packet_read_line(int fd, char *buffer, unsigned size) { - return packet_read(fd, buffer, size, 0); + return packet_read(fd, buffer, size, PACKET_READ_CHOMP_NEWLINE); } int packet_get_line(struct strbuf *out, diff --git a/pkt-line.h b/pkt-line.h index 8cd326c92237bd5ecc67587e2199c5ea4c1bae67..5d2fb423d60486e8185fbdfef95e75cd31483f00 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -44,11 +44,18 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((f * If options does contain PACKET_READ_GENTLE_ON_EOF, we will not die on * condition 4 (truncated input), but instead return -1. However, we will still * die for the other 3 conditions. + * + * If options contains PACKET_READ_CHOMP_NEWLINE, a trailing newline (if + * present) is removed from the buffer before returning. */ #define PACKET_READ_GENTLE_ON_EOF (1u<<0) +#define PACKET_READ_CHOMP_NEWLINE (1u<<1) int packet_read(int fd, char *buffer, unsigned size, int options); -/* Historical convenience wrapper for packet_read that sets no options */ +/* + * Convenience wrapper for packet_read that is not gentle, and sets the + * CHOMP_NEWLINE option. + */ int packet_read_line(int fd, char *buffer, unsigned size); int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len); diff --git a/remote-curl.c b/remote-curl.c index 7be4b53495647ebf06b50bd22a37678481bfcb3a..b28f96504815224b2b1a06d8d07f0c6ef81c6380 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -308,7 +308,7 @@ static size_t rpc_out(void *ptr, size_t eltsize, if (!avail) { rpc->initial_buffer = 0; - avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc); + avail = packet_read(rpc->out, rpc->buf, rpc->alloc, 0); if (!avail) return 0; rpc->pos = 0; @@ -425,7 +425,7 @@ static int post_rpc(struct rpc_state *rpc) break; } - n = packet_read_line(rpc->out, buf, left); + n = packet_read(rpc->out, buf, left, 0); if (!n) break; rpc->len += n; @@ -579,7 +579,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads) rpc->hdr_accept = strbuf_detach(&buf, NULL); while (!err) { - int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc); + int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0); if (!n) break; rpc->pos = 0; diff --git a/send-pack.c b/send-pack.c index bde796b1bbb80aca5c3e39c0b0365dcbb43b9c09..8c230bf6c925ba6bd3617af62f44cfea8dc09a7b 100644 --- a/send-pack.c +++ b/send-pack.c @@ -111,10 +111,7 @@ static int receive_status(int in, struct ref *refs) int len = packet_read_line(in, line, sizeof(line)); if (prefixcmp(line, "unpack ")) return error("did not receive remote status"); - if (strcmp(line, "unpack ok\n")) { - char *p = line + strlen(line) - 1; - if (*p == '\n') - *p = '\0'; + if (strcmp(line, "unpack ok")) { error("unpack failed: %s", line + 7); ret = -1; } @@ -131,7 +128,6 @@ static int receive_status(int in, struct ref *refs) break; } - line[strlen(line)-1] = '\0'; refname = line + 3; msg = strchr(refname, ' '); if (msg) diff --git a/sideband.c b/sideband.c index 8f7b25bf7915ec80d55d8a9f6fa6a8953b495204..15cc1aec22115a9f0d41d9e06e29184f64f1a86a 100644 --- a/sideband.c +++ b/sideband.c @@ -38,7 +38,7 @@ int recv_sideband(const char *me, int in_stream, int out) while (1) { int band, len; - len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX); + len = packet_read(in_stream, buf + pf, LARGE_PACKET_MAX, 0); if (len == 0) break; if (len < 1) { diff --git a/upload-pack.c b/upload-pack.c index afc2d9279cdcd57b235cbf69590ffc102f2397b5..6e6d16687640e561cbaa745a2d851a83f3630681 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -50,13 +50,6 @@ static void reset_timeout(void) alarm(timeout); } -static int strip(char *line, int len) -{ - if (len && line[len-1] == '\n') - line[--len] = 0; - return len; -} - static ssize_t send_client_data(int fd, const char *data, ssize_t sz) { if (use_sideband) @@ -447,7 +440,6 @@ static int get_common_commits(void) got_other = 0; continue; } - strip(line, len); if (!prefixcmp(line, "have ")) { switch (got_sha1(line+5, sha1)) { case -1: /* they have what we do not */