summaryrefslogtreecommitdiff
path: root/pkt-line.c
diff options
context:
space:
mode:
Diffstat (limited to 'pkt-line.c')
-rw-r--r--pkt-line.c45
1 files changed, 23 insertions, 22 deletions
diff --git a/pkt-line.c b/pkt-line.c
index bc63b3b..187a229 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -3,7 +3,7 @@
char packet_buffer[LARGE_PACKET_MAX];
static const char *packet_trace_prefix = "git";
-static const char trace_key[] = "GIT_TRACE_PACKET";
+static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
void packet_trace_identity(const char *prog)
{
@@ -15,7 +15,7 @@ static void packet_trace(const char *buf, unsigned int len, int write)
int i;
struct strbuf out;
- if (!trace_want(trace_key))
+ if (!trace_want(&trace_packet))
return;
/* +32 is just a guess for header + quoting */
@@ -27,7 +27,7 @@ static void packet_trace(const char *buf, unsigned int len, int write)
if ((len >= 4 && starts_with(buf, "PACK")) ||
(len >= 5 && starts_with(buf+1, "PACK"))) {
strbuf_addstr(&out, "PACK ...");
- unsetenv(trace_key);
+ trace_disable(&trace_packet);
}
else {
/* XXX we should really handle printable utf8 */
@@ -43,7 +43,7 @@ static void packet_trace(const char *buf, unsigned int len, int write)
}
strbuf_addch(&out, '\n');
- trace_strbuf(trace_key, &out);
+ trace_strbuf(&trace_packet, &out);
strbuf_release(&out);
}
@@ -64,44 +64,45 @@ void packet_buf_flush(struct strbuf *buf)
}
#define hex(a) (hexchar[(a) & 15])
-static char buffer[1000];
-static unsigned format_packet(const char *fmt, va_list args)
+static void format_packet(struct strbuf *out, const char *fmt, va_list args)
{
static char hexchar[] = "0123456789abcdef";
- unsigned n;
+ size_t orig_len, n;
- n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
- if (n >= sizeof(buffer)-4)
+ orig_len = out->len;
+ strbuf_addstr(out, "0000");
+ strbuf_vaddf(out, fmt, args);
+ n = out->len - orig_len;
+
+ if (n > LARGE_PACKET_MAX)
die("protocol error: impossibly long line");
- n += 4;
- buffer[0] = hex(n >> 12);
- buffer[1] = hex(n >> 8);
- buffer[2] = hex(n >> 4);
- buffer[3] = hex(n);
- packet_trace(buffer+4, n-4, 1);
- return n;
+
+ out->buf[orig_len + 0] = hex(n >> 12);
+ out->buf[orig_len + 1] = hex(n >> 8);
+ out->buf[orig_len + 2] = hex(n >> 4);
+ out->buf[orig_len + 3] = hex(n);
+ packet_trace(out->buf + orig_len + 4, n - 4, 1);
}
void packet_write(int fd, const char *fmt, ...)
{
+ static struct strbuf buf = STRBUF_INIT;
va_list args;
- unsigned n;
+ strbuf_reset(&buf);
va_start(args, fmt);
- n = format_packet(fmt, args);
+ format_packet(&buf, fmt, args);
va_end(args);
- write_or_die(fd, buffer, n);
+ write_or_die(fd, buf.buf, buf.len);
}
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
{
va_list args;
- unsigned n;
va_start(args, fmt);
- n = format_packet(fmt, args);
+ format_packet(buf, fmt, args);
va_end(args);
- strbuf_add(buf, buffer, n);
}
static int get_packet_data(int fd, char **src_buf, size_t *src_size,