Merge pull request #1410 from phkelley/push_sideband Advertise and support side-band-64k when calling receive-pack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
diff --git a/src/transports/smart.h b/src/transports/smart.h
index a9e894b..c52401a 100644
--- a/src/transports/smart.h
+++ b/src/transports/smart.h
@@ -88,6 +88,7 @@ typedef git_pkt_data git_pkt_progress;
typedef struct {
enum git_pkt_type type;
+ int len;
char error[GIT_FLEX_ARRAY];
} git_pkt_err;
diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c
index 51edd91..99da375 100644
--- a/src/transports/smart_pkt.c
+++ b/src/transports/smart_pkt.c
@@ -122,6 +122,7 @@ static int err_pkt(git_pkt **out, const char *line, size_t len)
GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_ERR;
+ pkt->len = (int)len;
memcpy(pkt->error, line, len);
pkt->error[len] = '\0';
@@ -166,6 +167,25 @@ static int progress_pkt(git_pkt **out, const char *line, size_t len)
return 0;
}
+static int sideband_error_pkt(git_pkt **out, const char *line, size_t len)
+{
+ git_pkt_err *pkt;
+
+ line++;
+ len--;
+ pkt = git__malloc(sizeof(git_pkt_err) + len + 1);
+ GITERR_CHECK_ALLOC(pkt);
+
+ pkt->type = GIT_PKT_ERR;
+ pkt->len = (int)len;
+ memcpy(pkt->error, line, len);
+ pkt->error[len] = '\0';
+
+ *out = (git_pkt *)pkt;
+
+ return 0;
+}
+
/*
* Parse an other-ref line.
*/
@@ -380,6 +400,8 @@ int git_pkt_parse_line(
ret = data_pkt(head, line, len);
else if (*line == GIT_SIDE_BAND_PROGRESS)
ret = progress_pkt(head, line, len);
+ else if (*line == GIT_SIDE_BAND_ERROR)
+ ret = sideband_error_pkt(head, line, len);
else if (!git__prefixcmp(line, "ACK"))
ret = ack_pkt(head, line, len);
else if (!git__prefixcmp(line, "NAK"))
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 75494b2..c653ad5 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -536,7 +536,8 @@ static int gen_pktline(git_buf *buf, git_push *push)
if (i == 0) {
++len; /* '\0' */
if (push->report_status)
- len += strlen(GIT_CAP_REPORT_STATUS);
+ len += strlen(GIT_CAP_REPORT_STATUS) + 1;
+ len += strlen(GIT_CAP_SIDE_BAND_64K) + 1;
}
git_oid_fmt(old_id, &spec->roid);
@@ -546,8 +547,13 @@ static int gen_pktline(git_buf *buf, git_push *push)
if (i == 0) {
git_buf_putc(buf, '\0');
- if (push->report_status)
+ /* Core git always starts their capabilities string with a space */
+ if (push->report_status) {
+ git_buf_putc(buf, ' ');
git_buf_printf(buf, GIT_CAP_REPORT_STATUS);
+ }
+ git_buf_putc(buf, ' ');
+ git_buf_printf(buf, GIT_CAP_SIDE_BAND_64K);
}
git_buf_putc(buf, '\n');
@@ -557,6 +563,74 @@ static int gen_pktline(git_buf *buf, git_push *push)
return git_buf_oom(buf) ? -1 : 0;
}
+static int add_push_report_pkt(git_push *push, git_pkt *pkt)
+{
+ push_status *status;
+
+ switch (pkt->type) {
+ case GIT_PKT_OK:
+ status = git__malloc(sizeof(push_status));
+ GITERR_CHECK_ALLOC(status);
+ status->msg = NULL;
+ status->ref = git__strdup(((git_pkt_ok *)pkt)->ref);
+ if (!status->ref ||
+ git_vector_insert(&push->status, status) < 0) {
+ git_push_status_free(status);
+ return -1;
+ }
+ break;
+ case GIT_PKT_NG:
+ status = git__calloc(sizeof(push_status), 1);
+ GITERR_CHECK_ALLOC(status);
+ status->ref = git__strdup(((git_pkt_ng *)pkt)->ref);
+ status->msg = git__strdup(((git_pkt_ng *)pkt)->msg);
+ if (!status->ref || !status->msg ||
+ git_vector_insert(&push->status, status) < 0) {
+ git_push_status_free(status);
+ return -1;
+ }
+ break;
+ case GIT_PKT_UNPACK:
+ push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok;
+ break;
+ case GIT_PKT_FLUSH:
+ return GIT_ITEROVER;
+ default:
+ giterr_set(GITERR_NET, "report-status: protocol error");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt)
+{
+ git_pkt *pkt;
+ const char *line = data_pkt->data, *line_end;
+ size_t line_len = data_pkt->len;
+ int error;
+
+ while (line_len > 0) {
+ error = git_pkt_parse_line(&pkt, line, &line_end, line_len);
+
+ if (error < 0)
+ return error;
+
+ /* Advance in the buffer */
+ line_len -= (line_end - line);
+ line = line_end;
+
+ error = add_push_report_pkt(push, pkt);
+
+ git_pkt_free(pkt);
+
+ if (error < 0 && error != GIT_ITEROVER)
+ return error;
+ }
+
+ return 0;
+}
+
static int parse_report(gitno_buffer *buf, git_push *push)
{
git_pkt *pkt;
@@ -586,46 +660,33 @@ static int parse_report(gitno_buffer *buf, git_push *push)
gitno_consume(buf, line_end);
- if (pkt->type == GIT_PKT_OK) {
- push_status *status = git__malloc(sizeof(push_status));
- GITERR_CHECK_ALLOC(status);
- status->ref = git__strdup(((git_pkt_ok *)pkt)->ref);
- status->msg = NULL;
- git_pkt_free(pkt);
- if (git_vector_insert(&push->status, status) < 0) {
- git__free(status);
- return -1;
- }
- continue;
- }
+ error = 0;
- if (pkt->type == GIT_PKT_NG) {
- push_status *status = git__malloc(sizeof(push_status));
- GITERR_CHECK_ALLOC(status);
- status->ref = git__strdup(((git_pkt_ng *)pkt)->ref);
- status->msg = git__strdup(((git_pkt_ng *)pkt)->msg);
- git_pkt_free(pkt);
- if (git_vector_insert(&push->status, status) < 0) {
- git__free(status);
- return -1;
- }
- continue;
+ switch (pkt->type) {
+ case GIT_PKT_DATA:
+ /* This is a sideband packet which contains other packets */
+ error = add_push_report_sideband_pkt(push, (git_pkt_data *)pkt);
+ break;
+ case GIT_PKT_ERR:
+ giterr_set(GITERR_NET, "report-status: Error reported: %s",
+ ((git_pkt_err *)pkt)->error);
+ error = -1;
+ break;
+ case GIT_PKT_PROGRESS:
+ break;
+ default:
+ error = add_push_report_pkt(push, pkt);
+ break;
}
- if (pkt->type == GIT_PKT_UNPACK) {
- push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok;
- git_pkt_free(pkt);
- continue;
- }
+ git_pkt_free(pkt);
- if (pkt->type == GIT_PKT_FLUSH) {
- git_pkt_free(pkt);
+ /* add_push_report_pkt returns GIT_ITEROVER when it receives a flush */
+ if (error == GIT_ITEROVER)
return 0;
- }
- git_pkt_free(pkt);
- giterr_set(GITERR_NET, "report-status: protocol error");
- return -1;
+ if (error < 0)
+ return error;
}
}