read-cache: add remove_file_from_index_with_flags()

add_file_to_index() takes flags such as ADD_CACHE_PRETEND and ADD_CACHE_VERBOSE and internally handles both reporting (e.g., "add 'path'") and suppressing index updates during dry runs. In contrast, remove_file_from_index() takes only istate and path without flags. Callers that perform file removals (such as update_callback() in read-cache.c) are forced to manually inspect ADD_CACHE_PRETEND and ADD_CACHE_VERBOSE flags for removed files. Introduce remove_file_from_index_with_flags() to encapsulate pretend mode and verbose reporting for index removals. Update update_callback() to use the new helper. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 31, 2026 at 05:56 UTC 341ce9229e9b5619b686ed9d169b54e33b29f594
2 files changed +18 -4
read-cache-ll.h
+3
@@ -391,11 +391,14 @@ int remove_index_entry_at(struct index_state *, int pos);
391
392 void remove_marked_cache_entries(struct index_state *istate, int invalidate);
393 int remove_file_from_index(struct index_state *, const char *path);
394 +int remove_file_from_index_with_flags(struct index_state *, const char *, int);
395 +
396 #define ADD_CACHE_VERBOSE 1
397 #define ADD_CACHE_PRETEND 2
398 #define ADD_CACHE_IGNORE_ERRORS 4
399 #define ADD_CACHE_IGNORE_REMOVAL 8
400 #define ADD_CACHE_INTENT 16
401 +
402 /*
403 * These two are used to add the contents of the file at path
404 * to the index, marking the working tree up-to-date by storing
read-cache.c
+15 -4
@@ -638,6 +638,20 @@ int remove_file_from_index(struct index_state *istate, const char *path)
638 return 0;
639 }
640
641 +int remove_file_from_index_with_flags(struct index_state *istate,
642 + const char *path,
643 + int flags)
644 +{
645 + int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
646 + int pretend = flags & ADD_CACHE_PRETEND;
647 +
648 + if (verbose)
649 + printf(_("remove '%s'\n"), path);
650 + if (pretend)
651 + return 0;
652 + return remove_file_from_index(istate, path);
653 +}
654 +
655 static int compare_name(struct cache_entry *ce, const char *path, int namelen)
656 {
657 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
@@ -4004,10 +4018,7 @@ static void update_callback(struct diff_queue_struct *q,
4018 case DIFF_STATUS_DELETED:
4019 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
4020 break;
4007 - if (!(data->flags & ADD_CACHE_PRETEND))
4008 - remove_file_from_index(data->index, path);
4009 - if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
4010 - printf(_("remove '%s'\n"), path);
4021 + remove_file_from_index_with_flags(data->index, path, data->flags);
4022 break;
4023 }
4024 }