Git development
 help / color / mirror / Atom feed
* [PATCH] commit: sync the index when committing with -a or pathspecs
@ 2026-12-04 19:49 Aman Ganapathy
  0 siblings, 0 replies; only message in thread
From: Aman Ganapathy @ 2026-12-04 19:49 UTC (permalink / raw)
  To: git; +Cc: Patrick Steinhardt, Neeraj Singh

When core.fsync includes "index", the expected result is that changes
to the index end up synced to disk. Normally, this means that when
files are staged or committed, "index.lock" is written, synced, and
then renamed to "index". Implementation wise, do_write_index() only
syncs it when the caller of write_locked_index() passes COMMIT_LOCK.
That flag tells it to rename the lock into place, and ba95e96d4c
(core.fsync: new option to harden the index, 2022-03-10) also uses it
to tell the repository's index apart from temporary ones created for a
single pass that don't need to be synced to disk.

"git commit -a", "git commit <paths>" and the interactive modes write
the repository's index without COMMIT_LOCK, because the lock is only
committed once the commit has been created. So the index is never
synced, even though "git add" followed by "git commit" syncs it.

On XFS, a crash shortly after "git commit -a" can leave ".git/index"
empty, and every command that reads it fails with "index file smaller
than expected". With "git commit <paths>", changes that were staged for
other paths are lost along with it.

Add a SYNC_LOCK flag that syncs the lock the way COMMIT_LOCK does, but
without committing it, and use it where commit writes the repository's
index.

Signed-off-by: Aman Ganapathy <amanganapathy@gmail.com>
---
I ran into this while testing crashes with core.fsync=all. Here's a
small repro (needs root and xfsprogs):

    truncate -s 300M /tmp/disk.img && mkfs.xfs -q /tmp/disk.img
    mount -o loop /tmp/disk.img /mnt && cd /mnt
    git init -q repo && cd repo
    git config core.fsync all
    echo one >f && git add f && git commit -q -m one && sync
    echo two >>f && git commit -q -a -m two
    git status >/dev/null
    xfs_io -x -c shutdown /mnt
    cd / && umount /mnt && mount -o loop /tmp/disk.img /mnt
    git -C /mnt/repo status   # "index file smaller than expected"

I added the "git status" because crashing right after the commit wasn't
enough, the old index survived. Some later command has to sync
something, and "git status" is what you'd usually run next. With
"git add" and a plain "git commit" instead of "commit -a", the index
survives. ext4 doesn't show this, I think because of auto_da_alloc.

I added a flag instead of reopening the lock in builtin/commit.c, since
write_locked_index() already closes it. Happy to change that if there's
a better way.

 builtin/commit.c                      | 11 +++++++----
 read-cache-ll.h                       |  4 ++++
 read-cache.c                          |  2 +-
 t/t7501-commit-basic-functionality.sh | 20 ++++++++++++++++++++
 4 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 36e47e18ed..5a89f96880 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -399,7 +399,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 
 		refresh_cache_or_die(refresh_flags);
 
-		if (write_locked_index(the_repository->index, &index_lock, 0))
+		if (write_locked_index(the_repository->index, &index_lock,
+				       SYNC_LOCK))
 			die(_("unable to create temporary index"));
 
 		old_repo_index_file = the_repository->index_file;
@@ -424,7 +425,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 		if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
 			if (reopen_lock_file(&index_lock) < 0)
 				die(_("unable to write index file"));
-			if (write_locked_index(the_repository->index, &index_lock, 0))
+			if (write_locked_index(the_repository->index,
+					       &index_lock, SYNC_LOCK))
 				die(_("unable to update temporary index"));
 		} else
 			warning(_("Failed to update main cache tree"));
@@ -462,7 +464,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 
 		refresh_cache_or_die(refresh_flags);
 		cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
-		if (write_locked_index(the_repository->index, &index_lock, 0))
+		if (write_locked_index(the_repository->index, &index_lock,
+				       SYNC_LOCK))
 			die(_("unable to write new index file"));
 		commit_style = COMMIT_NORMAL;
 		ret = get_lock_file_path(&index_lock);
@@ -535,7 +538,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
 	add_remove_files(&partial);
 	refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
 	cache_tree_update(the_repository->index, WRITE_TREE_SILENT);
-	if (write_locked_index(the_repository->index, &index_lock, 0))
+	if (write_locked_index(the_repository->index, &index_lock, SYNC_LOCK))
 		die(_("unable to write new index file"));
 
 	path = repo_git_path(the_repository, "next-index-%"PRIuMAX,
diff --git a/read-cache-ll.h b/read-cache-ll.h
index 182fe0a08c..b5257b4a01 100644
--- a/read-cache-ll.h
+++ b/read-cache-ll.h
@@ -286,6 +286,7 @@ int is_index_unborn(struct index_state *);
 /* For use with `write_locked_index()`. */
 #define COMMIT_LOCK		(1 << 0)
 #define SKIP_IF_UNCHANGED	(1 << 1)
+#define SYNC_LOCK		(1 << 2)
 
 /*
  * Write the index while holding an already-taken lock. Close the lock,
@@ -305,6 +306,9 @@ int is_index_unborn(struct index_state *);
  *
  * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
  * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
+ *
+ * The lock is synced to disk according to core.fsync if `COMMIT_LOCK`
+ * or `SYNC_LOCK` is given.
  */
 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
 
diff --git a/read-cache.c b/read-cache.c
index 595497d14c..cabf9c54d9 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -3160,7 +3160,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
 	}
 
 	csum_fsync_flag = 0;
-	if (!alternate_index_output && (flags & COMMIT_LOCK))
+	if (!alternate_index_output && (flags & (COMMIT_LOCK | SYNC_LOCK)))
 		csum_fsync_flag = CSUM_FSYNC;
 
 	finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_INDEX,
diff --git a/t/t7501-commit-basic-functionality.sh b/t/t7501-commit-basic-functionality.sh
index d0af38df20..8c6977c6b7 100755
--- a/t/t7501-commit-basic-functionality.sh
+++ b/t/t7501-commit-basic-functionality.sh
@@ -786,4 +786,24 @@ test_expect_success '--dry-run --short' '
 	git commit --dry-run --short
 '
 
+test_expect_success 'commit -a syncs the index' '
+	test_when_finished "rm -f trace2.txt" &&
+	echo sync >>file &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -c core.fsync=index -c core.fsyncMethod=fsync \
+		commit -a -m sync &&
+	test_grep "\"name\":\"hardware-flush\",\"count\":1}" trace2.txt
+'
+
+test_expect_success 'commit with pathspec syncs the index' '
+	test_when_finished "rm -f trace2.txt" &&
+	echo sync >>file &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -c core.fsync=index -c core.fsyncMethod=fsync \
+		commit -m sync file &&
+	test_grep "\"name\":\"hardware-flush\",\"count\":1}" trace2.txt
+'
+
 test_done
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 19:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 19:49 [PATCH] commit: sync the index when committing with -a or pathspecs Aman Ganapathy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox