{"thread":{"id":"66912","subject":"[PATCH] archive: separate --no-add-file and --no-add-virtual-file","startedAt":"2026-11-22T11:59:40Z","lastAt":"2026-11-22T11:59:40Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559092","messageId":"25761d5e-b8ac-404b-9c50-763595c17522@web.de","threadId":"66912","inReplyTo":null,"subject":"[PATCH] archive: separate --no-add-file and --no-add-virtual-file","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2026-11-22T11:59:40Z","receivedAt":"2026-11-22T11:59:40Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"The options --add-file and --add-virtual-file of git archive add\ndifferent types of files.  They register them in the same string-list\nvariable though.  Their negative forms clear the whole shared list,\nwhich is surprising.  Make them remove only the entries added by their\nrespective positive form instead.\n\nWe could do that by using separate lists, but that would change the\norder of files in generated archives.  So do it by filtering the\nstring-list using the new function filter_string_list_func(), which\nsupports our custom item destructor extra_file_info_clear().\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n archive.c           | 14 ++++++++++++--\n string-list.c       | 18 ++++++++++++++++++\n string-list.h       | 10 ++++++++++\n t/t5000-tar-tree.sh | 18 ++++++++++++++++++\n 4 files changed, 58 insertions(+), 2 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex 59790be986..33bd2c3d5c 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -559,6 +559,14 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)\n \tfree(info);\n }\n \n+static int keep_file(struct string_list_item *item, void *cb_data)\n+{\n+\tstruct extra_file_info *info = item->util;\n+\tbool is_regular_file = !info->content;\n+\tbool keep_regular_file = !!cb_data;\n+\treturn is_regular_file == keep_regular_file;\n+}\n+\n static int add_file_cb(const struct option *opt, const char *arg, int unset)\n {\n \tstruct archiver_args *args = opt->value;\n@@ -569,8 +577,10 @@ static int add_file_cb(const struct option *opt, const char *arg, int unset)\n \tstruct extra_file_info *info;\n \n \tif (unset) {\n-\t\tstring_list_clear_func(&args->extra_files,\n-\t\t\t\t       extra_file_info_clear);\n+\t\tbool keep_regular_file = !!strcmp(opt->long_name, \"add-file\");\n+\t\tfilter_string_list_func(&args->extra_files,\n+\t\t\t\t\textra_file_info_clear, keep_file,\n+\t\t\t\t\tkeep_regular_file ? (void *)1 : NULL);\n \t\treturn 0;\n \t}\n \ndiff --git a/string-list.c b/string-list.c\nindex d260b873c8..b469ba9576 100644\n--- a/string-list.c\n+++ b/string-list.c\n@@ -151,6 +151,24 @@ void filter_string_list(struct string_list *list, int free_util,\n \tlist->nr = dst;\n }\n \n+void filter_string_list_func(struct string_list *list,\n+\t\t\t     string_list_clear_func_t clearfunc,\n+\t\t\t     string_list_each_func_t want, void *cb_data)\n+{\n+\tsize_t dst = 0;\n+\tfor (size_t src = 0; src < list->nr; src++) {\n+\t\tif (want(&list->items[src], cb_data)) {\n+\t\t\tlist->items[dst++] = list->items[src];\n+\t\t} else {\n+\t\t\tclearfunc(list->items[src].util,\n+\t\t\t\t  list->items[src].string);\n+\t\t\tif (list->strdup_strings)\n+\t\t\t\tfree(list->items[src].string);\n+\t\t}\n+\t}\n+\tlist->nr = dst;\n+}\n+\n static int item_is_not_empty(struct string_list_item *item, void *data UNUSED)\n {\n \treturn *item->string != '\\0';\ndiff --git a/string-list.h b/string-list.h\nindex b86ee7c099..b08282b78f 100644\n--- a/string-list.h\n+++ b/string-list.h\n@@ -134,6 +134,16 @@ typedef void (*string_list_clear_func_t)(void *p, const char *str);\n /** Call a custom clear function on each util pointer */\n void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);\n \n+/**\n+ * Apply `want` to each item in `list`, retaining only the ones for which\n+ * the function returns true.  Call `clearfunc` on the util members of\n+ * any items that have to be deleted.  Preserve the order of the items\n+ * that are retained.\n+ */\n+void filter_string_list_func(struct string_list *list,\n+\t\t\t     string_list_clear_func_t clearfunc,\n+\t\t\t     string_list_each_func_t want, void *cb_data);\n+\n /*\n  * Set the length of a string_list to `nr`, provided that (a) `list`\n  * does not own its own storage, and (b) that `nr` is no larger than\ndiff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh\nindex 3ad600c793..3e181e4687 100755\n--- a/t/t5000-tar-tree.sh\n+++ b/t/t5000-tar-tree.sh\n@@ -552,4 +552,22 @@ test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read\n \ttest_cmp expect actual\n '\n \n+test_expect_success '--no-add-file keeps --add-virtual-file entries' '\n+\techo foo >file &&\n+\tgit archive --add-file=file --add-virtual-file=virtual:file \\\n+\t\t--no-add-file $EMPTY_TREE >virtual.tar &&\n+\t\"$TAR\" tf virtual.tar >actual &&\n+\techo virtual >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '--no-add-virtual-file keeps --add-file entries' '\n+\techo foo >file &&\n+\tgit archive --add-file=file --add-virtual-file=virtual:file \\\n+\t\t--no-add-virtual-file $EMPTY_TREE >untracked.tar &&\n+\t\"$TAR\" tf untracked.tar >actual &&\n+\techo file >expect &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.56.1\n\n"}]}