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

[PATCH v3 2/2] ls-files: add %(skipworktree) atom to format option

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 4, 2023, 16:16 UTC
Message-ID
<15e77743f7f19994ca2c3da626e57962275383a0.1675527416.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1458.v3.git.1675527416.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

Because sometimes we want to check if the files in the index match the sparse specification, so introduce "%(skipworktree)" atom to git ls-files `--format` option. When we use this option, if the file match the sparse specification, it will output "1", otherwise, output empty string "".

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 Documentation/git-ls-files.txt |  4 ++++
 builtin/ls-files.c             |  3 +++
 t/t3013-ls-files-format.sh     | 16 ++++++++++++++++
 3 files changed, 23 insertions(+)
diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index 440043cdb8e..4902c311927 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -260,6 +260,10 @@ eolattr::
 	that applies to the path.
 path::
 	The pathname of the file which is recorded in the index.
+skipworktree::
+	Whether the file in the index has the SKIP_WORKTREE bit set.
+	See link:technical/sparse-checkout.txt[sparse-checkout]
+	for more information.
 
 EXCLUDE PATTERNS
 ----------------
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index a03b559ecaa..bbff868ae6b 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -280,6 +280,9 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,
 			      data->pathname));
 	else if (skip_prefix(start, "(path)", &p))
 		write_name_to_buf(sb, data->pathname);
+	else if (skip_prefix(start, "(skipworktree)", &p))
+		strbuf_addstr(sb, ce_skip_worktree(data->ce) ?
+			      "1" : "");
 	else
 		die(_("bad ls-files format: %%%.*s"), (int)len, start);
 
diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
index efb7450bf1e..65939d38444 100755
--- a/t/t3013-ls-files-format.sh
+++ b/t/t3013-ls-files-format.sh
@@ -92,4 +92,20 @@ test_expect_success 'git ls-files --format with --debug' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git ls-files --format with skipworktree' '
+	test_when_finished "git sparse-checkout disable" &&
+	mkdir dir1 dir2 &&
+	echo "file1" >dir1/file1.txt &&
+	echo "file2" >dir2/file2.txt &&
+	git add dir1 dir2 &&
+	git commit -m skipworktree &&
+	git sparse-checkout set dir1 &&
+	git ls-files --format="%(skipworktree) %(path)" "dir*" >actual &&
+	cat >expect <<-\EOF &&
+	 dir1/file1.txt
+	1 dir2/file2.txt
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadget
Message 24 of 24 in “ls-files: add %(skipworktree) atom to format option”
  1. ls-files: add %(skipworktree) atom to format optionZheNing Hu via GitGitGadget, Jan 11, 2023
  2. Elijah NewrenJan 12, 2023
  3. Junio C HamanoJan 12, 2023
  4. Elijah NewrenJan 13, 2023
  5. ZheNing HuJan 13, 2023
  6. ZheNing HuJan 13, 2023
  7. Elijah NewrenJan 14, 2023
  8. Ævar Arnfjörð BjarmasonJan 12, 2023
  9. ZheNing HuJan 13, 2023
  10. 0/2 ls-files: add %(skipworktree) atom to format optionZheNing Hu via GitGitGadget, Jan 19, 2023
  11. 1/2 docs: fix sparse-checkout docs linkZheNing Hu via GitGitGadget, Jan 19, 2023
  12. Elijah NewrenJan 20, 2023
  13. Martin ÅgrenJan 20, 2023
  14. ZheNing HuJan 23, 2023
  15. ZheNing HuJan 23, 2023
  16. 2/2 ls-files: add %(skipworktree) atom to format optionZheNing Hu via GitGitGadget, Jan 19, 2023
  17. Elijah NewrenJan 20, 2023
  18. Junio C HamanoJan 20, 2023
  19. ZheNing HuJan 23, 2023
  20. Junio C HamanoJan 23, 2023
  21. ZheNing HuJan 23, 2023
  22. 0/2 ls-files: add %(skipworktree) atom to format optionZheNing Hu via GitGitGadget, Feb 4, 2023
  23. 1/2 docs: fix sparse-checkout docs linkZheNing Hu via GitGitGadget, Feb 4, 2023
  24. 2/2 ls-files: add %(skipworktree) atom to format optionZheNing Hu via GitGitGadget, Feb 4, 2023

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.