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

[PATCH 05/23] Teach ls-files and update-index to respect skip-worktree bit

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 14, 2009, 10:30 UTC
Message-ID
<1260786666-8405-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1260786666-8405-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin-ls-files.c               |    2 +
 builtin-update-index.c           |   38 +++++++-----
 t/t7011-skip-worktree-reading.sh |  114 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 16 deletions(-)
 create mode 100755 t/t7011-skip-worktree-reading.sh
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index c1afbad..ad7e447 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -194,6 +194,8 @@ static void show_files(struct dir_struct *dir, const char *prefix)
 				continue;
 			if (ce->ce_flags & CE_UPDATE)
 				continue;
+			if (ce_skip_worktree(ce))
+				continue;
 			err = lstat(ce->name, &st);
 			if (show_deleted && err)
 				show_ce_entry(tag_removed, ce);
diff --git a/builtin-update-index.c b/builtin-update-index.c
index 5e97d09..97b9ea6 100644
--- a/builtin-update-index.c
+++ b/builtin-update-index.c
@@ -172,29 +172,29 @@ static int process_directory(const char *path, int len, struct stat *st)
 	return error("%s: is a directory - add files inside instead", path);
 }
 
-/*
- * Process a regular file
- */
-static int process_file(const char *path, int len, struct stat *st)
-{
-	int pos = cache_name_pos(path, len);
-	struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
-
-	if (ce && S_ISGITLINK(ce->ce_mode))
-		return error("%s is already a gitlink, not replacing", path);
-
-	return add_one_path(ce, path, len, st);
-}
-
 static int process_path(const char *path)
 {
-	int len;
+	int pos, len;
 	struct stat st;
+	struct cache_entry *ce;
 
 	len = strlen(path);
 	if (has_symlink_leading_path(path, len))
 		return error("'%s' is beyond a symbolic link", path);
 
+	pos = cache_name_pos(path, len);
+	ce = pos < 0 ? NULL : active_cache[pos];
+	if (ce && ce_skip_worktree(ce)) {
+		/*
+		 * working directory version is assumed "good"
+		 * so updating it does not make sense.
+		 * On the other hand, removing it from index should work
+		 */
+		if (allow_remove && remove_file_from_cache(path))
+			return error("%s: cannot remove from the index", path);
+		return 0;
+	}
+
 	/*
 	 * First things first: get the stat information, to decide
 	 * what to do about the pathname!
@@ -205,7 +205,13 @@ static int process_path(const char *path)
 	if (S_ISDIR(st.st_mode))
 		return process_directory(path, len, &st);
 
-	return process_file(path, len, &st);
+	/*
+	 * Process a regular file
+	 */
+	if (ce && S_ISGITLINK(ce->ce_mode))
+		return error("%s is already a gitlink, not replacing", path);
+
+	return add_one_path(ce, path, len, &st);
 }
 
 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
diff --git a/t/t7011-skip-worktree-reading.sh b/t/t7011-skip-worktree-reading.sh
new file mode 100755
index 0000000..ede3ee1
--- /dev/null
+++ b/t/t7011-skip-worktree-reading.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nguyễn Thái Ngọc Duy
+#
+
+test_description='skip-worktree bit test'
+
+. ./test-lib.sh
+
+cat >expect.full <<EOF
+H 1
+H 2
+H init.t
+H sub/1
+H sub/2
+EOF
+
+cat >expect.skip <<EOF
+S 1
+H 2
+H init.t
+S sub/1
+H sub/2
+EOF
+
+NULL_SHA1=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
+ZERO_SHA1=0000000000000000000000000000000000000000
+setup_absent() {
+	test -f 1 && rm 1
+	git update-index --remove 1 &&
+	git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
+	git update-index --skip-worktree 1
+}
+
+test_absent() {
+	echo "100644 $NULL_SHA1 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 &&
+	git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
+	git update-index --skip-worktree 1
+}
+
+test_dirty() {
+	echo "100644 $NULL_SHA1 0	1" > expected &&
+	git ls-files --stage 1 > result &&
+	test_cmp expected result &&
+	echo dirty > expected
+	test_cmp expected 1
+}
+
+test_expect_success 'setup' '
+	test_commit init &&
+	mkdir sub &&
+	touch ./1 ./2 sub/1 sub/2 &&
+	git add 1 2 sub/1 sub/2 &&
+	git update-index --skip-worktree 1 sub/1 &&
+	git ls-files -t > result &&
+	test_cmp expect.skip result
+'
+
+test_expect_success 'update-index' '
+	setup_absent &&
+	git update-index 1 &&
+	test_absent
+'
+
+test_expect_success 'update-index' '
+	setup_dirty &&
+	git update-index 1 &&
+	test_dirty
+'
+
+test_expect_success 'update-index --remove' '
+	setup_absent &&
+	git update-index --remove 1 &&
+	test -z "$(git ls-files 1)" &&
+	test ! -f 1
+'
+
+test_expect_success 'update-index --remove' '
+	setup_dirty &&
+	git update-index --remove 1 &&
+	test -z "$(git ls-files 1)" &&
+	echo dirty > expected &&
+	test_cmp expected 1
+'
+
+test_expect_success 'ls-files --delete' '
+	setup_absent &&
+	test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --delete' '
+	setup_dirty &&
+	test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --modified' '
+	setup_absent &&
+	test -z "$(git ls-files -m)"
+'
+
+test_expect_success 'ls-files --modified' '
+	setup_dirty &&
+	test -z "$(git ls-files -m)"
+'
+
+test_done
-- 
1.6.5.2.216.g9c1ec
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 10 of 30 in “nd/sparse reroll”
  1. 00/23 nd/sparse rerollNguyễn Thái Ngọc Duy, Dec 14, 2009
  2. 01/23 update-index: refactor mark_valid() in preparation for new optionsNguyễn Thái Ngọc Duy, Dec 14, 2009
  3. 02/23 Add test-index-versionNguyễn Thái Ngọc Duy, Dec 14, 2009
  4. 03/23 Introduce "skip-worktree" bit in index, teach Git to get/set this bitNguyễn Thái Ngọc Duy, Dec 14, 2009
  5. Greg PriceDec 14, 2009
  6. Nguyen Thai Ngoc DuyDec 15, 2009
  7. Johannes SixtDec 15, 2009
  8. Nguyen Thai Ngoc DuyDec 15, 2009
  9. 04/23 update-index: ignore update request if it's skip-worktreeNguyễn Thái Ngọc Duy, Dec 14, 2009
  10. 05/23 Teach ls-files and update-index to respect skip-worktree bitNguyễn Thái Ngọc Duy, Dec 14, 2009
  11. 06/23 Teach diff machinery to respect skip-worktree bitNguyễn Thái Ngọc Duy, Dec 14, 2009
  12. 07/23 Teach grep to respect skip-worktree bitNguyễn Thái Ngọc Duy, Dec 14, 2009
  13. 08/23 Teach commit to respect skip-worktree bitNguyễn Thái Ngọc Duy, Dec 14, 2009
  14. 09/23 Teach Git to respect skip-worktree bit (writing part)Nguyễn Thái Ngọc Duy, Dec 14, 2009
  15. 10/23 Avoid writing to buffer in add_excludes_from_file_1()Nguyễn Thái Ngọc Duy, Dec 14, 2009
  16. 11/23 Read .gitignore from index if it is skip-worktreeNguyễn Thái Ngọc Duy, Dec 14, 2009
  17. 12/23 unpack-trees(): carry skip-worktree bit over in merged_entry()Nguyễn Thái Ngọc Duy, Dec 14, 2009
  18. 13/23 excluded_1(): support exclude files in indexNguyễn Thái Ngọc Duy, Dec 14, 2009
  19. 14/23 dir.c: export excluded_1() and add_excludes_from_file_1()Nguyễn Thái Ngọc Duy, Dec 14, 2009
  20. 15/23 Introduce "sparse checkout"Nguyễn Thái Ngọc Duy, Dec 14, 2009
  21. 16/23 unpack-trees(): add CE_WT_REMOVE to remove on worktree aloneNguyễn Thái Ngọc Duy, Dec 14, 2009
  22. 17/23 unpack-trees.c: generalize verify_* functionsNguyễn Thái Ngọc Duy, Dec 14, 2009
  23. 18/23 unpack-trees(): "enable" sparse checkout and load $GIT_DIR/info/sparse-checkoutNguyễn Thái Ngọc Duy, Dec 14, 2009
  24. 19/23 unpack_trees(): apply $GIT_DIR/info/sparse-checkout to the final indexNguyễn Thái Ngọc Duy, Dec 14, 2009
  25. 20/23 unpack-trees(): ignore worktree check outside checkout areaNguyễn Thái Ngọc Duy, Dec 14, 2009
  26. 21/23 read-tree: add --no-sparse-checkout to disable sparse checkout supportNguyễn Thái Ngọc Duy, Dec 14, 2009
  27. 22/23 Add tests for sparse checkoutNguyễn Thái Ngọc Duy, Dec 14, 2009
  28. 23/23 sparse checkout: inhibit empty worktreeNguyễn Thái Ngọc Duy, Dec 14, 2009
  29. Johannes SixtDec 14, 2009
  30. Nguyen Thai Ngoc DuyDec 14, 2009

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.