diff mbox series

[2/4] show: integrate with the sparse index

Message ID 27ab853a9b4f0a50880c92dd8949d6fe9b7f00ba.1649349442.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series Sparse index integration with 'git show' | expand

Commit Message

Derrick Stolee April 7, 2022, 4:37 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

The 'git show' command can take an input to request the state of an
object in the index. This can lead to parsing the index in order to load
a specific file entry. Without the change presented here, a sparse index
would expand to a full one, taking much longer than usual to access a
simple file.

There is one behavioral change that happens here, though: we now can
find a sparse directory entry within the index! Commands that previously
failed because we could not find an entry in the worktree or index now
succeed because we _do_ find an entry in the index.

There might be more work to do to make other situations succeed when
looking for an indexed tree, perhaps by looking at or updating the
cache-tree extension as needed. These situations include having a full
index or asking for a directory that is within the sparse-checkout cone
(and hence is not a sparse directory entry in the index).

For now, we demonstrate how the sparse index integration is extremely
simple for files outside of the cone as well as directories within the
cone. A later change will resolve this behavior around sparse
directories.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 builtin/log.c                            |  5 +++++
 t/t1092-sparse-checkout-compatibility.sh | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)

Comments

Josh Steadmon April 14, 2022, 6:50 p.m. UTC | #1
On 2022.04.07 16:37, Derrick Stolee via GitGitGadget wrote:
> From: Derrick Stolee <dstolee@microsoft.com>
> 
> The 'git show' command can take an input to request the state of an
> object in the index. This can lead to parsing the index in order to load
> a specific file entry. Without the change presented here, a sparse index
> would expand to a full one, taking much longer than usual to access a
> simple file.
> 
> There is one behavioral change that happens here, though: we now can
> find a sparse directory entry within the index! Commands that previously
> failed because we could not find an entry in the worktree or index now
> succeed because we _do_ find an entry in the index.

As with the test in the previous commit, a reminder that sparse-indexes
are not necessarily subsets of a full index could be helpful here.


> There might be more work to do to make other situations succeed when
> looking for an indexed tree, perhaps by looking at or updating the
> cache-tree extension as needed. These situations include having a full
> index or asking for a directory that is within the sparse-checkout cone
> (and hence is not a sparse directory entry in the index).
> 
> For now, we demonstrate how the sparse index integration is extremely
> simple for files outside of the cone as well as directories within the
> cone. A later change will resolve this behavior around sparse
> directories.
> 
> Signed-off-by: Derrick Stolee <derrickstolee@github.com>
> ---
>  builtin/log.c                            |  5 +++++
>  t/t1092-sparse-checkout-compatibility.sh | 23 +++++++++++++++++++----
>  2 files changed, 24 insertions(+), 4 deletions(-)
> 
> diff --git a/builtin/log.c b/builtin/log.c
> index c211d66d1d0..8e2e9912ab9 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -661,6 +661,11 @@ int cmd_show(int argc, const char **argv, const char *prefix)
>  	init_log_defaults();
>  	git_config(git_log_config, NULL);
>  
> +	if (the_repository->gitdir) {
> +		prepare_repo_settings(the_repository);
> +		the_repository->settings.command_requires_full_index = 0;
> +	}
> +
>  	memset(&match_all, 0, sizeof(match_all));
>  	repo_init_revisions(the_repository, &rev, prefix);
>  	git_config(grep_config, &rev.grep_filter);
> diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
> index 74792b5ebbc..f6a14e08b81 100755
> --- a/t/t1092-sparse-checkout-compatibility.sh
> +++ b/t/t1092-sparse-checkout-compatibility.sh
> @@ -1159,12 +1159,20 @@ test_expect_success 'show (cached blobs/trees)' '
>  	test_sparse_match git show :folder1/a &&
>  
>  	# Asking "git show" for directories in the index
> -	# does not work as implemented. The error message is
> -	# different for a full checkout and a sparse checkout
> -	# when the directory is outside of the cone.
> +	# changes depending on the existence of a sparse index.

The wording here seems awkward after these changes are applied. Without
other context, it makes it sound to me like the command(s) used to show
a directory change depending on the existence of a sparse index, rather
than the fact that the behavior of `git show` changes.


>  	test_all_match test_must_fail git show :deep/ &&
>  	test_must_fail git -C full-checkout show :folder1/ &&
> -	test_sparse_match test_must_fail git show :folder1/
> +	test_must_fail git -C sparse-checkout show :folder1/ &&
> +
> +	git -C sparse-index show :folder1/ >actual &&
> +	git -C full-checkout show HEAD:folder1 >expect &&
> +
> +	# The output of "git show" includes the way we referenced the
> +	# objects, so strip that out.
> +	test_line_count = 4 actual &&
> +	tail -n 2 actual >actual-trunc &&
> +	tail -n 2 expect >expect-trunc &&
> +	test_cmp expect-trunc actual-trunc
>  '

It's not specific to this commit, but in general I think the series of
changes to this test would be easier to follow if we used hard-coded
strings to compare against, rather than matching parts of files against
each other. It makes it more clear to the reader exactly which behavior
is changing, and can make it more obvious why certain output is
undesirable. However, it would make the test more brittle to future
changes.


>  test_expect_success 'submodule handling' '
> @@ -1388,6 +1396,13 @@ test_expect_success 'sparse index is not expanded: diff' '
>  	ensure_not_expanded diff --cached
>  '
>  
> +test_expect_success 'sparse index is not expanded: show' '
> +	init_repos &&
> +
> +	ensure_not_expanded show :a &&
> +	ensure_not_expanded show :deep/a
> +'
> +
>  test_expect_success 'sparse index is not expanded: update-index' '
>  	init_repos &&
>  
> -- 
> gitgitgadget
>
Derrick Stolee April 18, 2022, 12:28 p.m. UTC | #2
On 4/14/2022 2:50 PM, Josh Steadmon wrote:
> On 2022.04.07 16:37, Derrick Stolee via GitGitGadget wrote:
>> From: Derrick Stolee <dstolee@microsoft.com>

>>  	# Asking "git show" for directories in the index
>> -	# does not work as implemented. The error message is
>> -	# different for a full checkout and a sparse checkout
>> -	# when the directory is outside of the cone.
>> +	# changes depending on the existence of a sparse index.
> 
> The wording here seems awkward after these changes are applied. Without
> other context, it makes it sound to me like the command(s) used to show
> a directory change depending on the existence of a sparse index, rather
> than the fact that the behavior of `git show` changes.

I can see that.

>> +	# The output of "git show" includes the way we referenced the
>> +	# objects, so strip that out.
>> +	test_line_count = 4 actual &&
>> +	tail -n 2 actual >actual-trunc &&
>> +	tail -n 2 expect >expect-trunc &&
>> +	test_cmp expect-trunc actual-trunc
>>  '
> 
> It's not specific to this commit, but in general I think the series of
> changes to this test would be easier to follow if we used hard-coded
> strings to compare against, rather than matching parts of files against
> each other. It makes it more clear to the reader exactly which behavior
> is changing, and can make it more obvious why certain output is
> undesirable. However, it would make the test more brittle to future
> changes.

The tests here are designed with this approach in mind: demonstrate
success by comparing to existing behavior. We don't want to be
coupled to the exact behavior of these commands, but we _do_ want to
demonstrate that the sparse-checkout or sparse-index features do not
change from the full-checkout behavior (unless we are demonstrating an
expected difference).

In particular, using comparisons like this are also robust against
changes in the test repository data shape, which has been necessary to
update as bugs are found.

Thanks,
-Stolee
diff mbox series

Patch

diff --git a/builtin/log.c b/builtin/log.c
index c211d66d1d0..8e2e9912ab9 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -661,6 +661,11 @@  int cmd_show(int argc, const char **argv, const char *prefix)
 	init_log_defaults();
 	git_config(git_log_config, NULL);
 
+	if (the_repository->gitdir) {
+		prepare_repo_settings(the_repository);
+		the_repository->settings.command_requires_full_index = 0;
+	}
+
 	memset(&match_all, 0, sizeof(match_all));
 	repo_init_revisions(the_repository, &rev, prefix);
 	git_config(grep_config, &rev.grep_filter);
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index 74792b5ebbc..f6a14e08b81 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -1159,12 +1159,20 @@  test_expect_success 'show (cached blobs/trees)' '
 	test_sparse_match git show :folder1/a &&
 
 	# Asking "git show" for directories in the index
-	# does not work as implemented. The error message is
-	# different for a full checkout and a sparse checkout
-	# when the directory is outside of the cone.
+	# changes depending on the existence of a sparse index.
 	test_all_match test_must_fail git show :deep/ &&
 	test_must_fail git -C full-checkout show :folder1/ &&
-	test_sparse_match test_must_fail git show :folder1/
+	test_must_fail git -C sparse-checkout show :folder1/ &&
+
+	git -C sparse-index show :folder1/ >actual &&
+	git -C full-checkout show HEAD:folder1 >expect &&
+
+	# The output of "git show" includes the way we referenced the
+	# objects, so strip that out.
+	test_line_count = 4 actual &&
+	tail -n 2 actual >actual-trunc &&
+	tail -n 2 expect >expect-trunc &&
+	test_cmp expect-trunc actual-trunc
 '
 
 test_expect_success 'submodule handling' '
@@ -1388,6 +1396,13 @@  test_expect_success 'sparse index is not expanded: diff' '
 	ensure_not_expanded diff --cached
 '
 
+test_expect_success 'sparse index is not expanded: show' '
+	init_repos &&
+
+	ensure_not_expanded show :a &&
+	ensure_not_expanded show :deep/a
+'
+
 test_expect_success 'sparse index is not expanded: update-index' '
 	init_repos &&