@matheusbsbs / matheus-bagliano / commits / 9de0dd361c
Fork 0

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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 25, 2020 at 19:59 UTC 9de0dd361c9ea2ca6eca14a7dd43fe11d170a253
3 files changed +54
connect.c
+2
@@ -460,6 +460,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
460 die(_("unknown object format '%s' specified by server"), hash_name);
461 reader->hash_algo = &hash_algos[hash_algo];
462 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
463 + } else {
464 + reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
465 }
466
467 if (server_options && server_options->nr &&
serve.c
+27
@@ -22,6 +22,14 @@ static int agent_advertise(struct repository *r,
22 return 1;
23 }
24
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 +
33 struct protocol_capability {
34 /*
35 * The name of the capability. The server uses this name when
@@ -57,6 +65,7 @@ static struct protocol_capability capabilities[] = {
65 { "ls-refs", always_advertise, ls_refs },
66 { "fetch", upload_pack_advertise, upload_pack_v2 },
67 { "server-option", always_advertise, NULL },
68 + { "object-format", object_format_advertise, NULL },
69 };
70
71 static void advertise_capabilities(void)
@@ -153,6 +162,22 @@ int has_capability(const struct argv_array *keys, const char *capability,
162 return 0;
163 }
164
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 +
181 enum request_state {
182 PROCESS_REQUEST_KEYS,
183 PROCESS_REQUEST_DONE,
@@ -223,6 +248,8 @@ static int process_request(void)
248 if (!command)
249 die("no command requested");
250
251 + check_algorithm(the_repository, &keys);
252 +
253 command->command(the_repository, &keys, &reader);
254
255 argv_array_clear(&keys);
t/t5701-git-serve.sh
+25
@@ -5,12 +5,17 @@ test_description='test protocol v2 server commands'
5 . ./test-lib.sh
6
7 test_expect_success 'test capability advertisement' '
8 + test_oid_cache <<-EOF &&
9 + wrong_algo sha1:sha256
10 + wrong_algo sha256:sha1
11 + EOF
12 cat >expect <<-EOF &&
13 version 2
14 agent=git/$(git version | cut -d" " -f3)
15 ls-refs
16 fetch=shallow
17 server-option
18 + object-format=$(test_oid algo)
19 0000
20 EOF
21
@@ -45,6 +50,7 @@ test_expect_success 'request invalid capability' '
50 test_expect_success 'request with no command' '
51 test-tool pkt-line pack >in <<-EOF &&
52 agent=git/test
53 + object-format=$(test_oid algo)
54 0000
55 EOF
56 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
@@ -54,6 +60,7 @@ test_expect_success 'request with no command' '
60 test_expect_success 'request invalid command' '
61 test-tool pkt-line pack >in <<-EOF &&
62 command=foo
63 + object-format=$(test_oid algo)
64 agent=git/test
65 0000
66 EOF
@@ -61,6 +68,17 @@ test_expect_success 'request invalid command' '
68 test_i18ngrep "invalid command" err
69 '
70
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 +
82 # Test the basics of ls-refs
83 #
84 test_expect_success 'setup some refs and tags' '
@@ -74,6 +92,7 @@ test_expect_success 'setup some refs and tags' '
92 test_expect_success 'basics of ls-refs' '
93 test-tool pkt-line pack >in <<-EOF &&
94 command=ls-refs
95 + object-format=$(test_oid algo)
96 0000
97 EOF
98
@@ -96,6 +115,7 @@ test_expect_success 'basics of ls-refs' '
115 test_expect_success 'basic ref-prefixes' '
116 test-tool pkt-line pack >in <<-EOF &&
117 command=ls-refs
118 + object-format=$(test_oid algo)
119 0001
120 ref-prefix refs/heads/master
121 ref-prefix refs/tags/one
@@ -116,6 +136,7 @@ test_expect_success 'basic ref-prefixes' '
136 test_expect_success 'refs/heads prefix' '
137 test-tool pkt-line pack >in <<-EOF &&
138 command=ls-refs
139 + object-format=$(test_oid algo)
140 0001
141 ref-prefix refs/heads/
142 0000
@@ -136,6 +157,7 @@ test_expect_success 'refs/heads prefix' '
157 test_expect_success 'peel parameter' '
158 test-tool pkt-line pack >in <<-EOF &&
159 command=ls-refs
160 + object-format=$(test_oid algo)
161 0001
162 peel
163 ref-prefix refs/tags/
@@ -157,6 +179,7 @@ test_expect_success 'peel parameter' '
179 test_expect_success 'symrefs parameter' '
180 test-tool pkt-line pack >in <<-EOF &&
181 command=ls-refs
182 + object-format=$(test_oid algo)
183 0001
184 symrefs
185 ref-prefix refs/heads/
@@ -178,6 +201,7 @@ test_expect_success 'symrefs parameter' '
201 test_expect_success 'sending server-options' '
202 test-tool pkt-line pack >in <<-EOF &&
203 command=ls-refs
204 + object-format=$(test_oid algo)
205 server-option=hello
206 server-option=world
207 0001
@@ -200,6 +224,7 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
224
225 test-tool pkt-line pack >in <<-EOF &&
226 command=fetch
227 + object-format=$(test_oid algo)
228 0001
229 this-is-not-a-command
230 0000