| 1235 | } |
| 1236 | |
| 1237 | static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option) |
| 1238 | { |
| 1239 | int pos; |
| 1240 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
| 1241 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; |
| 1242 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
| 1243 | int new_only = option & ADD_CACHE_NEW_ONLY; |
| 1244 | |
| 1245 | /* |
| 1246 | * If this entry's path sorts after the last entry in the index, |
| 1247 | * we can avoid searching for it. |
| 1248 | */ |
| 1249 | if (istate->cache_nr > 0 && |
| 1250 | strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0) |
| 1251 | pos = index_pos_to_insert_pos(istate->cache_nr); |
| 1252 | else |
| 1253 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE); |
| 1254 | |
| 1255 | /* |
| 1256 | * Cache tree path should be invalidated only after index_name_stage_pos, |
| 1257 | * in case it expands a sparse index. |
| 1258 | */ |
| 1259 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) |
| 1260 | cache_tree_invalidate_path(istate, ce->name); |
| 1261 | |
| 1262 | /* existing match? Just replace it. */ |
| 1263 | if (pos >= 0) { |
| 1264 | if (!new_only) |
| 1265 | replace_index_entry(istate, pos, ce); |
| 1266 | return 0; |
| 1267 | } |
| 1268 | pos = -pos-1; |
| 1269 | |
| 1270 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) |
| 1271 | untracked_cache_add_to_index(istate, ce->name); |
| 1272 | |
| 1273 | /* |
| 1274 | * Inserting a merged entry ("stage 0") into the index |
| 1275 | * will always replace all non-merged entries.. |
| 1276 | */ |
| 1277 | if (pos < istate->cache_nr && ce_stage(ce) == 0) { |
| 1278 | while (ce_same_name(istate->cache[pos], ce)) { |
| 1279 | ok_to_add = 1; |
| 1280 | if (!remove_index_entry_at(istate, pos)) |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | if (!ok_to_add) |
| 1286 | return -1; |
| 1287 | if (verify_path_internal(ce->name, ce->ce_mode) == PATH_INVALID) |
| 1288 | return error(_("invalid path '%s'"), ce->name); |
| 1289 | |
| 1290 | if (!skip_df_check && |
| 1291 | check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { |
| 1292 | if (!ok_to_replace) |
| 1293 | return error(_("'%s' appears as both a file and as a directory"), |
| 1294 | ce->name); |
no test coverage detected