]> git.ipfire.org Git - thirdparty/git.git/commitdiff
serve: advertise object-format capability for protocol v2
authorbrian m. carlson <sandals@crustytoothpaste.net>
Mon, 25 May 2020 19:59:17 +0000 (19:59 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 May 2020 17:07:07 +0000 (10:07 -0700)
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>
connect.c
serve.c
t/t5701-git-serve.sh

index 66650ff2d3349213ac34a9600516d656063c97c9..2ada5b516186eb1743e3856dc9c4e6f28a1ed6be 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -460,6 +460,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
                        die(_("unknown object format '%s' specified by server"), hash_name);
                reader->hash_algo = &hash_algos[hash_algo];
                packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
+       } else {
+               reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
        }
 
        if (server_options && server_options->nr &&
diff --git a/serve.c b/serve.c
index 317256c1a493c4b2cc730a6212add4d1ef5696d4..7ab7807feffea42b83ce42051b07f4b051b1f3d3 100644 (file)
--- a/serve.c
+++ b/serve.c
@@ -22,6 +22,14 @@ static int agent_advertise(struct repository *r,
        return 1;
 }
 
+static int object_format_advertise(struct repository *r,
+                                  struct strbuf *value)
+{
+       if (value)
+               strbuf_addstr(value, r->hash_algo->name);
+       return 1;
+}
+
 struct protocol_capability {
        /*
         * The name of the capability.  The server uses this name when
@@ -57,6 +65,7 @@ static struct protocol_capability capabilities[] = {
        { "ls-refs", always_advertise, ls_refs },
        { "fetch", upload_pack_advertise, upload_pack_v2 },
        { "server-option", always_advertise, NULL },
+       { "object-format", object_format_advertise, NULL },
 };
 
 static void advertise_capabilities(void)
@@ -153,6 +162,22 @@ int has_capability(const struct argv_array *keys, const char *capability,
        return 0;
 }
 
+static void check_algorithm(struct repository *r, struct argv_array *keys)
+{
+       int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
+       const char *algo_name;
+
+       if (has_capability(keys, "object-format", &algo_name)) {
+               client = hash_algo_by_name(algo_name);
+               if (client == GIT_HASH_UNKNOWN)
+                       die("unknown object format '%s'", algo_name);
+       }
+
+       if (client != server)
+               die("mismatched object format: server %s; client %s\n",
+                   r->hash_algo->name, hash_algos[client].name);
+}
+
 enum request_state {
        PROCESS_REQUEST_KEYS,
        PROCESS_REQUEST_DONE,
@@ -223,6 +248,8 @@ static int process_request(void)
        if (!command)
                die("no command requested");
 
+       check_algorithm(the_repository, &keys);
+
        command->command(the_repository, &keys, &reader);
 
        argv_array_clear(&keys);
index ffb9613885904eaf474a726f6124032d13db9753..a1f5fdc9fdcf522706d4ad74230bff3887bbf9f9 100755 (executable)
@@ -5,12 +5,17 @@ test_description='test protocol v2 server commands'
 . ./test-lib.sh
 
 test_expect_success 'test capability advertisement' '
+       test_oid_cache <<-EOF &&
+       wrong_algo sha1:sha256
+       wrong_algo sha256:sha1
+       EOF
        cat >expect <<-EOF &&
        version 2
        agent=git/$(git version | cut -d" " -f3)
        ls-refs
        fetch=shallow
        server-option
+       object-format=$(test_oid algo)
        0000
        EOF
 
@@ -45,6 +50,7 @@ test_expect_success 'request invalid capability' '
 test_expect_success 'request with no command' '
        test-tool pkt-line pack >in <<-EOF &&
        agent=git/test
+       object-format=$(test_oid algo)
        0000
        EOF
        test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
@@ -54,6 +60,7 @@ test_expect_success 'request with no command' '
 test_expect_success 'request invalid command' '
        test-tool pkt-line pack >in <<-EOF &&
        command=foo
+       object-format=$(test_oid algo)
        agent=git/test
        0000
        EOF
@@ -61,6 +68,17 @@ test_expect_success 'request invalid command' '
        test_i18ngrep "invalid command" err
 '
 
+test_expect_success 'wrong object-format' '
+       test-tool pkt-line pack >in <<-EOF &&
+       command=fetch
+       agent=git/test
+       object-format=$(test_oid wrong_algo)
+       0000
+       EOF
+       test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+       test_i18ngrep "mismatched object format" err
+'
+
 # Test the basics of ls-refs
 #
 test_expect_success 'setup some refs and tags' '
@@ -74,6 +92,7 @@ test_expect_success 'setup some refs and tags' '
 test_expect_success 'basics of ls-refs' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        0000
        EOF
 
@@ -96,6 +115,7 @@ test_expect_success 'basics of ls-refs' '
 test_expect_success 'basic ref-prefixes' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        0001
        ref-prefix refs/heads/master
        ref-prefix refs/tags/one
@@ -116,6 +136,7 @@ test_expect_success 'basic ref-prefixes' '
 test_expect_success 'refs/heads prefix' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        0001
        ref-prefix refs/heads/
        0000
@@ -136,6 +157,7 @@ test_expect_success 'refs/heads prefix' '
 test_expect_success 'peel parameter' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        0001
        peel
        ref-prefix refs/tags/
@@ -157,6 +179,7 @@ test_expect_success 'peel parameter' '
 test_expect_success 'symrefs parameter' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        0001
        symrefs
        ref-prefix refs/heads/
@@ -178,6 +201,7 @@ test_expect_success 'symrefs parameter' '
 test_expect_success 'sending server-options' '
        test-tool pkt-line pack >in <<-EOF &&
        command=ls-refs
+       object-format=$(test_oid algo)
        server-option=hello
        server-option=world
        0001
@@ -200,6 +224,7 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
 
        test-tool pkt-line pack >in <<-EOF &&
        command=fetch
+       object-format=$(test_oid algo)
        0001
        this-is-not-a-command
        0000