Skip to contentSkip to conversation

archive: separate --no-add-file and --no-add-virtual-file

PATCH New René Scharfe 1 participant 1 message Nov 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 archive: separate --no-add-file and --no-add-virtual-file

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().

---
 archive.c           | 14 ++++++++++++--
 string-list.c       | 18 ++++++++++++++++++
 string-list.h       | 10 ++++++++++
 t/t5000-tar-tree.sh | 18 ++++++++++++++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
archive.c +12 -2
index 59790be986..33bd2c3d5c 100644
@@ -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;
 	}
 
string-list.c +18 -0
index d260b873c8..b469ba9576 100644
@@ -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';
string-list.h +10 -0
index b86ee7c099..b08282b78f 100644
@@ -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
t/t5000-tar-tree.sh +18 -0
index 3ad600c793..3e181e4687 100755
@@ -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
Signature · 1 line
2.56.1