From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796413751; b=WX5jm+4Q5pGHssOaqSuv22DEQTeDtZdZx14VsKygjM9x36E2ZzVAaRZcNvbi5YKL4WKC WZuA2VbKqI+AixlxDZGnElqS/55SVbqCtzA8F7zNRIFj6DDRigx3uDbQ52Oxi6WNBT6Je x0CiSqwr2vEMWkw2ktqOhG+BYBinB4pu4thrSlbsK2rLTDrpAyIus2d0tCuAgU+mOH2FE fqLqldCWh1XsfcroYy+nG/NKe+L3+n2AErtU3ui7dBTBpUpGHE+YN8yKGqF32o0Apg9v8 LIilmTkIha+C3znOhK5yQUXPajWXMW9zYm8x6Y26zEDKl2ESqgYpVAYRPG32KkP5v1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796413751; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=ijff54Y3hcT9hx65w9yV21NLI+zzQVob3BKP/W4HNd0=; b=uqrry+3L5f5dLXNCJs0l8RTZeUWSFNGb0pqmfw1UD4iuVGpq7fGi33omZ76Vm+Z/PCDq 7yDt4aq6O7FcWrXDkNcguCvXc+NtFDDZUMHYsTb+MXrT6LSJsjy+6i5NMcPBYbVUIxTEu Z/eWJ3nkUHRexw5p/i2dc95+VKWNpkDhN/L+beC0QbGEDQG36He9ByT03VkreyA1fOjXD XrlEQNMNDpwpWMJCtdTTROqf7oZnXeB/E73hMKjoznBaTB1k7Jwct6EEI5jjgkfijUXL9 qwkRlMrvDHZrZxgFHWZtNw7XOm3dYkWqNIBUbDbVWCQHPx1hAlQI5jKVbRxtPiVfcCA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=n5klOds2; arc=none smtp.remote-ip=74.125.82.48 Received: from mail-dl1-f48.google.com (mail-dl1-f48.google.com [74.125.82.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4CED606A68F for ; Fri, 4 Dec 2026 19:49:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B4CED606A68F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=n5klOds2 Received: by mail-dl1-f48.google.com with SMTP id a92af1059eb24-1b589050416so1606893c88.0 for ; Fri, 04 Dec 2026 11:49:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796413750; x=1797018550; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ijff54Y3hcT9hx65w9yV21NLI+zzQVob3BKP/W4HNd0=; b=n5klOds2jXpQ2fGLI1r7feVM73n4mVKI+1s8j7nNn9BK3EwLdRqyqr38Pvu3gDEKuA mpOVHg6Dalxkr0YkFECoUJMMrWGmzyRhAPncB2Lqm2H6SWiQawFYTDeeh10uFmMjMUTy VDtJT8cPMkrX37YwPsEMYqqUeJPUSoSik8GI3JbPpyZFhjEYgLeepoU/uHQKEsZWpzea MVhD4c0G6Rdl+mG0rL1WHe/iD462R8/+9Q81HRnabr1ka5GyfzYVdMXxRJIYtSNacjQG Y7dE9V07Z1uRjYTDRVd6lTaBw8mHgAznd20rpFKO56yFCKYEsB06D6h1ss4QmSGy0svO oXjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796413750; x=1797018550; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ijff54Y3hcT9hx65w9yV21NLI+zzQVob3BKP/W4HNd0=; b=0DZTuK/6eV1xZoPkaij4JgdqFlXmLwaaqzWbTPCfQLXshOKgEkHSYhCv1dJY6cZ5Ch oENPAOeKbjG5uJqou60bQuiRIPfSg5GkIDT+xPPfwVosihJp5PgMMsXns9A0Pge9T2Ap eVVkteTqjRa5MpEWot/dWMsPd+Q6yMV1D5E9o/JZbz9UCsWaKr1buxrV/BTGHjRDuZk5 aMKjBr/S65gCmkWSVCzsgDi6PuAD8bbdhlQ3H3LPhmbTTNX6gwsGeFdBA6dWowmLIL53 GAu/GY/a26vnHyLAQYSK6b1FC7GtK501Hn+YesgLj29mfpOfQIYTZAaHIJaZRpb3jOtw a+YQ== X-Gm-Message-State: APuU4Z+UQ/i2i4YmuWbzdydKvSGahWuvNdVEi3I1ur5E1ZE+pLcGwaZ5 4FboQLv1bqWDgAW/Yt2joGgHcghYYvNKN/sAsoROKE+VVGzBVXleE9J6V1Xokw== X-Gm-Gg: AZFdTZdMsRyXTKdngKVbdswHvCTBycwIX7BMBUWsZh76wdH91TftR0wIVqsGVD+rvgi xPTFrR1hJRvuynEcNOaYQ3BVghmtmL0NHFC6SgAp1V1oLgK/SC49CfKv4vWigB6998iglMpSZjz QL0nm75zdYVSkca1p+gzhzLs+h3asVZptmsnr5mTv0L4I1Zh+e7ixFjg1oyFXnQAWZCalojjI2P jLH8cnl090rpJmMAYGFm/deriP0y6Luf0B5sDYmUF5Ovh58wyZXJbvb7A33NM0VlLKK+mkmP761 UiFHt/GQ3ts2HAzmM18n0+rcHLHpCijx487RhSPjaN9BgPVssDZVuSSXGYWQIDoeH30mI3eeYSS 7SgBIOZF2uTQPK7CSkkVGuweAMc3W3yBFiGa7Of4s3avqSJQZEm3mSGFotutLw/pi6dVXCKPKPs IDTIyLH7iVNCEowMBLkGyWCZVJ9a4bYAAl5FVavfTmVzZq2GXSoQMlh96YoNhn2hmR44oJ4bUwK tW1P6IdJbx98AJmXknVmcs1Mohw3wjEd6QOJeW1QE1j2cTIeBrM855lWVUZariuXDFvyAmC1Ns+ d+2yMUR3EIkwAUZtZNFyZtFAzsPmyeQInoV5HqkhpzZUsOBKx3IiKmxIcMWLxuw7/IcRJ3LfXDP koDWVymd32FUwQc2LxWWgCA0= X-Received: by 2002:a05:701a:c9d0:10b0:1b2:4f97:27d7 with SMTP id a92af1059eb24-1b5d7114b22mr2472479c88.36.1796413749579; Fri, 04 Dec 2026 11:49:09 -0800 (PST) Received: from localhost.localdomain ([2601:645:c100:ab8f:a453:914a:6bd1:2a12]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d63d04e7sm9375119c88.1.2026.12.04.11.49.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 04 Dec 2026 11:49:09 -0800 (PST) From: Aman Ganapathy To: git@vger.kernel.org Cc: Patrick Steinhardt , Neeraj Singh Subject: [PATCH] commit: sync the index when committing with -a or pathspecs Date: Fri, 4 Dec 2026 11:49:07 -0800 Message-Id: <20261204194907.48107-1-amanganapathy@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 " 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 ", 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 --- 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)