add: don't write objects with --dry-run

When the option --dry-run/-n is given, "git add" doesn't change the index, but still writes out new object files. Only hash the latter without writing instead to make the run as dry as possible. Use this opportunity to also make the hash_flags variable unsigned, to match the index_path() parameter it is used as. Reported-by: git.mexon@spamgourmet.com Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 12, 2021 at 21:15 UTC e578d0311d7b19ebd8cdadc6941f2aa060b7a850
2 files changed +4 -1
read-cache.c
+1 -1
@@ -738,7 +738,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
738 int intent_only = flags & ADD_CACHE_INTENT;
739 int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
740 (intent_only ? ADD_CACHE_NEW_ONLY : 0));
741 - int hash_flags = HASH_WRITE_OBJECT;
741 + unsigned hash_flags = pretend ? 0 : HASH_WRITE_OBJECT;
742 struct object_id oid;
743
744 if (flags & ADD_CACHE_RENORMALIZE)
t/t2200-add-update.sh
+3
@@ -129,12 +129,15 @@ test_expect_success 'add -n -u should not add but just report' '
129 echo "remove '\''top'\''"
130 ) >expect &&
131 before=$(git ls-files -s check top) &&
132 + git count-objects -v >objects_before &&
133 echo changed >>check &&
134 rm -f top &&
135 git add -n -u >actual &&
136 after=$(git ls-files -s check top) &&
137 + git count-objects -v >objects_after &&
138
139 test "$before" = "$after" &&
140 + test_cmp objects_before objects_after &&
141 test_cmp expect actual
142
143 '