git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 2/7] t3705: add tests for `git add` in sparse checkouts

From
Matheus Tavares <matheus.bernardino@usp.br>
Date
Mar 12, 2021, 22:47 UTC
Message-ID
<d93c3f51465a3e409819db63bd81802e7ef20ea9.1615588108.git.matheus.bernardino@usp.br>
In-Reply-To
<cover.1615588108.git.matheus.bernardino@usp.br>

We already have a couple tests for `add` with SKIP_WORKTREE entries in t7012, but these only cover the most basic scenarios. As we will be changing how `add` deals with sparse paths in the subsequent commits, let's move these two tests to their own file and add more test cases for different `add` options and situations. This also demonstrates two options that don't currently respect SKIP_WORKTREE entries: `--chmod` and `--renormalize`.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 t/t3705-add-sparse-checkout.sh   | 96 ++++++++++++++++++++++++++++++++
 t/t7012-skip-worktree-writing.sh | 19 -------
 2 files changed, 96 insertions(+), 19 deletions(-)
 create mode 100755 t/t3705-add-sparse-checkout.sh
diff --git a/t/t3705-add-sparse-checkout.sh b/t/t3705-add-sparse-checkout.sh
new file mode 100755
index 0000000000..6c5b8be863
--- /dev/null
+++ b/t/t3705-add-sparse-checkout.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='git add in sparse checked out working trees'
+
+. ./test-lib.sh
+
+SPARSE_ENTRY_BLOB=""
+
+# Optionally take a printf format string to write to the sparse_entry file
+setup_sparse_entry () {
+	# 'sparse_entry' might already be in the index with the skip-worktree
+	# bit set. Remove it so that the subsequent git add can update it.
+	git update-index --force-remove sparse_entry &&
+	if test $# -eq 1
+	then
+		printf "$1" >sparse_entry
+	else
+		>sparse_entry
+	fi &&
+	git add sparse_entry &&
+	git update-index --skip-worktree sparse_entry &&
+	SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry)
+}
+
+test_sparse_entry_unchanged () {
+	echo "100644 $SPARSE_ENTRY_BLOB 0	sparse_entry" >expected &&
+	git ls-files --stage sparse_entry >actual &&
+	test_cmp expected actual
+}
+
+setup_gitignore () {
+	test_when_finished rm -f .gitignore &&
+	cat >.gitignore <<-EOF
+	*
+	!/sparse_entry
+	EOF
+}
+
+test_expect_success 'git add does not remove sparse entries' '
+	setup_sparse_entry &&
+	rm sparse_entry &&
+	git add sparse_entry &&
+	test_sparse_entry_unchanged
+'
+
+test_expect_success 'git add -A does not remove sparse entries' '
+	setup_sparse_entry &&
+	rm sparse_entry &&
+	setup_gitignore &&
+	git add -A &&
+	test_sparse_entry_unchanged
+'
+
+test_expect_success 'git add . does not remove sparse entries' '
+	setup_sparse_entry &&
+	rm sparse_entry &&
+	setup_gitignore &&
+	git add . &&
+	test_sparse_entry_unchanged
+'
+
+for opt in "" -f -u --ignore-removal --dry-run
+do
+	test_expect_success "git add${opt:+ $opt} does not update sparse entries" '
+		setup_sparse_entry &&
+		echo modified >sparse_entry &&
+		git add $opt sparse_entry &&
+		test_sparse_entry_unchanged
+	'
+done
+
+test_expect_success 'git add --refresh does not update sparse entries' '
+	setup_sparse_entry &&
+	git ls-files --debug sparse_entry | grep mtime >before &&
+	test-tool chmtime -60 sparse_entry &&
+	git add --refresh sparse_entry &&
+	git ls-files --debug sparse_entry | grep mtime >after &&
+	test_cmp before after
+'
+
+test_expect_failure 'git add --chmod does not update sparse entries' '
+	setup_sparse_entry &&
+	git add --chmod=+x sparse_entry &&
+	test_sparse_entry_unchanged &&
+	! test -x sparse_entry
+'
+
+test_expect_failure 'git add --renormalize does not update sparse entries' '
+	test_config core.autocrlf false &&
+	setup_sparse_entry "LINEONE\r\nLINETWO\r\n" &&
+	echo "sparse_entry text=auto" >.gitattributes &&
+	git add --renormalize sparse_entry &&
+	test_sparse_entry_unchanged
+'
+
+test_done
diff --git a/t/t7012-skip-worktree-writing.sh b/t/t7012-skip-worktree-writing.sh
index f2a8e76511..a1080b94e3 100755
--- a/t/t7012-skip-worktree-writing.sh
+++ b/t/t7012-skip-worktree-writing.sh
@@ -60,13 +60,6 @@ setup_absent() {
 	git update-index --skip-worktree 1
 }
 
-test_absent() {
-	echo "100644 $EMPTY_BLOB 0	1" > expected &&
-	git ls-files --stage 1 > result &&
-	test_cmp expected result &&
-	test ! -f 1
-}
-
 setup_dirty() {
 	git update-index --force-remove 1 &&
 	echo dirty > 1 &&
@@ -100,18 +93,6 @@ test_expect_success 'index setup' '
 	test_cmp expected result
 '
 
-test_expect_success 'git-add ignores worktree content' '
-	setup_absent &&
-	git add 1 &&
-	test_absent
-'
-
-test_expect_success 'git-add ignores worktree content' '
-	setup_dirty &&
-	git add 1 &&
-	test_dirty
-'
-
 test_expect_success 'git-rm fails if worktree is dirty' '
 	setup_dirty &&
 	test_must_fail git rm 1 &&
-- 
2.30.1
Previous: Matheus TavaresNext: Derrick Stolee
Message 2 of 32 in “add/rm: honor sparse checkout and warn on sparse paths”
  1. 0/7 add/rm: honor sparse checkout and warn on sparse pathsMatheus Tavares, Mar 12, 2021
  2. 2/7 t3705: add tests for `git add` in sparse checkoutsMatheus Tavares, Mar 12, 2021
  3. Derrick StoleeMar 23, 2021
  4. 1/7 add: include magic part of pathspec on --refresh errorMatheus Tavares, Mar 12, 2021
  5. 4/7 pathspec: allow to ignore SKIP_WORKTREE entries on index matchingMatheus Tavares, Mar 12, 2021
  6. 5/7 refresh_index(): add REFRESH_DONT_MARK_SPARSE_MATCHES flagMatheus Tavares, Mar 12, 2021
  7. Junio C HamanoMar 18, 2021
  8. Junio C HamanoMar 19, 2021
  9. Matheus Tavares BernardinoMar 19, 2021
  10. Junio C HamanoMar 19, 2021
  11. Matheus Tavares BernardinoMar 30, 2021
  12. Elijah NewrenMar 31, 2021
  13. 3/7 add: make --chmod and --renormalize honor sparse checkoutsMatheus Tavares, Mar 12, 2021
  14. 6/7 add: warn when asked to update SKIP_WORKTREE entriesMatheus Tavares, Mar 12, 2021
  15. 7/7 rm: honor sparse checkout patternsMatheus Tavares, Mar 12, 2021
  16. Ævar Arnfjörð BjarmasonMar 21, 2021
  17. Matheus Tavares BernardinoMar 22, 2021
  18. Derrick StoleeMar 23, 2021
  19. Elijah NewrenMar 13, 2021
  20. 0/7 add/rm: honor sparse checkout and warn on sparse pathsMatheus Tavares, Apr 8, 2021
  21. 1/7 add: include magic part of pathspec on --refresh errorMatheus Tavares, Apr 8, 2021
  22. 2/7 t3705: add tests for `git add` in sparse checkoutsMatheus Tavares, Apr 8, 2021
  23. Derrick StoleeApr 14, 2021
  24. 3/7 add: make --chmod and --renormalize honor sparse checkoutsMatheus Tavares, Apr 8, 2021
  25. 4/7 pathspec: allow to ignore SKIP_WORKTREE entries on index matchingMatheus Tavares, Apr 8, 2021
  26. 5/7 refresh_index(): add flag to ignore SKIP_WORKTREE entriesMatheus Tavares, Apr 8, 2021
  27. 6/7 add: warn when asked to update SKIP_WORKTREE entriesMatheus Tavares, Apr 8, 2021
  28. 7/7 rm: honor sparse checkout patternsMatheus Tavares, Apr 8, 2021
  29. Elijah NewrenApr 14, 2021
  30. Matheus Tavares BernardinoApr 14, 2021
  31. Junio C HamanoApr 16, 2021
  32. Elijah NewrenApr 16, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.