sparse checkout: show error messages when worktree shaping fails

verify_* functions can queue errors up and to be printed later at label return_failed. In case of errors, do not go to label "done" directly because all queued messages would be dropped on the floor. Found-by: Joshua Jensen <jjensen@workspacewhiz.com> Tracked-down-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Sep 22, 2011 at 21:24 UTC 17d26a4d0485229c24b649013a40d5928ba223cc
2 files changed +25 -3
t/t1011-read-tree-sparse-checkout.sh
+15
@@ -234,4 +234,19 @@ test_expect_success 'read-tree --reset removes outside worktree' '
234 test_cmp empty result
235 '
236
237 +test_expect_success 'print errors when failed to update worktree' '
238 + echo sub >.git/info/sparse-checkout &&
239 + git checkout -f init &&
240 + mkdir sub &&
241 + touch sub/added sub/addedtoo &&
242 + test_must_fail git checkout top 2>actual &&
243 + cat >expected <<\EOF &&
244 +error: The following untracked working tree files would be overwritten by checkout:
245 + sub/added
246 + sub/addedtoo
247 +Please move or remove them before you can switch branches.
248 +EOF
249 + test_cmp expected actual
250 +'
251 +
252 test_done
unpack-trees.c
+10 -3
@@ -1089,6 +1089,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1089 */
1090 mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1091
1092 + ret = 0;
1093 for (i = 0; i < o->result.cache_nr; i++) {
1094 struct cache_entry *ce = o->result.cache[i];
1095
@@ -1101,17 +1102,23 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1102 * correct CE_NEW_SKIP_WORKTREE
1103 */
1104 if (ce->ce_flags & CE_ADDED &&
1104 - verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
1105 - return -1;
1105 + verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
1106 + if (!o->show_all_errors)
1107 + goto return_failed;
1108 + ret = -1;
1109 + }
1110
1111 if (apply_sparse_checkout(ce, o)) {
1112 + if (!o->show_all_errors)
1113 + goto return_failed;
1114 ret = -1;
1109 - goto done;
1115 }
1116 if (!ce_skip_worktree(ce))
1117 empty_worktree = 0;
1118
1119 }
1120 + if (ret < 0)
1121 + goto return_failed;
1122 if (o->result.cache_nr && empty_worktree) {
1123 /* dubious---why should this fail??? */
1124 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");