git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 4/4] upload-pack.c: introduce 'uploadpackfilter.tree.maxDepth'

From
Taylor Blau <me@ttaylorr.com>
Date
Jul 31, 2020, 20:26 UTC
Message-ID
<c9d71809f49dc1b600b75769e0b6aae17382bf4e.1596227003.git.me@ttaylorr.com>
In-Reply-To
<cover.1596227003.git.me@ttaylorr.com>

In b79cf959b2 (upload-pack.c: allow banning certain object filter(s), 2020-02-26), we introduced functionality to disallow certain object filters from being chosen from within 'git upload-pack'. Traditionally, administrators use this functionality to disallow filters that are known to perform slowly, for e.g., those that do not have bitmap-level filtering.

In the past, the '--filter=tree:<n>' was one such filter that does not have bitmap-level filtering support, and so was likely to be banned by administrators.

However, in the previous couple of commits, we introduced bitmap-level filtering for the case when 'n' is equal to '0', i.e., as if we had a '--filter=tree:none' choice.

While it would be sufficient to simply write
  $ git config uploadpackfilter.tree.allow true

(since it would allow all values of 'n'), we would like to be able to allow this filter for certain values of 'n', i.e., those no greater than some pre-specified maximum.

In order to do this, introduce a new configuration key, as follows:
  $ git config uploadpackfilter.tree.maxDepth <m>

where '<m>' specifies the maximum allowed value of 'n' in the filter 'tree:n'. Administrators who wish to allow for only the value '0' can write:

  $ git config uploadpackfilter.tree.allow true
  $ git config uploadpackfilter.tree.maxDepth 0
which allows '--filter=tree:0', but no other values.

Unfortunately, since the tree depth is an unsigned long, we can't use, say, -1 as a sentinel value, and so we must also keep track of "have we set this" as well as "to what value".

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 Documentation/config/uploadpack.txt |  6 ++++++
 t/t5616-partial-clone.sh            |  9 +++++++++
 upload-pack.c                       | 27 ++++++++++++++++++++++++++-
 3 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/Documentation/config/uploadpack.txt b/Documentation/config/uploadpack.txt
index fffe8ac648..ee7b3ac94f 100644
--- a/Documentation/config/uploadpack.txt
+++ b/Documentation/config/uploadpack.txt
@@ -69,6 +69,12 @@ uploadpackfilter.<filter>.allow::
 	combined filters, both `combine` and all of the nested filter
 	kinds must be allowed. Defaults to `uploadpackfilter.allow`.
 
+uploadpackfilter.tree.maxDepth::
+	Only allow `--filter=tree=<n>` when `n` is no more than the value of
+	`uploadpackfilter.tree.maxDepth`. If set, this also implies
+	`uploadpackfilter.tree.allow=true`, unless this configuration
+	variable had already been set. Has no effect if unset.
+
 uploadpack.allowRefInWant::
 	If this option is set, `upload-pack` will support the `ref-in-want`
 	feature of the protocol version 2 `fetch` command.  This feature
diff --git a/t/t5616-partial-clone.sh b/t/t5616-partial-clone.sh
index 0d46b5a2f8..35cb6a34a3 100755
--- a/t/t5616-partial-clone.sh
+++ b/t/t5616-partial-clone.sh
@@ -259,6 +259,15 @@ test_expect_success 'upload-pack fails banned object filters with fallback' '
 	test_i18ngrep "filter '\''blob:none'\'' not supported" err
 '
 
+test_expect_success 'upload-pack limits tree depth filters' '
+	test_config -C srv.bare uploadpackfilter.allow false &&
+	test_config -C srv.bare uploadpackfilter.tree.allow true &&
+	test_config -C srv.bare uploadpackfilter.tree.maxDepth 0 &&
+	test_must_fail ok=sigpipe git clone --no-checkout --filter=tree:1 \
+		"file://$(pwd)/srv.bare" pc3 2>err &&
+	test_i18ngrep "filter '\''tree'\'' not supported (maximum depth: 0, but got: 1)" err
+'
+
 test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
 	rm -rf src dst &&
 	git init src &&
diff --git a/upload-pack.c b/upload-pack.c
index 5fa22da31f..131445b212 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -105,6 +105,7 @@ struct upload_pack_data {
 	unsigned use_include_tag : 1;
 	unsigned allow_filter : 1;
 	unsigned allow_filter_fallback : 1;
+	unsigned long tree_filter_max_depth;
 
 	unsigned done : 1;					/* v2 only */
 	unsigned allow_ref_in_want : 1;				/* v2 only */
@@ -136,6 +137,7 @@ static void upload_pack_data_init(struct upload_pack_data *data)
 	data->extra_edge_obj = extra_edge_obj;
 	data->allowed_filters = allowed_filters;
 	data->allow_filter_fallback = 1;
+	data->tree_filter_max_depth = ULONG_MAX;
 	packet_writer_init(&data->writer, 1);
 
 	data->keepalive = 5;
@@ -996,8 +998,17 @@ static int allows_filter_choice(struct upload_pack_data *data,
 	const char *key = list_object_filter_config_name(opts->choice);
 	struct string_list_item *item = string_list_lookup(&data->allowed_filters,
 							   key);
+	int allowed = -1;
 	if (item)
-		return (intptr_t) item->util;
+		allowed = (intptr_t) item->util;
+
+	if (allowed != 0 &&
+	    opts->choice == LOFC_TREE_DEPTH &&
+	    opts->tree_exclude_depth > data->tree_filter_max_depth)
+		return 0;
+
+	if (allowed > -1)
+		return allowed;
 	return data->allow_filter_fallback;
 }
 
@@ -1029,6 +1040,11 @@ static void die_if_using_banned_filter(struct upload_pack_data *data)
 
 	strbuf_addf(&buf, "git upload-pack: filter '%s' not supported",
 		    list_object_filter_config_name(banned->choice));
+	if (banned->choice == LOFC_TREE_DEPTH &&
+	    data->tree_filter_max_depth != ULONG_MAX)
+		strbuf_addf(&buf, _(" (maximum depth: %lu, but got: %lu)"),
+			    data->tree_filter_max_depth,
+			    banned->tree_exclude_depth);
 
 	packet_writer_error(&data->writer, "%s\n", buf.buf);
 	die("%s", buf.buf);
@@ -1243,6 +1259,15 @@ static int parse_object_filter_config(const char *var, const char *value,
 	if (!strcmp(key, "allow"))
 		string_list_insert(&data->allowed_filters, buf.buf)->util =
 			(void *)(intptr_t)git_config_bool(var, value);
+	else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
+		if (!value) {
+			strbuf_release(&buf);
+			return config_error_nonbool(var);
+		}
+		string_list_insert(&data->allowed_filters, buf.buf)->util =
+			(void *)(intptr_t)1;
+		data->tree_filter_max_depth = git_config_ulong(var, value);
+	}
 
 	strbuf_release(&buf);
 	return 0;
-- 
2.28.0.rc1.13.ge78abce653
Previous: Taylor BlauNext: Jeff King
Message 21 of 32 in “upload-pack: custom allowed object filters”
  1. 0/4 upload-pack: custom allowed object filtersTaylor Blau, Jul 23, 2020
  2. 1/4 list_objects_filter_options: introduce 'list_object_filter_config_name'Taylor Blau, Jul 23, 2020
  3. 2/4 upload-pack.c: allow banning certain object filter(s)Taylor Blau, Jul 23, 2020
  4. 3/4 upload-pack.c: pass 'struct list_objects_filter_options *'Taylor Blau, Jul 23, 2020
  5. 4/4 upload-pack.c: introduce 'uploadpackfilter.tree.maxDepth'Taylor Blau, Jul 23, 2020
  6. SZEDER GáborJul 23, 2020
  7. Taylor BlauJul 24, 2020
  8. Jeff KingJul 24, 2020
  9. Taylor BlauJul 27, 2020
  10. SZEDER GáborJul 27, 2020
  11. Taylor BlauJul 27, 2020
  12. Jeff KingJul 27, 2020
  13. SZEDER GáborJul 27, 2020
  14. Taylor BlauJul 27, 2020
  15. 0/4 upload-pack: custom allowed object filtersTaylor Blau, Jul 31, 2020
  16. 1/4 list_objects_filter_options: introduce 'list_object_filter_config_name'Taylor Blau, Jul 31, 2020
  17. 2/4 upload-pack.c: allow banning certain object filter(s)Taylor Blau, Jul 31, 2020
  18. Jeff KingJul 31, 2020
  19. Taylor BlauJul 31, 2020
  20. 3/4 upload-pack.c: pass 'struct list_objects_filter_options *'Taylor Blau, Jul 31, 2020
  21. 4/4 upload-pack.c: introduce 'uploadpackfilter.tree.maxDepth'Taylor Blau, Jul 31, 2020
  22. Jeff KingJul 31, 2020
  23. Jeff KingJul 31, 2020
  24. Taylor BlauJul 31, 2020
  25. Taylor BlauJul 31, 2020
  26. Jeff KingJul 31, 2020
  27. Jeff KingJul 31, 2020
  28. 0/3 upload-pack: custom allowed object filtersTaylor Blau, Aug 3, 2020
  29. 2/3 upload-pack.c: allow banning certain object filter(s)Taylor Blau, Aug 3, 2020
  30. 1/3 list_objects_filter_options: introduce 'list_object_filter_config_name'Taylor Blau, Aug 3, 2020
  31. 3/3 upload-pack.c: introduce 'uploadpackfilter.tree.maxDepth'Taylor Blau, Aug 3, 2020
  32. Jeff KingAug 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.