Skip to content

Commit 9de0dd3

Browse files
bk2204gitster
authored andcommitted
serve: advertise object-format capability for protocol v2
In order to communicate the protocol supported by the server side, add support for advertising the object-format capability. We check that the client side sends us an identical algorithm if it sends us its own object-format capability, and assume it speaks SHA-1 if not. In the test, when we're using an algorithm other than SHA-1, we need to specify the algorithm in use so we don't get a failure with an "unknown format" message. Add a test that we handle a mismatched algorithm. Remove the test_oid_init call since it's no longer necessary. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab67235 commit 9de0dd3

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

connect.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
460460
die(_("unknown object format '%s' specified by server"), hash_name);
461461
reader->hash_algo = &hash_algos[hash_algo];
462462
packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
463+
} else {
464+
reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
463465
}
464466

465467
if (server_options && server_options->nr &&

serve.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ static int agent_advertise(struct repository *r,
2222
return 1;
2323
}
2424

25+
static int object_format_advertise(struct repository *r,
26+
struct strbuf *value)
27+
{
28+
if (value)
29+
strbuf_addstr(value, r->hash_algo->name);
30+
return 1;
31+
}
32+
2533
struct protocol_capability {
2634
/*
2735
* The name of the capability. The server uses this name when
@@ -57,6 +65,7 @@ static struct protocol_capability capabilities[] = {
5765
{ "ls-refs", always_advertise, ls_refs },
5866
{ "fetch", upload_pack_advertise, upload_pack_v2 },
5967
{ "server-option", always_advertise, NULL },
68+
{ "object-format", object_format_advertise, NULL },
6069
};
6170

6271
static void advertise_capabilities(void)
@@ -153,6 +162,22 @@ int has_capability(const struct argv_array *keys, const char *capability,
153162
return 0;
154163
}
155164

165+
static void check_algorithm(struct repository *r, struct argv_array *keys)
166+
{
167+
int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
168+
const char *algo_name;
169+
170+
if (has_capability(keys, "object-format", &algo_name)) {
171+
client = hash_algo_by_name(algo_name);
172+
if (client == GIT_HASH_UNKNOWN)
173+
die("unknown object format '%s'", algo_name);
174+
}
175+
176+
if (client != server)
177+
die("mismatched object format: server %s; client %s\n",
178+
r->hash_algo->name, hash_algos[client].name);
179+
}
180+
156181
enum request_state {
157182
PROCESS_REQUEST_KEYS,
158183
PROCESS_REQUEST_DONE,
@@ -223,6 +248,8 @@ static int process_request(void)
223248
if (!command)
224249
die("no command requested");
225250

251+
check_algorithm(the_repository, &keys);
252+
226253
command->command(the_repository, &keys, &reader);
227254

228255
argv_array_clear(&keys);

t/t5701-git-serve.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ test_description='test protocol v2 server commands'
55
. ./test-lib.sh
66

77
test_expect_success 'test capability advertisement' '
8+
test_oid_cache <<-EOF &&
9+
wrong_algo sha1:sha256
10+
wrong_algo sha256:sha1
11+
EOF
812
cat >expect <<-EOF &&
913
version 2
1014
agent=git/$(git version | cut -d" " -f3)
1115
ls-refs
1216
fetch=shallow
1317
server-option
18+
object-format=$(test_oid algo)
1419
0000
1520
EOF
1621
@@ -45,6 +50,7 @@ test_expect_success 'request invalid capability' '
4550
test_expect_success 'request with no command' '
4651
test-tool pkt-line pack >in <<-EOF &&
4752
agent=git/test
53+
object-format=$(test_oid algo)
4854
0000
4955
EOF
5056
test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
@@ -54,13 +60,25 @@ test_expect_success 'request with no command' '
5460
test_expect_success 'request invalid command' '
5561
test-tool pkt-line pack >in <<-EOF &&
5662
command=foo
63+
object-format=$(test_oid algo)
5764
agent=git/test
5865
0000
5966
EOF
6067
test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
6168
test_i18ngrep "invalid command" err
6269
'
6370

71+
test_expect_success 'wrong object-format' '
72+
test-tool pkt-line pack >in <<-EOF &&
73+
command=fetch
74+
agent=git/test
75+
object-format=$(test_oid wrong_algo)
76+
0000
77+
EOF
78+
test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
79+
test_i18ngrep "mismatched object format" err
80+
'
81+
6482
# Test the basics of ls-refs
6583
#
6684
test_expect_success 'setup some refs and tags' '
@@ -74,6 +92,7 @@ test_expect_success 'setup some refs and tags' '
7492
test_expect_success 'basics of ls-refs' '
7593
test-tool pkt-line pack >in <<-EOF &&
7694
command=ls-refs
95+
object-format=$(test_oid algo)
7796
0000
7897
EOF
7998
@@ -96,6 +115,7 @@ test_expect_success 'basics of ls-refs' '
96115
test_expect_success 'basic ref-prefixes' '
97116
test-tool pkt-line pack >in <<-EOF &&
98117
command=ls-refs
118+
object-format=$(test_oid algo)
99119
0001
100120
ref-prefix refs/heads/master
101121
ref-prefix refs/tags/one
@@ -116,6 +136,7 @@ test_expect_success 'basic ref-prefixes' '
116136
test_expect_success 'refs/heads prefix' '
117137
test-tool pkt-line pack >in <<-EOF &&
118138
command=ls-refs
139+
object-format=$(test_oid algo)
119140
0001
120141
ref-prefix refs/heads/
121142
0000
@@ -136,6 +157,7 @@ test_expect_success 'refs/heads prefix' '
136157
test_expect_success 'peel parameter' '
137158
test-tool pkt-line pack >in <<-EOF &&
138159
command=ls-refs
160+
object-format=$(test_oid algo)
139161
0001
140162
peel
141163
ref-prefix refs/tags/
@@ -157,6 +179,7 @@ test_expect_success 'peel parameter' '
157179
test_expect_success 'symrefs parameter' '
158180
test-tool pkt-line pack >in <<-EOF &&
159181
command=ls-refs
182+
object-format=$(test_oid algo)
160183
0001
161184
symrefs
162185
ref-prefix refs/heads/
@@ -178,6 +201,7 @@ test_expect_success 'symrefs parameter' '
178201
test_expect_success 'sending server-options' '
179202
test-tool pkt-line pack >in <<-EOF &&
180203
command=ls-refs
204+
object-format=$(test_oid algo)
181205
server-option=hello
182206
server-option=world
183207
0001
@@ -200,6 +224,7 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
200224
201225
test-tool pkt-line pack >in <<-EOF &&
202226
command=fetch
227+
object-format=$(test_oid algo)
203228
0001
204229
this-is-not-a-command
205230
0000

0 commit comments

Comments
 (0)