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

Re: [PATCH 2/3] within_depth: fix return for empty path

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 7, 2025, 06:15 UTC
Message-ID
<aJREivx80NI08BEU@pks.im>
In-Reply-To
<87ms8cttdj.fsf@iotcl.com>
On Wed, Aug 06, 2025 at 04:30:32PM +0200, Toon Claes wrote:
Show 83 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> > Just for my own understanding: the only difference is when we don't have
> > even a single matching slash, as we don't verify `depth > max_depth` in
> > that case. So in theory, we could modify the function to the following
> > equivalent:
> >
> > 	int within_depth(const char *name, int namelen,
> > 				int depth, int max_depth)
> > 	{
> > 		const char *cp = name, *cpe = name + namelen;
> >
> > 		if (depth > max_depth)
> > 			return 0;
> >
> > 		while (cp < cpe) {
> > 			if (*cp++ != '/')
> > 				continue;
> > 			depth++;
> > 			if (depth > max_depth)
> > 				return 0;
> > 		}
> > 		return 1;
> > 	}
> >
> > (Not saying we should, I'm just double checking my understanding).
> 
> To be honest, I wasn't sure no more. I decided to see if I can write a
> unit test. This is what I came up with:
> 
>     void test_dir__within_depth(void)
>     {
>     	struct test_case {
>     		const char *path;
>     		int depth;
>     		int max_depth;
>     		int expect;
>     	} test_cases[] = {
>     		/* depth = 0; max_depth = 0 */
>     		{ "",         0, 0, 1 },
>     		{ "file",     0, 0, 1 },
>     		{ "a",        0, 0, 1 },
>     		{ "a/file",   0, 0, 0 },
>     		{ "a/b",      0, 0, 0 },
>     		{ "a/b/file", 0, 0, 0 },
> 
>     		/* depth = 0; max_depth = 1 */
>     		{ "",         0, 1, 1 },
>     		{ "file",     0, 1, 1 },
>     		{ "a",        0, 1, 1 },
>     		{ "a/file",   0, 1, 1 },
>     		{ "a/b",      0, 1, 1 },
>     		{ "a/b/file", 0, 1, 0 },
> 
>     		/* depth = 1; max_depth = 1 */
>     		{ "",         1, 1, 1 },
>     		{ "file",     1, 1, 1 },
>     		{ "a",        1, 1, 1 },
>     		{ "a/file",   1, 1, 0 },
>     		{ "a/b",      1, 1, 0 },
>     		{ "a/b/file", 1, 1, 0 },
> 
>     		/* depth = 1; max_depth = 0 */
>     		{ "",         1, 0, 0 },
>     		{ "file",     1, 0, 0 },
>     		{ "a",        1, 0, 0 },
>     		{ "a/file",   1, 0, 0 },
>     		{ "a/b",      1, 0, 0 },
>     		{ "a/b/file", 1, 0, 0 },
>     	};
> 
>     	for (size_t i = 0; i < ARRAY_SIZE(test_cases); i++) {
>     		int result = within_depth(test_cases[i].path, strlen(test_cases[i].path),
>     					  test_cases[i].depth, test_cases[i].max_depth);
>     		cl_assert_equal_b(result, test_cases[i].expect);
>     	}
>     }
> 
> The change in this patch affect the last batch of tests (the batch with
> 'depth = 1; max_depth = 0'). Running the test on both this patch and
> your suggestion gives the same results, so yes your suggestion has the
> same output.
> 
> Do you think it's worth to add such unit test?

Sure, if we can easily test this via such a unit test I think it would be a good addition.

Patrick
Previous: Toon ClaesNext: Toon Claes
Message 6 of 16 in “Teach git-diff-tree(1) option --max-depth”
  1. 0/3 Teach git-diff-tree(1) option --max-depthToon Claes, Jul 29, 2025
  2. 1/3 combine-diff: zero memory used for callback filepairsToon Claes, Jul 29, 2025
  3. 2/3 within_depth: fix return for empty pathToon Claes, Jul 29, 2025
  4. Patrick SteinhardtJul 30, 2025
  5. Toon ClaesAug 6, 2025
  6. Patrick SteinhardtAug 7, 2025
  7. 3/3 diff: teach tree-diff a max-depth parameterToon Claes, Jul 29, 2025
  8. Patrick SteinhardtJul 30, 2025
  9. Toon ClaesAug 6, 2025
  10. Patrick SteinhardtAug 7, 2025
  11. 0/3 Teach git-diff-tree(1) option --max-depthToon Claes, Aug 7, 2025
  12. 1/3 combine-diff: zero memory used for callback filepairsToon Claes, Aug 7, 2025
  13. 2/3 within_depth: fix return for empty pathToon Claes, Aug 7, 2025
  14. 3/3 diff: teach tree-diff a max-depth parameterToon Claes, Aug 7, 2025
  15. Junio C HamanoAug 14, 2025
  16. Patrick SteinhardtAug 15, 2025

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.