|
27 | 27 | #include "submodule-config.h"
|
28 | 28 | #include "strbuf.h"
|
29 | 29 | #include "quote.h"
|
| 30 | +#include "dir.h" |
30 | 31 |
|
31 | 32 | #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
|
32 | 33 |
|
@@ -130,12 +131,45 @@ static void update_index_from_diff(struct diff_queue_struct *q,
|
130 | 131 | struct diff_options *opt, void *data)
|
131 | 132 | {
|
132 | 133 | int i;
|
| 134 | + int pos; |
133 | 135 | int intent_to_add = *(int *)data;
|
134 | 136 |
|
135 | 137 | for (i = 0; i < q->nr; i++) {
|
136 | 138 | struct diff_filespec *one = q->queue[i]->one;
|
| 139 | + struct diff_filespec *two = q->queue[i]->two; |
137 | 140 | int is_missing = !(one->mode && !is_null_oid(&one->oid));
|
| 141 | + int was_missing = !two->mode && is_null_oid(&two->oid); |
138 | 142 | struct cache_entry *ce;
|
| 143 | + struct cache_entry *ceBefore; |
| 144 | + struct checkout state = CHECKOUT_INIT; |
| 145 | + |
| 146 | + /* |
| 147 | + * When using the sparse-checkout feature the cache entries that are |
| 148 | + * added here will not have the skip-worktree bit set. |
| 149 | + * Without this code there is data that is lost because the files that |
| 150 | + * would normally be in the working directory are not there and show as |
| 151 | + * deleted for the next status or in the case of added files just disappear. |
| 152 | + * We need to create the previous version of the files in the working |
| 153 | + * directory so that they will have the right content and the next |
| 154 | + * status call will show modified or untracked files correctly. |
| 155 | + */ |
| 156 | + if (core_apply_sparse_checkout && !file_exists(two->path)) |
| 157 | + { |
| 158 | + pos = cache_name_pos(two->path, strlen(two->path)); |
| 159 | + if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) && (is_missing || !was_missing)) |
| 160 | + { |
| 161 | + state.force = 1; |
| 162 | + state.refresh_cache = 1; |
| 163 | + state.istate = &the_index; |
| 164 | + ceBefore = make_cache_entry(&the_index, two->mode, &two->oid, two->path, |
| 165 | + 0, 0); |
| 166 | + if (!ceBefore) |
| 167 | + die(_("make_cache_entry failed for path '%s'"), |
| 168 | + two->path); |
| 169 | + |
| 170 | + checkout_entry(ceBefore, &state, NULL, NULL); |
| 171 | + } |
| 172 | + } |
139 | 173 |
|
140 | 174 | if (is_missing && !intent_to_add) {
|
141 | 175 | remove_file_from_cache(one->path);
|
|
0 commit comments