Skip to content

Commit 7c601dc

Browse files
bk2204gitster
authored andcommitted
connect: detect algorithm when fetching refs
If we're fetching refs, detect the hash algorithm and parse the refs using that algorithm. As mentioned in the documentation, if multiple versions of the object-format capability are provided, we use the first. No known implementation supports multiple algorithms now, but they may in the future. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 48bf141 commit 7c601dc

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

connect.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,25 @@ static void annotate_refs_with_symref_info(struct ref *ref)
220220

221221
static void process_capabilities(struct packet_reader *reader, int *linelen)
222222
{
223+
const char *feat_val;
224+
int feat_len;
223225
const char *line = reader->line;
224226
int nul_location = strlen(line);
225227
if (nul_location == *linelen)
226228
return;
227229
server_capabilities_v1 = xstrdup(line + nul_location + 1);
228230
*linelen = nul_location;
231+
232+
feat_val = server_feature_value("object-format", &feat_len);
233+
if (feat_val) {
234+
char *hash_name = xstrndup(feat_val, feat_len);
235+
int hash_algo = hash_algo_by_name(hash_name);
236+
if (hash_algo != GIT_HASH_UNKNOWN)
237+
reader->hash_algo = &hash_algos[hash_algo];
238+
free(hash_name);
239+
} else {
240+
reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
241+
}
229242
}
230243

231244
static int process_dummy_ref(const struct packet_reader *reader)
@@ -234,7 +247,7 @@ static int process_dummy_ref(const struct packet_reader *reader)
234247
struct object_id oid;
235248
const char *name;
236249

237-
if (parse_oid_hex(line, &oid, &name))
250+
if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
238251
return 0;
239252
if (*name != ' ')
240253
return 0;
@@ -258,7 +271,7 @@ static int process_ref(const struct packet_reader *reader, int len,
258271
struct object_id old_oid;
259272
const char *name;
260273

261-
if (parse_oid_hex(line, &old_oid, &name))
274+
if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
262275
return 0;
263276
if (*name != ' ')
264277
return 0;
@@ -270,7 +283,7 @@ static int process_ref(const struct packet_reader *reader, int len,
270283
die(_("protocol error: unexpected capabilities^{}"));
271284
} else if (check_ref(name, flags)) {
272285
struct ref *ref = alloc_ref(name);
273-
oidcpy(&ref->old_oid, &old_oid);
286+
memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
274287
**list = ref;
275288
*list = &ref->next;
276289
}
@@ -288,7 +301,7 @@ static int process_shallow(const struct packet_reader *reader, int len,
288301
if (!skip_prefix(line, "shallow ", &arg))
289302
return 0;
290303

291-
if (get_oid_hex(arg, &old_oid))
304+
if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
292305
die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
293306
if (!shallow_points)
294307
die(_("repository on the other end cannot be shallow"));

0 commit comments

Comments
 (0)