git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: git@vger.kernel.org
Cc: "Derrick Stolee" <stolee@gmail.com>,
	"Thomas Gummerer" <t.gummerer@gmail.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>
Subject: [PATCH v2 4/6] read-cache: look for shared index files next to the index, too
Date: Thu, 26 Aug 2021 23:00:02 +0200	[thread overview]
Message-ID: <20210826210004.672860-5-szeder.dev@gmail.com> (raw)
In-Reply-To: <20210826210004.672860-1-szeder.dev@gmail.com>

When reading a split index git always looks for its referenced shared
base index in the gitdir of the current repository, even when reading
an alternate index specified via GIT_INDEX_FILE, and even when that
alternate index file is the "main" '.git/index' file of an other
repository.  However, if that split index and its referenced shared
index files were written by a git command running entirely in that
other repository, then, naturally, the shared index file is written to
that other repository's gitdir.  Consequently, a git command
attempting to read that shared index file while running in a different
repository won't be able find it and will error out.

I'm not sure in what use case it is necessary to read the index of one
repository by a git command running in a different repository, but it
is certainly possible to do so, and in fact the test 'bare repository:
check that --cached honors index' in 't0003-attributes.sh' does
exactly that.  If GIT_TEST_SPLIT_INDEX=1 were to split the index in
just the right moment [1], then this test would indeed fail, because
the referenced shared index file could not be found.

Let's look for the referenced shared index file not only in the gitdir
of the current directory, but, if the shared index is not there, right
next to the split index as well.

[1] We haven't seen this issue trigger a failure in t0003 yet,
    because:

      - While GIT_TEST_SPLIT_INDEX=1 is supposed to trigger index
        splitting randomly, the first index write has always been
        deterministic and it has never split the index.

      - That alternate index file in the other repository is written
        only once in the entire test script, so it's never split.

    However, the next patch will fix GIT_TEST_SPLIT_INDEX, and while
    doing so it will slightly change its behavior to always split the
    index already on the first index write, and t0003 would always
    fail without this patch.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 read-cache.c           | 14 +++++++++++++-
 t/t1700-split-index.sh | 23 +++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/read-cache.c b/read-cache.c
index 9048ef9e90..fbd59886a3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2391,9 +2391,21 @@ int read_index_from(struct index_state *istate, const char *path,
 	base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
 	trace2_region_enter_printf("index", "shared/do_read_index",
 				   the_repository, "%s", base_path);
-	ret = do_read_index(split_index->base, base_path, 1);
+	ret = do_read_index(split_index->base, base_path, 0);
 	trace2_region_leave_printf("index", "shared/do_read_index",
 				   the_repository, "%s", base_path);
+	if (!ret) {
+		char *path_copy = xstrdup(path);
+		const char *base_path2 = xstrfmt("%s/sharedindex.%s",
+						 dirname(path_copy),
+						 base_oid_hex);
+		free(path_copy);
+		trace2_region_enter_printf("index", "shared/do_read_index",
+					   the_repository, "%s", base_path2);
+		ret = do_read_index(split_index->base, base_path2, 1);
+		trace2_region_leave_printf("index", "shared/do_read_index",
+					   the_repository, "%s", base_path2);
+	}
 	if (!oideq(&split_index->base_oid, &split_index->base->oid))
 		die(_("broken index, expect %s in %s, got %s"),
 		    base_oid_hex, base_path,
diff --git a/t/t1700-split-index.sh b/t/t1700-split-index.sh
index 986baa612e..e2aa0bd949 100755
--- a/t/t1700-split-index.sh
+++ b/t/t1700-split-index.sh
@@ -510,4 +510,27 @@ test_expect_success 'do not refresh null base index' '
 	)
 '
 
+test_expect_success 'reading split index at alternate location' '
+	git init reading-alternate-location &&
+	(
+		cd reading-alternate-location &&
+		>file-in-alternate &&
+		git update-index --split-index --add file-in-alternate
+	) &&
+	echo file-in-alternate >expect &&
+
+	# Should be able to find the shared index both right next to
+	# the specified split index file ...
+	GIT_INDEX_FILE=./reading-alternate-location/.git/index \
+	git ls-files --cached >actual &&
+	test_cmp expect actual &&
+
+	# ... and, for backwards compatibility, in the current GIT_DIR
+	# as well.
+	mv -v ./reading-alternate-location/.git/sharedindex.* .git &&
+	GIT_INDEX_FILE=./reading-alternate-location/.git/index \
+	git ls-files --cached >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.33.0.358.g803110d36e


  parent reply	other threads:[~2021-08-26 21:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17 17:49 [PATCH 0/6] Fix GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-17 17:49 ` [PATCH 1/6] t1600-index: remove unnecessary redirection SZEDER Gábor
2021-08-17 18:12   ` Derrick Stolee
2021-08-17 18:39     ` SZEDER Gábor
2021-08-17 18:48       ` Derrick Stolee
2021-08-17 17:49 ` [PATCH 2/6] t1600-index: don't run git commands upstream of a pipe SZEDER Gábor
2021-08-17 17:49 ` [PATCH 3/6] t1600-index: disable GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-17 17:49 ` [PATCH 4/6] read-cache: look for shared index files next to the index, too SZEDER Gábor
2021-08-17 17:49 ` [PATCH 5/6] tests: disable GIT_TEST_SPLIT_INDEX for sparse index tests SZEDER Gábor
2021-08-17 18:26   ` Derrick Stolee
2021-08-17 21:32     ` SZEDER Gábor
2021-08-18 18:51       ` Derrick Stolee
2021-08-17 17:49 ` [PATCH 6/6] read-cache: fix GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-17 18:29   ` Derrick Stolee
2021-08-26 20:59 ` [PATCH v2 0/6] Fix GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-26 20:59   ` [PATCH v2 1/6] t1600-index: remove unnecessary redirection SZEDER Gábor
2021-08-26 21:00   ` [PATCH v2 2/6] t1600-index: don't run git commands upstream of a pipe SZEDER Gábor
2021-08-26 21:00   ` [PATCH v2 3/6] t1600-index: disable GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-26 21:00   ` SZEDER Gábor [this message]
2021-08-26 21:00   ` [PATCH v2 5/6] tests: disable GIT_TEST_SPLIT_INDEX for sparse index tests SZEDER Gábor
2021-08-26 21:00   ` [PATCH v2 6/6] read-cache: fix GIT_TEST_SPLIT_INDEX SZEDER Gábor
2021-08-31 14:47   ` [PATCH v2 0/6] Fix GIT_TEST_SPLIT_INDEX Derrick Stolee
2021-08-31 17:38     ` Junio C Hamano

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=20210826210004.672860-5-szeder.dev@gmail.com \
    --to=szeder.dev@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=stolee@gmail.com \
    --cc=t.gummerer@gmail.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).