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

Re: [PATCH 11/19] tree_entry_interesting: support depth limit

From
Nguyen Thai Ngoc Duy <pclouds@gmail.com>
Date
Dec 14, 2010, 14:44 UTC
Message-ID
<AANLkTikkU1h3vYDGpf4h8-9-sSWeYqtnZh1ZfB3NU3Db@mail.gmail.com>
In-Reply-To
<7vwrnd1r2s.fsf@alter.siamese.dyndns.org>
2010/12/14 Junio C Hamano <gitster@pobox.com>:
Show 21 quoted lines
>> +int within_depth(const char *name, int namelen,
>> +                     int depth, int max_depth)
>> +{
>> +     const char *cp = name, *cpe = name + namelen;
>> +
>> +     while (cp < cpe) {
>> +             if (*cp++ != '/')
>> +                     continue;
>> +             depth++;
>> +             if (depth > max_depth)
>> +                     return 0;
>> +     }
>> +     return 1;
>> +}
>
> Makes me almost suspect that it may make more sense to keep track of the
> "depth" in a similar way as "base" and "baselen" as "traversal state" on
> the side of the caller so that you do not have to scan the string for
> slashes over and over again.  But given the codeflow, doing so might make
> the result look too ugly, so I won't recommend that without thinking,
> though.
Moreover, this function is also used by match_pathspec_depth().
Show 16 quoted lines
>> -     if (!ps || !ps->nr)
>> +     if (!ps)
>>               return 1;
>>
>> +     if (!ps->nr) {
>> +             if (!ps->recursive || ps->max_depth == -1)
>> +                     return 1;
>> +             return !!within_depth(base, baselen,
>> +                                   !!S_ISDIR(entry->mode),
>> +                                   ps->max_depth);
>> +     }
>
> This gives different behaviour to between callers that give you NULL as
> pathspec and callers that give you a pathspec with zero elements.  Is this
> intended?  What is the use case (iow, what does an end user give from the
> command line to experience this difference)?

Old pathspec type's legacy. When pathspec is of "const char **", it can be NULL. When struct pathspec is used, I don't think we need to support NULL pathspec. Will remove that "if (!ps)" clause.

Show 24 quoted lines
>> @@ -571,7 +580,13 @@ int tree_entry_interesting(const struct name_entry *entry,
>>                       if (!match_dir_prefix(base, baselen, match, matchlen))
>>                               /* Just a random prefix match */
>>                               continue;
>> -                     return 2;
>> +
>> +                     if (!ps->recursive || ps->max_depth == -1)
>> +                             return 2;
>> +
>> +                     return !!within_depth(base+matchlen+1, baselen-matchlen-1,
>> +                                           !!S_ISDIR(entry->mode),
>> +                                           ps->max_depth);
>>               }
>
> If two pathspecs that overlap with each other (e.g. "Documentation/" and
> "Documentation/technical") are given, and if the shorter one comes before
> the longer one in ps[], wouldn't this give you an unexpected result?  When
> inspecting "Documentation/technical/api/foo.txt" with depth limit of 2, if
> you didn't have "Documentation/" pathspec, you count "api/foo.txt"
> relative to "Documentation/technical", declare that the path is within
> limit, and show it.  But if you have "Documentation/" in ps[], you look at
> it, decide "technical/api/foo.txt" is too deep and return false without
> even looking at "Documentation/technical" that may appear later in ps[],
> no?
Right. Hmm.. grep's pathspec_matches() probably has this problem too.
-- 
Duy
Previous: Junio C HamanoNext: Nguyễn Thái Ngọc Duy
Message 30 of 45 in “nd/struct-pathspec (or pathspec unification [1])”
  1. 00/19 nd/struct-pathspec (or pathspec unification [1])Nguyễn Thái Ngọc Duy, Dec 13, 2010
  2. 01/19 Add struct pathspecNguyễn Thái Ngọc Duy, Dec 13, 2010
  3. Thiago FarinaDec 13, 2010
  4. Nguyen Thai Ngoc DuyDec 14, 2010
  5. 02/19 diff-no-index: use diff_tree_setup_paths()Nguyễn Thái Ngọc Duy, Dec 13, 2010
  6. 03/19 pathspec: cache string length when initializing pathspecNguyễn Thái Ngọc Duy, Dec 13, 2010
  7. 04/19 Convert struct diff_options to use struct pathspecNguyễn Thái Ngọc Duy, Dec 13, 2010
  8. Junio C HamanoDec 13, 2010
  9. Nguyen Thai Ngoc DuyDec 14, 2010
  10. 05/19 tree_entry_interesting(): remove dependency on struct diff_optionsNguyễn Thái Ngọc Duy, Dec 13, 2010
  11. Junio C HamanoDec 13, 2010
  12. 06/19 Move tree_entry_interesting() to tree-walk.c and export itNguyễn Thái Ngọc Duy, Dec 13, 2010
  13. 07/19 glossary: define pathspecNguyễn Thái Ngọc Duy, Dec 13, 2010
  14. 08/19 pathspec: mark wildcard pathspecs from the beginningNguyễn Thái Ngọc Duy, Dec 13, 2010
  15. Junio C HamanoDec 13, 2010
  16. 09/19 tree-diff.c: reserve space in "base" for pathname concatenationNguyễn Thái Ngọc Duy, Dec 13, 2010
  17. Junio C HamanoDec 13, 2010
  18. Nguyen Thai Ngoc DuyDec 14, 2010
  19. Junio C HamanoDec 14, 2010
  20. Nguyen Thai Ngoc DuyDec 14, 2010
  21. Johannes SixtDec 14, 2010
  22. Nguyen Thai Ngoc DuyDec 14, 2010
  23. Johannes SixtDec 14, 2010
  24. Nguyen Thai Ngoc DuyDec 14, 2010
  25. Junio C HamanoDec 14, 2010
  26. 10/19 tree_entry_interesting(): factor out most matching logicNguyễn Thái Ngọc Duy, Dec 13, 2010
  27. Junio C HamanoDec 13, 2010
  28. 11/19 tree_entry_interesting: support depth limitNguyễn Thái Ngọc Duy, Dec 13, 2010
  29. Junio C HamanoDec 13, 2010
  30. Nguyen Thai Ngoc DuyDec 14, 2010
  31. 12/19 tree_entry_interesting(): support wildcard matchingNguyễn Thái Ngọc Duy, Dec 13, 2010
  32. Junio C HamanoDec 13, 2010
  33. Nguyen Thai Ngoc DuyDec 14, 2010
  34. 13/19 tree_entry_interesting(): optimize fnmatch when base is matchedNguyễn Thái Ngọc Duy, Dec 13, 2010
  35. Junio C HamanoDec 13, 2010
  36. 14/19 Convert ce_path_match() use to match_pathspec()Nguyễn Thái Ngọc Duy, Dec 13, 2010
  37. Junio C HamanoDec 13, 2010
  38. Nguyen Thai Ngoc DuyDec 14, 2010
  39. 15/19 pathspec: add match_pathspec_depth()Nguyễn Thái Ngọc Duy, Dec 13, 2010
  40. Junio C HamanoDec 13, 2010
  41. Nguyen Thai Ngoc DuyDec 14, 2010
  42. 16/19 grep: convert to use struct pathspecNguyễn Thái Ngọc Duy, Dec 13, 2010
  43. 17/19 grep: use match_pathspec_depth() for cache/worktree greppingNguyễn Thái Ngọc Duy, Dec 13, 2010
  44. 18/19 grep: use preallocated buffer for grep_tree()Nguyễn Thái Ngọc Duy, Dec 13, 2010
  45. 19/19 grep: drop pathspec_matches() in favor of tree_entry_interesting()Nguyễn Thái Ngọc Duy, Dec 13, 2010

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.