Use common capabilities Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
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
diff --git a/src/pkt.c b/src/pkt.c
index fa0ee77..12f1478 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -269,13 +269,35 @@ int git_pkt_send_flush(int s)
return gitno_send(s, flush, STRLEN(flush), 0);
}
+static int send_want_with_caps(git_remote_head *head, git_transport_caps *caps, int fd)
+{
+ char capstr[20]; /* Longer than we need */
+ char oid[GIT_OID_HEXSZ +1] = {0}, *cmd;
+ int error, len;
+
+ if (caps->ofs_delta)
+ strcpy(capstr, GIT_CAP_OFS_DELTA);
+
+ len = STRLEN("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ + strlen(capstr) + 1 /* LF */;
+ cmd = git__malloc(len + 1);
+ if (cmd == NULL)
+ return GIT_ENOMEM;
+
+ git_oid_fmt(oid, &head->oid);
+ memset(cmd, 0x0, len + 1);
+ snprintf(cmd, len + 1, "%04xwant %s%c%s\n", len, oid, 0, capstr);
+ error = gitno_send(fd, cmd, len, 0);
+ free(cmd);
+ return error;
+}
+
/*
* All "want" packets have the same length and format, so what we do
* is overwrite the OID each time.
*/
#define WANT_PREFIX "0032want "
-int git_pkt_send_wants(git_headarray *refs, int fd)
+int git_pkt_send_wants(git_headarray *refs, git_transport_caps *caps, int fd)
{
unsigned int i;
int ret = GIT_SUCCESS;
@@ -286,10 +308,18 @@ int git_pkt_send_wants(git_headarray *refs, int fd)
buf[sizeof(buf) - 2] = '\n';
buf[sizeof(buf) - 1] = '\0';
- for (i = 0; i < refs->len; ++i) {
+ if (refs->len > 0 && caps->common) {
+ /* Some capabilities are active, so we need to send what we support */
+ send_want_with_caps(refs->heads[0], caps, fd);
+ i = 1;
+ } else {
+ i = 0;
+ }
+
+ for (; i < refs->len; ++i) {
head = refs->heads[i];
if (head->type != GIT_WHN_WANT)
- continue;
+ continue; /* FIXME: return? refs shouldn't have any other type */
git_oid_fmt(buf + STRLEN(WANT_PREFIX), &head->oid);
gitno_send(fd, buf, STRLEN(buf), 0);
diff --git a/src/pkt.h b/src/pkt.h
index 3a8fac5..1c6a206 100644
--- a/src/pkt.h
+++ b/src/pkt.h
@@ -27,6 +27,7 @@
#define INCLUDE_pkt_h__
#include "common.h"
+#include "transport.h"
#include "git2/net.h"
enum git_pkt_type {
@@ -76,7 +77,7 @@ typedef struct {
int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t len);
int git_pkt_send_flush(int s);
int git_pkt_send_done(int s);
-int git_pkt_send_wants(git_headarray *refs, int fd);
+int git_pkt_send_wants(git_headarray *refs, git_transport_caps *caps, int fd);
int git_pkt_send_have(git_oid *oid, int fd);
void git_pkt_free(git_pkt *pkt);
diff --git a/src/transport.h b/src/transport.h
index 0e08697..e809734 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -5,6 +5,13 @@
#include "git2/net.h"
#include "vector.h"
+#define GIT_CAP_OFS_DELTA "ofs-delta"
+
+typedef struct git_transport_caps {
+ int common:1,
+ ofs_delta:1;
+} git_transport_caps;
+
/*
* A day in the life of a network operation
* ========================================
diff --git a/src/transport_git.c b/src/transport_git.c
index 46678a2..4a10f17 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -41,6 +41,7 @@ typedef struct {
int socket;
git_vector refs;
git_remote_head **heads;
+ git_transport_caps caps;
} transport_git;
/*
@@ -218,6 +219,36 @@ static int store_refs(transport_git *t)
return error;
}
+static int detect_caps(transport_git *t)
+{
+ git_vector *refs = &t->refs;
+ git_pkt_ref *pkt;
+ git_transport_caps *caps = &t->caps;
+ const char *ptr;
+
+ pkt = git_vector_get(refs, 0);
+ /* No refs or capabilites, odd but not a problem */
+ if (pkt == NULL || pkt->capabilities == NULL)
+ return GIT_SUCCESS;
+
+ ptr = pkt->capabilities;
+ while (ptr != NULL && *ptr != '\0') {
+ if (*ptr == ' ')
+ ptr++;
+
+ if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
+ caps->common = caps->ofs_delta = 1;
+ ptr += STRLEN(GIT_CAP_OFS_DELTA);
+ continue;
+ }
+
+ /* We don't know this capability, so skip it */
+ ptr = strchr(ptr, ' ');
+ }
+
+ return GIT_SUCCESS;
+}
+
/*
* Since this is a network connection, we need to parse and store the
* pkt-lines at this stage and keep them there.
@@ -242,6 +273,10 @@ static int git_connect(git_transport *transport, int direction)
t->parent.connected = 1;
error = store_refs(t);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ error = detect_caps(t);
cleanup:
if (error < GIT_SUCCESS) {
@@ -280,7 +315,7 @@ static int git_send_wants(git_transport *transport, git_headarray *array)
{
transport_git *t = (transport_git *) transport;
- return git_pkt_send_wants(array, t->socket);
+ return git_pkt_send_wants(array, &t->caps, t->socket);
}
static int git_send_have(git_transport *transport, git_oid *oid)