From: Patrick Steinhardt <ps@pks.im>
To: Toon Claes <toon@iotcl.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
Justin Tobler <jltobler@gmail.com>
Subject: Re: [PATCH 2/3] within_depth: fix return for empty path
Date: Thu, 7 Aug 2025 08:15:38 +0200 [thread overview]
Message-ID: <aJREivx80NI08BEU@pks.im> (raw)
In-Reply-To: <87ms8cttdj.fsf@iotcl.com>
On Wed, Aug 06, 2025 at 04:30:32PM +0200, Toon Claes wrote:
> 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
next prev parent reply other threads:[~2025-08-07 6:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-29 18:57 [PATCH 0/3] Teach git-diff-tree(1) option --max-depth Toon Claes
2025-07-29 18:57 ` [PATCH 1/3] combine-diff: zero memory used for callback filepairs Toon Claes
2025-07-29 18:57 ` [PATCH 2/3] within_depth: fix return for empty path Toon Claes
2025-07-30 6:20 ` Patrick Steinhardt
2025-08-06 14:30 ` Toon Claes
2025-08-07 6:15 ` Patrick Steinhardt [this message]
2025-07-29 18:57 ` [PATCH 3/3] diff: teach tree-diff a max-depth parameter Toon Claes
2025-07-30 6:20 ` Patrick Steinhardt
2025-08-06 14:49 ` Toon Claes
2025-08-07 5:55 ` Patrick Steinhardt
2025-08-07 20:52 ` [PATCH v2 0/3] Teach git-diff-tree(1) option --max-depth Toon Claes
2025-08-07 20:52 ` [PATCH v2 1/3] combine-diff: zero memory used for callback filepairs Toon Claes
2025-08-07 20:52 ` [PATCH v2 2/3] within_depth: fix return for empty path Toon Claes
2025-08-07 20:52 ` [PATCH v2 3/3] diff: teach tree-diff a max-depth parameter Toon Claes
2025-08-14 15:15 ` [PATCH v2 0/3] Teach git-diff-tree(1) option --max-depth Junio C Hamano
2025-08-15 5:17 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aJREivx80NI08BEU@pks.im \
--to=ps@pks.im \
--cc=git@vger.kernel.org \
--cc=jltobler@gmail.com \
--cc=peff@peff.net \
--cc=toon@iotcl.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.