Git development
 help / color / mirror / Atom feed
* [PATCH] archive: separate --no-add-file and --no-add-virtual-file
@ 2026-11-22 11:59 René Scharfe
  0 siblings, 0 replies; only message in thread
From: René Scharfe @ 2026-11-22 11:59 UTC (permalink / raw)
  To: Git List

The options --add-file and --add-virtual-file of git archive add
different types of files.  They register them in the same string-list
variable though.  Their negative forms clear the whole shared list,
which is surprising.  Make them remove only the entries added by their
respective positive form instead.

We could do that by using separate lists, but that would change the
order of files in generated archives.  So do it by filtering the
string-list using the new function filter_string_list_func(), which
supports our custom item destructor extra_file_info_clear().

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 archive.c           | 14 ++++++++++++--
 string-list.c       | 18 ++++++++++++++++++
 string-list.h       | 10 ++++++++++
 t/t5000-tar-tree.sh | 18 ++++++++++++++++++
 4 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/archive.c b/archive.c
index 59790be986..33bd2c3d5c 100644
--- a/archive.c
+++ b/archive.c
@@ -559,6 +559,14 @@ static void extra_file_info_clear(void *util, const char *str UNUSED)
 	free(info);
 }
 
+static int keep_file(struct string_list_item *item, void *cb_data)
+{
+	struct extra_file_info *info = item->util;
+	bool is_regular_file = !info->content;
+	bool keep_regular_file = !!cb_data;
+	return is_regular_file == keep_regular_file;
+}
+
 static int add_file_cb(const struct option *opt, const char *arg, int unset)
 {
 	struct archiver_args *args = opt->value;
@@ -569,8 +577,10 @@ static int add_file_cb(const struct option *opt, const char *arg, int unset)
 	struct extra_file_info *info;
 
 	if (unset) {
-		string_list_clear_func(&args->extra_files,
-				       extra_file_info_clear);
+		bool keep_regular_file = !!strcmp(opt->long_name, "add-file");
+		filter_string_list_func(&args->extra_files,
+					extra_file_info_clear, keep_file,
+					keep_regular_file ? (void *)1 : NULL);
 		return 0;
 	}
 
diff --git a/string-list.c b/string-list.c
index d260b873c8..b469ba9576 100644
--- a/string-list.c
+++ b/string-list.c
@@ -151,6 +151,24 @@ void filter_string_list(struct string_list *list, int free_util,
 	list->nr = dst;
 }
 
+void filter_string_list_func(struct string_list *list,
+			     string_list_clear_func_t clearfunc,
+			     string_list_each_func_t want, void *cb_data)
+{
+	size_t dst = 0;
+	for (size_t src = 0; src < list->nr; src++) {
+		if (want(&list->items[src], cb_data)) {
+			list->items[dst++] = list->items[src];
+		} else {
+			clearfunc(list->items[src].util,
+				  list->items[src].string);
+			if (list->strdup_strings)
+				free(list->items[src].string);
+		}
+	}
+	list->nr = dst;
+}
+
 static int item_is_not_empty(struct string_list_item *item, void *data UNUSED)
 {
 	return *item->string != '\0';
diff --git a/string-list.h b/string-list.h
index b86ee7c099..b08282b78f 100644
--- a/string-list.h
+++ b/string-list.h
@@ -134,6 +134,16 @@ typedef void (*string_list_clear_func_t)(void *p, const char *str);
 /** Call a custom clear function on each util pointer */
 void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
 
+/**
+ * Apply `want` to each item in `list`, retaining only the ones for which
+ * the function returns true.  Call `clearfunc` on the util members of
+ * any items that have to be deleted.  Preserve the order of the items
+ * that are retained.
+ */
+void filter_string_list_func(struct string_list *list,
+			     string_list_clear_func_t clearfunc,
+			     string_list_each_func_t want, void *cb_data);
+
 /*
  * Set the length of a string_list to `nr`, provided that (a) `list`
  * does not own its own storage, and (b) that `nr` is no larger than
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 3ad600c793..3e181e4687 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -552,4 +552,22 @@ test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read
 	test_cmp expect actual
 '
 
+test_expect_success '--no-add-file keeps --add-virtual-file entries' '
+	echo foo >file &&
+	git archive --add-file=file --add-virtual-file=virtual:file \
+		--no-add-file $EMPTY_TREE >virtual.tar &&
+	"$TAR" tf virtual.tar >actual &&
+	echo virtual >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '--no-add-virtual-file keeps --add-file entries' '
+	echo foo >file &&
+	git archive --add-file=file --add-virtual-file=virtual:file \
+		--no-add-virtual-file $EMPTY_TREE >untracked.tar &&
+	"$TAR" tf untracked.tar >actual &&
+	echo file >expect &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.56.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-22 11:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 11:59 [PATCH] archive: separate --no-add-file and --no-add-virtual-file René Scharfe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox