{"thread":{"id":"66429","subject":"[BUG] sparse-checkout: checkout clobbers untracked in-cone file","startedAt":"2026-09-30T20:21:01Z","lastAt":"2026-10-09T14:00:05Z","messageCount":2,"participants":["Webstrand","Muhammed Dilshad A"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"553752","messageId":"CACm1TQd5b9tX368LrsD26Q6tm_mzdi1nwGtGd2V1jwYW+r2c1A@mail.gmail.com","threadId":"66429","inReplyTo":null,"subject":"[BUG] sparse-checkout: checkout clobbers untracked in-cone file","fromName":"Webstrand","fromEmail":"webstrand@gmail.com","sentAt":"2026-09-30T20:20:48Z","receivedAt":"2026-09-30T20:21:01Z","isPatch":false,"sender":{"key":"webstrand@gmail.com","avatar":null},"body":"What did you do before the bug happened? (Steps to reproduce your issue)\n\n    git init --initial-branch=locus repo\n    cd repo\n    mkdir src docs\n    echo \"tracked\" >src/tracked.txt\n    echo \"tracked\" >docs/tracked.txt\n    git add .\n    git commit --message=\"initial\"\n    git sparse-checkout set src\n    git branch some-feature-branch\n    git checkout some-feature-branch\n    echo \"incoming\" >src/new.txt\n    git add src/new.txt\n    git commit --message=\"add src/new.txt\"\n    git checkout locus\n    echo \"local\" >src/new.txt # conflicting untracked file\n    git checkout some-feature-branch\n    echo $?\n    cat src/new.txt\n\nWhat did you expect to happen? (Expected behavior)\n\nThe checkout operation should be aborted as a whole:\n\n 1. No files in the working tree should be updated.\n 2. HEAD should not be moved and should continue to reference locus.\n 3. src/new.txt must be unmodified, preserving the untracked content\n    \"local\".\n 4. git should exit with status 1 and print the following error:\n\n    error: The following untracked working tree files would be\noverwritten by checkout:\n        src/new.txt\n    Please move or remove them before you switch branches.\n    Aborting\n\nSparse checkouts should have the same behavior as non-sparse checkouts\nof the same commits.\n\nWhat happened instead? (Actual behavior)\n\nThe checkout operation clobbered untracked files:\n\n 1. Files in the working tree were updated.\n 2. HEAD was moved to reference some-feature-branch.\n 3. src/new.txt was overwritten with the content \"incoming\", clobbering\n    it.\n 4. git exited with status 0 and printed the following warning:\n\n    warning: The following paths were already present and thus not\nupdated despite sparse patterns:\n        src/new.txt\n\n    After fixing the above paths, you may want to run `git\nsparse-checkout reapply`.\n    Switched to branch 'some-feature-branch'\n\nAdditionally, the produced warning is incorrect and misleading.\n\nWhat's different between what you expected and what actually happened?\n\nThe checkout completed instead of being aborted, src/new.txt was\nclobbered instead of preserved, and the conflict was reported as a\nwarning instead of an error, and the warning was incorrect and\nmisleading. As a result, the untracked content of src/new.txt was\ndestroyed.\n\nAnything else you want to add:\n\nThe behavior is present from v2.27.0, the first release to contain\n681c637b4a, through the tip of master (a018953688).\n\nThe behavior was introduced by\n\n681c637b4a unpack-trees: failure to set SKIP_WORKTREE bits always just a warning\n\nPrior to 681c6, git refused the sparse checkout, producing the same\nerror as the non-sparse checkout, with the same behavior as in \"Expected\nbehavior\". src/new.txt was not clobbered. Starting with 681c6, git\ncompletes the sparse checkout with the warning shown in \"Actual\nbehavior\", and src/new.txt is clobbered.\n\n[System Info]\ngit version 2.56.0\ncpu: x86_64\nbuilt from commit: a018953688f1b10bddf91bff8747068f5f4746a4\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nrust: enabled\nfeature: fsmonitor--daemon\ngettext: enabled\nlibcurl: 8.22.0\nOpenSSL: OpenSSL 3.5.3 16 Sep 2025\nzlib: 1.3.1\nSHA-1: SHA1_DC\nSHA-256: SHA256_BLK\ndefault-ref-format: files\ndefault-hash: sha1\nuname: Linux 7.2.7-1-default #1 SMP PREEMPT_DYNAMIC Tue Sep 22\n08:00:24 UTC 2026 (e601a2d) x86_64\ncompiler info: gnuc: 16.2\nlibc info: glibc: 2.44\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\nnot run from a git repository - no hooks to show\n"},{"id":"554606","messageId":"a17e56acd90a931ebe95555bf62b47469ea2708a.1791543268.git.dilsheddilu123@gmail.com","threadId":"66429","inReplyTo":"CACm1TQd5b9tX368LrsD26Q6tm_mzdi1nwGtGd2V1jwYW+r2c1A@mail.gmail.com","subject":"[PATCH] unpack-trees: protect untracked files in sparse checkouts","fromName":"Muhammed Dilshad A","fromEmail":"dilsheddilu123@gmail.com","sentAt":"2026-10-09T14:00:05Z","receivedAt":"2026-10-09T14:00:05Z","isPatch":true,"sender":{"key":"dilsheddilu123@gmail.com","avatar":null},"body":"In a sparse checkout, switching to a branch that adds a file over an\nuntracked local path can warn and continue, replacing the local contents.\nOrdinary checkout refuses the same switch.\n\nmerged_entry() defers the check for new paths until sparse patterns are\nknown. The deferred check reports a sparsity warning, and unpack_trees()\nclears its failure before updating the working tree.\n\nUse the ordinary untracked overwrite error for that check. Finish checking\nall new entries before applying sparsity changes, and abort through the\nexisting failure path if any conflict is found. Keep advisory warnings for\nsparsity changes to existing entries.\n\nAdd a regression comparing full checkout, sparse checkout, and sparse-index\ncheckout. Check both conflicting files, complete diagnostics, HEAD, an\nunrelated tracked file, and an existing staged change. Adjust the warning\ntest to use existing skipped entries and verify that their local contents\nsurvive.\n\nReported-by: Webstrand <webstrand@gmail.com>\nLink: https://lore.kernel.org/git/CACm1TQd5b9tX368LrsD26Q6tm_mzdi1nwGtGd2V1jwYW+r2c1A@mail.gmail.com/\nSigned-off-by: Muhammed Dilshad A <dilsheddilu123@gmail.com>\n---\n t/t1011-read-tree-sparse-checkout.sh     | 16 ++++++---\n t/t1092-sparse-checkout-compatibility.sh | 41 ++++++++++++++++++++++++\n unpack-trees.c                           |  9 +++++-\n 3 files changed, 60 insertions(+), 6 deletions(-)\n\ndiff --git a/t/t1011-read-tree-sparse-checkout.sh b/t/t1011-read-tree-sparse-checkout.sh\nindex 93244ee134..54a7258b03 100755\n--- a/t/t1011-read-tree-sparse-checkout.sh\n+++ b/t/t1011-read-tree-sparse-checkout.sh\n@@ -263,12 +263,15 @@ test_expect_success 'read-tree --reset removes outside worktree' '\n '\n \n test_expect_success 'print warnings when some worktree updates disabled' '\n-\techo sub >.git/info/sparse-checkout &&\n-\tgit checkout -f init &&\n+\techo init.t >.git/info/sparse-checkout &&\n+\tgit checkout -f top &&\n \tmkdir sub &&\n-\ttouch sub/added sub/addedtoo &&\n+\ttest_write_lines local >sub/added &&\n+\ttest_write_lines local >sub/addedtoo &&\n+\techo sub >.git/info/sparse-checkout &&\n+\t# Keep skip-worktree bits on the materialized paths to test the warning.\n \t# Use -q to suppress \"Previous HEAD position\" and \"Head is now at\" msgs\n-\tgit checkout -q top 2>actual &&\n+\tgit -c sparse.expectFilesOutsideOfPatterns=true checkout -q top 2>actual &&\n \tcat >expected <<\\EOF &&\n warning: The following paths were already present and thus not updated despite sparse patterns:\n \tsub/added\n@@ -276,7 +279,10 @@ warning: The following paths were already present and thus not updated despite s\n \n After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n EOF\n-\ttest_cmp expected actual\n+\ttest_cmp expected actual &&\n+\ttest_write_lines local >expected-content &&\n+\ttest_cmp expected-content sub/added &&\n+\ttest_cmp expected-content sub/addedtoo\n '\n \n test_expect_success 'checkout without --ignore-skip-worktree-bits' '\ndiff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh\nindex 05b54062b3..0958af35c4 100755\n--- a/t/t1092-sparse-checkout-compatibility.sh\n+++ b/t/t1092-sparse-checkout-compatibility.sh\n@@ -486,6 +486,47 @@ test_expect_success 'checkout with modified sparse directory' '\n \ttest_all_match git checkout base\n '\n \n+test_expect_success 'checkout protects untracked files inside sparse cone' '\n+\tinit_repos &&\n+\tgit -C initial-repo rev-parse base >expect-head &&\n+\ttest_write_lines local >expect-untracked &&\n+\ttest_write_lines a >expect-tracked &&\n+\ttest_write_lines staged >expect-staged &&\n+\ttest_write_lines refs/heads/base >expect-branch &&\n+\tfor repo in full-checkout sparse-checkout sparse-index\n+\tdo\n+\t\t(\n+\t\t\tcd \"$repo\" &&\n+\t\t\tgit checkout -b new-file &&\n+\t\t\ttest_write_lines incoming >deep/new-file &&\n+\t\t\ttest_write_lines incoming >deep/other-file &&\n+\t\t\ttest_write_lines changed >deep/a &&\n+\t\t\tgit add deep/new-file deep/other-file deep/a &&\n+\t\t\tgit commit -m \"add a file and change a tracked file\" &&\n+\t\t\tgit checkout base &&\n+\t\t\ttest_write_lines local >deep/new-file &&\n+\t\t\ttest_write_lines local >deep/other-file &&\n+\t\t\ttest_write_lines staged >a &&\n+\t\t\tgit add a &&\n+\t\t\tgit write-tree >\"../$repo-index-before\"\n+\t\t) || return 1\n+\tdone &&\n+\ttest_all_match test_must_fail git checkout new-file &&\n+\tfor repo in full-checkout sparse-checkout sparse-index\n+\tdo\n+\t\tgit -C \"$repo\" rev-parse HEAD >actual-head &&\n+\t\ttest_cmp expect-head actual-head &&\n+\t\tgit -C \"$repo\" symbolic-ref HEAD >actual-branch &&\n+\t\ttest_cmp expect-branch actual-branch &&\n+\t\ttest_cmp expect-untracked \"$repo/deep/new-file\" &&\n+\t\ttest_cmp expect-untracked \"$repo/deep/other-file\" &&\n+\t\ttest_cmp expect-tracked \"$repo/deep/a\" &&\n+\t\ttest_cmp expect-staged \"$repo/a\" &&\n+\t\tgit -C \"$repo\" write-tree >actual-index &&\n+\t\ttest_cmp \"$repo-index-before\" actual-index || return 1\n+\tdone\n+'\n+\n test_expect_success 'checkout orphan then non-orphan' '\n \tinit_repos &&\n \ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 1802809ad3..f763f284a6 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -2058,8 +2058,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \t\t\t * correct CE_NEW_SKIP_WORKTREE\n \t\t\t */\n \t\t\tif (ce->ce_flags & CE_ADDED &&\n-\t\t\t    verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))\n+\t\t\t    verify_absent(ce,\n+\t\t\t\tERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))\n \t\t\t\tret = 1;\n+\t\t}\n+\t\tif (ret)\n+\t\t\tgoto return_failed;\n+\n+\t\tfor (i = 0; i < o->internal.result.cache_nr; i++) {\n+\t\t\tstruct cache_entry *ce = o->internal.result.cache[i];\n \n \t\t\tif (apply_sparse_checkout(&o->internal.result, ce, o))\n \t\t\t\tret = 1;\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n-- \n2.55.0\n\n\n"}]}