diff options
author | Jeff King <peff@peff.net> | 2013-02-20 15:02:28 -0500 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-02-20 13:42:21 -0800 |
commit | 819b929d3389f6007e1c469d9060e7876caeb97f (patch) | |
tree | a978fc00c1d271cd00b910c3e4a6dc864ea7de55 /builtin | |
parent | 0380942902b23f02f7f595bc394e09bcd74d4ded (diff) | |
download | git-819b929d3389f6007e1c469d9060e7876caeb97f.tar.gz |
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>
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/archive.c | 2 | ||||
-rw-r--r-- | builtin/fetch-pack.c | 2 | ||||
-rw-r--r-- | builtin/receive-pack.c | 2 | ||||
-rw-r--r-- | builtin/upload-archive.c | 4 |
4 files changed, 0 insertions, 10 deletions
diff --git a/builtin/archive.c b/builtin/archive.c index 9a1cfd3dac..d381ac4147 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 940ae35dc2..f73664f433 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 9129563782..6679e636c7 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 1517dec406..d90f0aba44 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)); |