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

[PATCH 19/20] update-index: test the system before enabling untracked cache

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 7, 2014, 14:51 UTC
Message-ID
<1399474320-6840-20-git-send-email-pclouds@gmail.com>
In-Reply-To
<1399474320-6840-1-git-send-email-pclouds@gmail.com>
---
 builtin/update-index.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 144 insertions(+)
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 003e28e..f18076b 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -46,6 +46,145 @@ static void report(const char *fmt, ...)
 	va_end(vp);
 }
 
+static void remove_test_directory(void)
+{
+	struct strbuf sb = STRBUF_INIT;
+	strbuf_addstr(&sb, git_path("dir-mtime-test"));
+	remove_dir_recursively(&sb, 0);
+	strbuf_release(&sb);
+}
+
+static void xmkdir(const char *path)
+{
+	if (mkdir(path, 0700))
+		die_errno(_("failed to create directory %s"), path);
+}
+
+static int xstat(const char *path, struct stat *st)
+{
+	if (stat(path, st))
+		die_errno(_("failed to stat %s"), path);
+	return 0;
+}
+
+static int create_file(const char *path)
+{
+	int fd = open(path, O_CREAT | O_RDWR, 0644);
+	if (fd < 0)
+		die_errno(_("failed to create file %s"), path);
+	return fd;
+}
+
+static void xunlink(const char *path)
+{
+	if (unlink(path))
+		die_errno(_("failed to delete file %s"), path);
+}
+
+static void xrmdir(const char *path)
+{
+	if (rmdir(path))
+		die_errno(_("failed to delete directory %s"), path);
+}
+
+static void avoid_racy(void)
+{
+	/*
+	 * not use if we could usleep(10) if USE_NSEC is defined. The
+	 * field nsec could be there, but the OS could choose to
+	 * ignore it?
+	 */
+	sleep(1);
+}
+
+static int test_if_untracked_cache_is_supported(void)
+{
+	struct stat st;
+	struct stat_data base;
+	int fd;
+
+	fprintf(stderr, _("Testing "));
+	xmkdir(git_path("dir-mtime-test"));
+	atexit(remove_test_directory);
+	xstat(git_path("dir-mtime-test"), &st);
+	fill_stat_data(&base, &st);
+	fputc('.', stderr);
+
+	avoid_racy();
+	fd = create_file(git_path("dir-mtime-test/newfile"));
+	xstat(git_path("dir-mtime-test"), &st);
+	if (!match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr,_("directory stat info does not "
+				    "change after adding a new file"));
+		return 0;
+	}
+	fill_stat_data(&base, &st);
+	fputc('.', stderr);
+
+	avoid_racy();
+	xmkdir(git_path("dir-mtime-test/new-dir"));
+	xstat(git_path("dir-mtime-test"), &st);
+	if (!match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr, _("directory stat info does not change "
+				     "after adding a new directory"));
+		return 0;
+	}
+	fill_stat_data(&base, &st);
+	fputc('.', stderr);
+
+	avoid_racy();
+	write_or_die(fd, "data", 4);
+	close(fd);
+	xstat(git_path("dir-mtime-test"), &st);
+	if (match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr, _("directory stat info changes "
+				     "after updating a file"));
+		return 0;
+	}
+	fputc('.', stderr);
+
+	avoid_racy();
+	close(create_file(git_path("dir-mtime-test/new-dir/new")));
+	xstat(git_path("dir-mtime-test"), &st);
+	if (match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr, _("directory stat info changes after "
+				     "adding a file inside subdirectory"));
+		return 0;
+	}
+	fputc('.', stderr);
+
+	avoid_racy();
+	xunlink(git_path("dir-mtime-test/newfile"));
+	xstat(git_path("dir-mtime-test"), &st);
+	if (!match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr, _("directory stat info does not "
+				     "change after deleting a file"));
+		return 0;
+	}
+	fill_stat_data(&base, &st);
+	fputc('.', stderr);
+
+	avoid_racy();
+	xunlink(git_path("dir-mtime-test/new-dir/new"));
+	xrmdir(git_path("dir-mtime-test/new-dir"));
+	xstat(git_path("dir-mtime-test"), &st);
+	if (!match_stat_data(&base, &st)) {
+		fputc('\n', stderr);
+		fprintf_ln(stderr, _("directory stat info does not "
+				     "change after deleting a directory"));
+		return 0;
+	}
+
+	xrmdir(git_path("dir-mtime-test"));
+	fprintf_ln(stderr, _(" OK"));
+	return 1;
+}
+
 static int mark_ce_flags(const char *path, int flag, int mark)
 {
 	int namelen = strlen(path);
@@ -823,6 +962,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			resolve_undo_clear_callback},
 		OPT_INTEGER(0, "index-version", &preferred_index_format,
 			N_("write index in this format")),
+		OPT_SET_INT(0, "force-untracked-cache", &untracked_cache,
+			    N_("enable untracked cache without testing the filesystem"), 2),
 		OPT_BOOL(0, "untracked-cache", &untracked_cache,
 			N_("enable/disable untracked cache")),
 		OPT_END()
@@ -921,6 +1062,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 	if (untracked_cache > 0) {
 		struct untracked_cache *uc;
 
+		if (untracked_cache < 2 &&
+		    !test_if_untracked_cache_is_supported())
+			return 1;
 		uc = xcalloc(1, sizeof(*uc));
 		uc->exclude_per_dir = ".gitignore";
 		/* should be the same flags used by git-status */
-- 
1.9.1.346.ga2b5940
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 20 of 21 in “Untracked cache to speed up "git status"”
  1. 00/20 Untracked cache to speed up "git status"Nguyễn Thái Ngọc Duy, May 7, 2014
  2. 01/20 dir.c: coding style fixNguyễn Thái Ngọc Duy, May 7, 2014
  3. 02/20 dir.h: move struct exclude declaration to top levelNguyễn Thái Ngọc Duy, May 7, 2014
  4. 03/20 prep_exclude: remove the artificial PATH_MAX limitNguyễn Thái Ngọc Duy, May 7, 2014
  5. 04/20 dir.c: optionally compute sha-1 of a .gitignore fileNguyễn Thái Ngọc Duy, May 7, 2014
  6. 05/20 untracked cache: record .gitignore information and dir hierarchyNguyễn Thái Ngọc Duy, May 7, 2014
  7. 06/20 untracked cache: initial untracked cache validationNguyễn Thái Ngọc Duy, May 7, 2014
  8. 07/20 untracked cache: invalidate dirs recursively if .gitignore changesNguyễn Thái Ngọc Duy, May 7, 2014
  9. 08/20 untracked cache: record/validate dir mtime and reuse cached outputNguyễn Thái Ngọc Duy, May 7, 2014
  10. 09/20 untracked cache: mark what dirs should be recursed/savedNguyễn Thái Ngọc Duy, May 7, 2014
  11. 10/20 untracked cache: don't open non-existent .gitignoreNguyễn Thái Ngọc Duy, May 7, 2014
  12. 11/20 untracked cache: save to an index extensionNguyễn Thái Ngọc Duy, May 7, 2014
  13. 12/20 untracked cache: load from UNTR index extensionNguyễn Thái Ngọc Duy, May 7, 2014
  14. 13/20 untracked cache: invalidate at index addition or removalNguyễn Thái Ngọc Duy, May 7, 2014
  15. 14/20 untracked cache: print untracked statistics with $GIT_TRACE_UNTRACKEDNguyễn Thái Ngọc Duy, May 7, 2014
  16. 15/20 read-cache.c: split racy stat test to a separate functionNguyễn Thái Ngọc Duy, May 7, 2014
  17. 16/20 untracked cache: avoid racy timestampsNguyễn Thái Ngọc Duy, May 7, 2014
  18. 17/20 status: support untracked cacheNguyễn Thái Ngọc Duy, May 7, 2014
  19. 18/20 update-index: manually enable or disable untracked cacheNguyễn Thái Ngọc Duy, May 7, 2014
  20. 19/20 update-index: test the system before enabling untracked cacheNguyễn Thái Ngọc Duy, May 7, 2014
  21. 20/20 t7063: tests for untracked cacheNguyễn Thái Ngọc Duy, May 7, 2014

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.