git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Brandon Williams <bmwill@google.com>
To: git@vger.kernel.org
Cc: jonathantanmy@google.com, gitster@pobox.com, sbeller@google.com,
	jrnieder@gmail.com, Brandon Williams <bmwill@google.com>
Subject: [PATCH v6 8/8] fetch-pack: implement ref-in-want
Date: Wed, 27 Jun 2018 15:30:23 -0700	[thread overview]
Message-ID: <20180627223023.49659-9-bmwill@google.com> (raw)
In-Reply-To: <20180627223023.49659-1-bmwill@google.com>

Implement ref-in-want on the client side so that when a server supports
the "ref-in-want" feature, a client will send "want-ref" lines for each
reference the client wants to fetch.  This feature allows clients to
tolerate inconsistencies that exist when a remote repository's refs
change during the course of negotiation.

This allows a client to request to request a particular ref without
specifying the OID of the ref.  This means that instead of hitting an
error when a ref no longer points at the OID it did at the beginning of
negotiation, negotiation can continue and the value of that ref will be
sent at the termination of negotiation, just before a packfile is sent.

More information on the ref-in-want feature can be found in
Documentation/technical/protocol-v2.txt.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 fetch-pack.c                       |  38 +++++++-
 remote.c                           |   1 +
 remote.h                           |   1 +
 t/t5703-upload-pack-ref-in-want.sh | 149 +++++++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 3 deletions(-)

diff --git a/fetch-pack.c b/fetch-pack.c
index 73890b894..0b4a9f288 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1102,9 +1102,10 @@ static void add_shallow_requests(struct strbuf *req_buf,
 
 static void add_wants(const struct ref *wants, struct strbuf *req_buf)
 {
+	int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
+
 	for ( ; wants ; wants = wants->next) {
 		const struct object_id *remote = &wants->old_oid;
-		const char *remote_hex;
 		struct object *o;
 
 		/*
@@ -1122,8 +1123,10 @@ static void add_wants(const struct ref *wants, struct strbuf *req_buf)
 			continue;
 		}
 
-		remote_hex = oid_to_hex(remote);
-		packet_buf_write(req_buf, "want %s\n", remote_hex);
+		if (!use_ref_in_want || wants->exact_oid)
+			packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
+		else
+			packet_buf_write(req_buf, "want-ref %s\n", wants->name);
 	}
 }
 
@@ -1334,6 +1337,32 @@ static void receive_shallow_info(struct fetch_pack_args *args,
 	args->deepen = 1;
 }
 
+static void receive_wanted_refs(struct packet_reader *reader, struct ref *refs)
+{
+	process_section_header(reader, "wanted-refs", 0);
+	while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
+		struct object_id oid;
+		const char *end;
+		struct ref *r = NULL;
+
+		if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
+			die("expected wanted-ref, got '%s'", reader->line);
+
+		for (r = refs; r; r = r->next) {
+			if (!strcmp(end, r->name)) {
+				oidcpy(&r->old_oid, &oid);
+				break;
+			}
+		}
+
+		if (!r)
+			die("unexpected wanted-ref: '%s'", reader->line);
+	}
+
+	if (reader->status != PACKET_READ_DELIM)
+		die("error processing wanted refs: %d", reader->status);
+}
+
 enum fetch_state {
 	FETCH_CHECK_LOCAL = 0,
 	FETCH_SEND_REQUEST,
@@ -1408,6 +1437,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 			if (process_section_header(&reader, "shallow-info", 1))
 				receive_shallow_info(args, &reader);
 
+			if (process_section_header(&reader, "wanted-refs", 1))
+				receive_wanted_refs(&reader, ref);
+
 			/* get the pack */
 			process_section_header(&reader, "packfile", 0);
 			if (get_pack(args, fd, pack_lockfile))
diff --git a/remote.c b/remote.c
index abe80c139..2c2376fff 100644
--- a/remote.c
+++ b/remote.c
@@ -1735,6 +1735,7 @@ int get_fetch_map(const struct ref *remote_refs,
 		if (refspec->exact_sha1) {
 			ref_map = alloc_ref(name);
 			get_oid_hex(name, &ref_map->old_oid);
+			ref_map->exact_oid = 1;
 		} else {
 			ref_map = get_remote_ref(remote_refs, name);
 		}
diff --git a/remote.h b/remote.h
index 45ecc6cef..976292152 100644
--- a/remote.h
+++ b/remote.h
@@ -73,6 +73,7 @@ struct ref {
 		force:1,
 		forced_update:1,
 		expect_old_sha1:1,
+		exact_oid:1,
 		deletion:1;
 
 	enum {
diff --git a/t/t5703-upload-pack-ref-in-want.sh b/t/t5703-upload-pack-ref-in-want.sh
index 32527a59c..a73c55a47 100755
--- a/t/t5703-upload-pack-ref-in-want.sh
+++ b/t/t5703-upload-pack-ref-in-want.sh
@@ -211,6 +211,18 @@ test_expect_success 'server is initially ahead - no ref in want' '
 	grep "ERR upload-pack: not our ref" err
 '
 
+test_expect_success 'server is initially ahead - ref in want' '
+	git -C "$REPO" config uploadpack.allowRefInWant true &&
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	inconsistency master 1234567890123456789012345678901234567890 &&
+	git -C local fetch &&
+
+	git -C "$REPO" rev-parse --verify master >expected &&
+	git -C local rev-parse --verify refs/remotes/origin/master >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'server is initially behind - no ref in want' '
 	git -C "$REPO" config uploadpack.allowRefInWant false &&
 	rm -rf local &&
@@ -223,6 +235,143 @@ test_expect_success 'server is initially behind - no ref in want' '
 	test_cmp expected actual
 '
 
+test_expect_success 'server is initially behind - ref in want' '
+	git -C "$REPO" config uploadpack.allowRefInWant true &&
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	inconsistency master "master^" &&
+	git -C local fetch &&
+
+	git -C "$REPO" rev-parse --verify "master" >expected &&
+	git -C local rev-parse --verify refs/remotes/origin/master >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'server loses a ref - ref in want' '
+	git -C "$REPO" config uploadpack.allowRefInWant true &&
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	echo "s/master/raster/" >"$HTTPD_ROOT_PATH/one-time-sed" &&
+	test_must_fail git -C local fetch 2>err &&
+
+	grep "ERR unknown ref refs/heads/raster" err
+'
+
 stop_httpd
 
+REPO="$(pwd)/repo"
+LOCAL_PRISTINE="$(pwd)/local_pristine"
+
+# $REPO
+# c(o/foo) d(o/bar)
+#        \ /
+#         b   e(baz)  f(master)
+#          \__  |  __/
+#             \ | /
+#               a
+#
+# $LOCAL_PRISTINE
+#		s32(side)
+#		|
+#		.
+#		.
+#		|
+#		a(master)
+test_expect_success 'setup repos for fetching with ref-in-want tests' '
+	(
+		git init "$REPO" &&
+		cd "$REPO" &&
+		test_commit a &&
+
+		# Local repo with many commits (so that negotiation will take
+		# more than 1 request/response pair)
+		rm -rf "$LOCAL_PRISTINE" &&
+		git clone "file://$REPO" "$LOCAL_PRISTINE" &&
+		cd "$LOCAL_PRISTINE" &&
+		git checkout -b side &&
+		for i in $(seq 1 33); do test_commit s$i; done &&
+
+		# Add novel commits to upstream
+		git checkout master &&
+		cd "$REPO" &&
+		git checkout -b o/foo &&
+		test_commit b &&
+		test_commit c &&
+		git checkout -b o/bar b &&
+		test_commit d &&
+		git checkout -b baz a &&
+		test_commit e &&
+		git checkout master &&
+		test_commit f
+	) &&
+	git -C "$REPO" config uploadpack.allowRefInWant true &&
+	git -C "$LOCAL_PRISTINE" config protocol.version 2
+'
+
+test_expect_success 'fetching with exact OID' '
+	test_when_finished "rm -f log" &&
+
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
+		$(git -C "$REPO" rev-parse d):refs/heads/actual &&
+
+	git -C "$REPO" rev-parse "d" >expected &&
+	git -C local rev-parse refs/heads/actual >actual &&
+	test_cmp expected actual &&
+	grep "want $(git -C "$REPO" rev-parse d)" log
+'
+
+test_expect_success 'fetching multiple refs' '
+	test_when_finished "rm -f log" &&
+
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin master baz &&
+
+	git -C "$REPO" rev-parse "master" "baz" >expected &&
+	git -C local rev-parse refs/remotes/origin/master refs/remotes/origin/baz >actual &&
+	test_cmp expected actual &&
+	grep "want-ref refs/heads/master" log &&
+	grep "want-ref refs/heads/baz" log
+'
+
+test_expect_success 'fetching ref and exact OID' '
+	test_when_finished "rm -f log" &&
+
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
+		master $(git -C "$REPO" rev-parse b):refs/heads/actual &&
+
+	git -C "$REPO" rev-parse "master" "b" >expected &&
+	git -C local rev-parse refs/remotes/origin/master refs/heads/actual >actual &&
+	test_cmp expected actual &&
+	grep "want $(git -C "$REPO" rev-parse b)" log &&
+	grep "want-ref refs/heads/master" log
+'
+
+test_expect_success 'fetching with wildcard that does not match any refs' '
+	test_when_finished "rm -f log" &&
+
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
+	test_must_be_empty out
+'
+
+test_expect_success 'fetching with wildcard that matches multiple refs' '
+	test_when_finished "rm -f log" &&
+
+	rm -rf local &&
+	cp -r "$LOCAL_PRISTINE" local &&
+	GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
+
+	git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
+	git -C local rev-parse "o/foo" "o/bar" >actual &&
+	test_cmp expected actual &&
+	grep "want-ref refs/heads/o/foo" log &&
+	grep "want-ref refs/heads/o/bar" log
+'
+
 test_done
-- 
2.18.0.399.gad0ab374a1-goog


  parent reply	other threads:[~2018-06-27 22:30 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-05 17:51 [PATCH 0/8] ref-in-want Brandon Williams
2018-06-05 17:51 ` [PATCH 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-05 17:51 ` [PATCH 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-05 19:11   ` Ramsay Jones
2018-06-05 20:32   ` Ævar Arnfjörð Bjarmason
2018-06-06 21:32     ` Brandon Williams
2018-06-06 22:42       ` Ævar Arnfjörð Bjarmason
2018-06-06 22:45         ` Brandon Williams
2018-06-05 17:51 ` [PATCH 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-05 17:51 ` [PATCH 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-05 17:51 ` [PATCH 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-05 17:51 ` [PATCH 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-05 17:51 ` [PATCH 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-05 17:51 ` [PATCH 8/8] fetch-pack: implement ref-in-want Brandon Williams
2018-06-13 21:39 ` [PATCH v2 0/8] ref-in-want Brandon Williams
2018-06-13 21:39   ` [PATCH v2 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-14 18:09     ` Stefan Beller
2018-06-14 19:21       ` Brandon Williams
2018-06-13 21:39   ` [PATCH v2 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-14 18:40     ` Stefan Beller
2018-06-14 18:52       ` Brandon Williams
2018-06-15 21:08     ` Junio C Hamano
2018-06-15 21:14       ` Junio C Hamano
2018-06-19 18:50       ` Brandon Williams
2018-06-19 20:37         ` Junio C Hamano
2018-06-19 23:14           ` Brandon Williams
2018-06-21 16:38             ` Junio C Hamano
2018-06-13 21:39   ` [PATCH v2 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-14 19:23     ` Stefan Beller
2018-06-13 21:39   ` [PATCH v2 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-13 21:39   ` [PATCH v2 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-13 21:39   ` [PATCH v2 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-14 19:32     ` Stefan Beller
2018-06-13 21:39   ` [PATCH v2 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-14 19:42     ` Stefan Beller
2018-06-14 23:59     ` Jonathan Tan
2018-06-19 17:41       ` Brandon Williams
2018-06-13 21:39   ` [PATCH v2 8/8] fetch-pack: implement ref-in-want Brandon Williams
2018-06-14 19:56     ` Stefan Beller
2018-06-14 21:18       ` Brandon Williams
2018-06-22 22:29         ` Jonathan Nieder
2018-06-15 21:20   ` [PATCH v2 0/8] ref-in-want Junio C Hamano
2018-06-18 18:05     ` Brandon Williams
2018-06-20 21:32   ` [PATCH v3 " Brandon Williams
2018-06-20 21:32     ` [PATCH v3 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-22 21:12       ` Jonathan Nieder
2018-06-20 21:32     ` [PATCH v3 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-25 17:40       ` Jonathan Tan
2018-06-25 18:09       ` Jonathan Tan
2018-06-25 18:20         ` Brandon Williams
2018-06-20 21:32     ` [PATCH v3 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-20 21:32     ` [PATCH v3 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-25 17:45       ` Jonathan Tan
2018-06-20 21:32     ` [PATCH v3 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-22 21:26       ` Jonathan Nieder
2018-06-22 21:42       ` Jonathan Nieder
2018-06-20 21:32     ` [PATCH v3 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-20 21:32     ` [PATCH v3 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-25 18:03       ` Jonathan Tan
2018-06-25 18:18         ` Brandon Williams
2018-06-20 21:32     ` [PATCH v3 8/8] fetch-pack: implement ref-in-want Brandon Williams
2018-06-22 23:01       ` Jonathan Nieder
2018-06-25 18:08         ` Brandon Williams
2018-06-25 18:53     ` [PATCH v4 0/8] ref-in-want Brandon Williams
2018-06-25 18:53       ` [PATCH v4 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-25 18:53       ` [PATCH v4 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-25 18:53       ` [PATCH v4 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-25 22:27         ` Jonathan Tan
2018-06-25 18:53       ` [PATCH v4 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-25 18:53       ` [PATCH v4 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-25 18:53       ` [PATCH v4 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-25 22:36         ` Jonathan Tan
2018-06-25 18:53       ` [PATCH v4 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-25 18:53       ` [PATCH v4 8/8] fetch-pack: implement ref-in-want Brandon Williams
2018-06-25 23:03       ` [PATCH v4 0/8] ref-in-want Jonathan Tan
2018-06-26 20:54       ` [PATCH v5 " Brandon Williams
2018-06-26 20:54         ` [PATCH v5 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-26 20:54         ` [PATCH v5 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-26 21:25           ` Junio C Hamano
2018-06-27 18:05             ` Brandon Williams
2018-06-27 18:53               ` Junio C Hamano
2018-06-27 20:46                 ` Brandon Williams
2018-06-27 20:59                   ` Stefan Beller
2018-06-27 18:06             ` Jonathan Tan
2018-06-26 20:54         ` [PATCH v5 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-26 21:34           ` Junio C Hamano
2018-06-27 18:09             ` Brandon Williams
2018-06-27 17:58           ` Jonathan Tan
2018-06-26 20:54         ` [PATCH v5 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-26 20:54         ` [PATCH v5 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-26 20:54         ` [PATCH v5 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-26 21:40           ` Junio C Hamano
2018-06-26 20:54         ` [PATCH v5 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-26 21:42           ` Junio C Hamano
2018-06-27 18:15             ` Brandon Williams
2018-06-26 20:54         ` [PATCH v5 8/8] fetch-pack: implement ref-in-want Brandon Williams
2018-06-27 18:09           ` Jonathan Tan
2018-06-27 18:18             ` Brandon Williams
2018-06-27 22:30         ` [PATCH v6 0/8] ref-in-want Brandon Williams
2018-06-27 22:30           ` [PATCH v6 1/8] test-pkt-line: add unpack-sideband subcommand Brandon Williams
2018-06-27 22:30           ` [PATCH v6 2/8] upload-pack: implement ref-in-want Brandon Williams
2018-06-27 22:30           ` [PATCH v6 3/8] upload-pack: test negotiation with changing repository Brandon Williams
2018-06-27 22:30           ` [PATCH v6 4/8] fetch: refactor the population of peer ref OIDs Brandon Williams
2018-06-27 22:30           ` [PATCH v6 5/8] fetch: refactor fetch_refs into two functions Brandon Williams
2018-06-27 22:30           ` [PATCH v6 6/8] fetch: refactor to make function args narrower Brandon Williams
2018-06-27 22:30           ` [PATCH v6 7/8] fetch-pack: put shallow info in output parameter Brandon Williams
2018-06-27 22:30           ` Brandon Williams [this message]
2018-07-22  9:20             ` [PATCH v6 8/8] fetch-pack: implement ref-in-want Duy Nguyen
2018-07-23 17:53               ` Brandon Williams
2018-07-23 18:13                 ` Duy Nguyen
2018-07-23 21:28                   ` Jonathan Nieder
2018-07-23 17:56               ` [PATCH] fetch-pack: mark die strings for translation Brandon Williams
2018-07-23 18:14                 ` Stefan Beller
2018-07-23 21:29                 ` Jonathan Nieder
2018-07-23 22:57                 ` Junio C Hamano
2018-07-23 22:59                   ` Junio C Hamano
2018-07-23 23:00                     ` Brandon Williams
2018-06-15 19:04 ` [PATCH 0/8] ref-in-want Jonathan Tan
2018-06-19 17:32   ` Brandon Williams
2018-06-19 19:23     ` Jonathan Tan
2018-06-19 23:16   ` Brandon Williams
2018-06-19 23:38     ` Jonathan Tan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180627223023.49659-9-bmwill@google.com \
    --to=bmwill@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jonathantanmy@google.com \
    --cc=jrnieder@gmail.com \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).