@matheusbsbs / matheus-bagliano / commits / ace224ac5f
Fork 0

sparse-checkout: stop blocking empty workdirs

Remove the error condition when updating the sparse-checkout leaves an empty working directory. This behavior was added in 9e1afb167 (sparse checkout: inhibit empty worktree, 2009-08-20). The comment was added in a7bc906f2 (Add explanation why we do not allow to sparse checkout to empty working tree, 2011-09-22) in response to a "dubious" comment in 84563a624 (unpack-trees.c: cosmetic fix, 2010-12-22). With the recent "cone mode" and "git sparse-checkout init [--cone]" command, it is common to set a reasonable sparse-checkout pattern set of /* !/*/ which matches only files at root. If the repository has no such files, then their "git sparse-checkout init" command will fail. Now that we expect this to be a common pattern, we should not have the commands fail on an empty working directory. If it is a confusing result, then the user can recover with "git sparse-checkout disable" or "git sparse-checkout set". This is especially simple when using cone mode. Reported-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 4, 2020 at 18:27 UTC ace224ac5fb120e9cae894e31713ab60e91f141f
3 files changed +13 -41
t/t1011-read-tree-sparse-checkout.sh
+9 -3
@@ -74,13 +74,19 @@ test_expect_success 'read-tree --no-sparse-checkout with empty .git/info/sparse-
74 test_expect_success 'read-tree with empty .git/info/sparse-checkout' '
75 git config core.sparsecheckout true &&
76 echo >.git/info/sparse-checkout &&
77 - read_tree_u_must_fail -m -u HEAD &&
77 + read_tree_u_must_succeed -m -u HEAD &&
78 git ls-files --stage >result &&
79 test_cmp expected result &&
80 git ls-files -t >result &&
81 + cat >expected.swt <<-\EOF &&
82 + S init.t
83 + S sub/added
84 + S sub/addedtoo
85 + S subsub/added
86 + EOF
87 test_cmp expected.swt result &&
82 - test -f init.t &&
83 - test -f sub/added
88 + ! test -f init.t &&
89 + ! test -f sub/added
90 '
91
92 test_expect_success 'match directories with trailing slash' '
t/t1091-sparse-checkout-builtin.sh
+3 -5
@@ -106,10 +106,8 @@ test_expect_success 'set enables config' '
106 cd empty-config &&
107 test_commit test file &&
108 test_path_is_missing .git/config.worktree &&
109 - test_must_fail git sparse-checkout set nothing &&
109 + git sparse-checkout set nothing &&
110 test_path_is_file .git/config.worktree &&
111 - test_must_fail git config core.sparseCheckout &&
112 - git sparse-checkout set "/*" &&
111 test_cmp_config true core.sparseCheckout
112 )
113 '
@@ -302,8 +300,8 @@ test_expect_success 'revert to old sparse-checkout on empty update' '
300 echo >file &&
301 git add file &&
302 git commit -m "test" &&
305 - test_must_fail git sparse-checkout set nothing 2>err &&
306 - test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
303 + git sparse-checkout set nothing 2>err &&
304 + test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
305 test_i18ngrep ! ".git/index.lock" err &&
306 git sparse-checkout set file
307 )
unpack-trees.c
+1 -33
@@ -1677,8 +1677,6 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1677 }
1678
1679 if (!o->skip_sparse_checkout) {
1680 - int empty_worktree = 1;
1681 -
1680 /*
1681 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
1682 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
@@ -1706,19 +1704,6 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1704
1705 if (apply_sparse_checkout(&o->result, ce, o))
1706 ret = 1;
1709 -
1710 - if (!ce_skip_worktree(ce))
1711 - empty_worktree = 0;
1712 - }
1713 - /*
1714 - * Sparse checkout is meant to narrow down checkout area
1715 - * but it does not make sense to narrow down to empty working
1716 - * tree. This is usually a mistake in sparse checkout rules.
1717 - * Do not allow users to do that.
1718 - */
1719 - if (o->result.cache_nr && empty_worktree) {
1720 - ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
1721 - goto done;
1707 }
1708 if (ret == 1) {
1709 /*
@@ -1779,7 +1764,7 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
1764 {
1765 enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
1766 struct pattern_list pl;
1782 - int i, empty_worktree;
1767 + int i;
1768 unsigned old_show_all_errors;
1769 int free_pattern_list = 0;
1770
@@ -1810,7 +1795,6 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
1795
1796 /* Then loop over entries and update/remove as needed */
1797 ret = UPDATE_SPARSITY_SUCCESS;
1813 - empty_worktree = 1;
1798 for (i = 0; i < o->src_index->cache_nr; i++) {
1799 struct cache_entry *ce = o->src_index->cache[i];
1800
@@ -1824,28 +1808,12 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
1808
1809 if (apply_sparse_checkout(o->src_index, ce, o))
1810 ret = UPDATE_SPARSITY_WARNINGS;
1827 -
1828 - if (!ce_skip_worktree(ce))
1829 - empty_worktree = 0;
1830 - }
1831 -
1832 - /*
1833 - * Sparse checkout is meant to narrow down checkout area
1834 - * but it does not make sense to narrow down to empty working
1835 - * tree. This is usually a mistake in sparse checkout rules.
1836 - * Do not allow users to do that.
1837 - */
1838 - if (o->src_index->cache_nr && empty_worktree) {
1839 - unpack_failed(o, "Sparse checkout leaves no entry on working directory");
1840 - ret = UPDATE_SPARSITY_INDEX_UPDATE_FAILURES;
1841 - goto done;
1811 }
1812
1813 skip_sparse_checkout:
1814 if (check_updates(o, o->src_index))
1815 ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
1816
1848 -done:
1817 display_warning_msgs(o);
1818 o->show_all_errors = old_show_all_errors;
1819 if (free_pattern_list)