threads / patch / 51047

patchmake slash-rules more readable

Subject: [PATCH] make slash-rules more readable

## tl;dr

6 messages between May 7, 2019 and May 18, 2019. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Dr. Adam Nielsen· May 7, 2019, 10:45 UTC · lore
gitignore.txt: make slash-rules more readable

Make all paragraphs valid, even if they are not read in strict order. Make paragraph better understandable for pattern without slash. Add paragraph for pattern with slash. Be precise whenever a trailing slashes would make a difference. Add some examples.

Signed-off-by: Dr. Adam Nielsen <admin@in-ici.net>
---
 Documentation/gitignore.txt | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)
Show changes to Documentation/gitignore.txt +24 −13
diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index b5bc9dbff0..7d7fbd202e 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -89,24 +89,35 @@ PATTERN FORMAT
    Put a backslash ("`\`") in front of the first "`!`" for patterns
    that begin with a literal "`!`", for example, "`\!important!.txt`".
 
- - If the pattern ends with a slash, it is removed for the
-   purpose of the following description, but it would only find
+ - If the pattern ends with a slash, it would only find
    a match with a directory.  In other words, `foo/` will match a
    directory `foo` and paths underneath it, but will not match a
    regular file or a symbolic link `foo` (this is consistent
    with the way how pathspec works in general in Git).
 
- - If the pattern does not contain a slash '/', Git treats it as
-   a shell glob pattern and checks for a match against the
-   pathname relative to the location of the `.gitignore` file
-   (relative to the toplevel of the work tree if not from a
-   `.gitignore` file).
-
- - Otherwise, Git treats the pattern as a shell glob: "`*`" matches
-   anything except "`/`", "`?`" matches any one character except "`/`"
-   and "`[]`" matches one character in a selected range. See
-   fnmatch(3) and the FNM_PATHNAME flag for a more detailed
-   description.
+ - If the pattern contains no slash "`/`" (except an optional trailing slash),
+   the pattern is matched against all files and folders (recursively)
+   from the location of the `.gitignore` file.
+   For example, `frotz/` matches `frotz` and `a/frotz` that
+   is a directory (relative from the `.gitignore` file).
+
+ - A pattern that contains a non-trailing slash is matched
+   relative to the location of the `.gitignore` file.
+   For example, `doc/frotz/` matches `doc/frotz` directory, but not
+   `a/doc/frotz` (relative from the `.gitignore` file).
+   Note that the pattern `doc/frotz` and `/doc/frotz` have the
+   same effect in any `.gitignore` file, while `/bar` and `bar`
+   have not the same effect (`/bar` will not match `foo/bar`).
+
+ - An asterisk "`*`" matches anything except a slash.
+   A pattern "foo/*", for example, matches "foo/test.json"
+   (a regular file), "foo/bar" (a diretory), but it does not match
+   "foo/bar/hello.c" (a regular file), as the asterisk in the
+   patter does not match "bar/hello.c" which has a slash in it.
+   The character "`?`" matches any one character except "`/`".
+   The range notation, e.g. `[a-zA-Z]`, can be used to match
+   one of the characters in a range. See fnmatch(3) and the
+   FNM_PATHNAME flag for a more detailed description.
 
  - A leading slash matches the beginning of the pathname.
    For example, "/{asterisk}.c" matches "cat-file.c" but not
-- 
2.17.1
Junio C Hamano· May 8, 2019, 05:33 UTC · re: Dr. Adam Nielsen · lore

Re: [PATCH] make slash-rules more readable

"Dr. Adam Nielsen" <admin@in-ici.net> writes:
Show 6 quoted lines
> + - A pattern that contains a non-trailing slash is matched
> +   For example, `doc/frotz/` matches `doc/frotz` directory, but not
> +   `a/doc/frotz` (relative from the `.gitignore` file).
> +   Note that the pattern `doc/frotz` and `/doc/frotz` have the
> +   same effect in any `.gitignore` file, while `/bar` and `bar`
> +   have not the same effect (`/bar` will not match `foo/bar`).

The "note" is not incorrect per-se. The behaviour described is because the leading slash is removed for the purpose of textual matching against paths, but still counts as a non-trailing slash for the purpose of anchoring the pattern to the level of recursion.

I am not sure if that is obvious to the readers, though. Especially because the "a leading slash matches the beginning of ..." which was in the original is still left and appears two bullet points after this one, the presentation order seem a bit suboptimal.

How about deleting that "A leading slash matches the beginning..." bullet, and then splitting the above bullet into two? That is

- A pattern that contains a non-trailing slash is matched
  For example, `doc/frotz/` matches `doc/frotz` directory, but not
  `a/doc/frotz` (relative from the `.gitignore` file).
- A leading slash, if any, is implicitly removed before matching the
  pattern with the pathname, but the pattern still counts as having
  a non-trailing slash for the purpose of the above rule.  For
  example, a pattern `{asterisk}.c` does not have any slash in it,
  so it would match a file or a directory whose name ends with `.c`
  anywhere in the directory that has `.gitignore` file in it
  (e.g. `sub/foo.c`, `bar.c`). By prefixing a slash to make it
  `/{asterisk}.c`, it can be limited to match only at the current
  level (i.e. `bar.c` but not `sub/foo.c`).
Show 5 quoted lines
> + - An asterisk "`*`" matches anything except a slash.
> +   A pattern "foo/*", for example, matches "foo/test.json"
> +   (a regular file), "foo/bar" (a diretory), but it does not match
> +   "foo/bar/hello.c" (a regular file), as the asterisk in the
> +   patter does not match "bar/hello.c" which has a slash in it.
s/patter/&n/
Show 7 quoted lines
> +   The character "`?`" matches any one character except "`/`".
> +   The range notation, e.g. `[a-zA-Z]`, can be used to match
> +   one of the characters in a range. See fnmatch(3) and the
> +   FNM_PATHNAME flag for a more detailed description.
>  
>   - A leading slash matches the beginning of the pathname.
>     For example, "/{asterisk}.c" matches "cat-file.c" but not
Then this last paragraph can be removed.
Dr. Adam Nielsen· May 12, 2019, 09:56 UTC · re: Junio C Hamano · lore

Re: [PATCH] make slash-rules more readable

On 08.05.19 07:33, Junio C Hamano wrote:
Show 15 quoted lines
> "Dr. Adam Nielsen" <admin@in-ici.net> writes:
> 
>> + - A pattern that contains a non-trailing slash is matched  relative to the location of the `.gitignore` file.
>> +   For example, `doc/frotz/` matches `doc/frotz` directory, but not
>> +   `a/doc/frotz` (relative from the `.gitignore` file).
>> +   Note that the pattern `doc/frotz` and `/doc/frotz` have the
>> +   same effect in any `.gitignore` file, while `/bar` and `bar`
>> +   have not the same effect (`/bar` will not match `foo/bar`).
> 
> The "note" is not incorrect per-se.  The behaviour described is
> because the leading slash is removed for the purpose of textual
> matching against paths, but still counts as a non-trailing slash for
> the purpose of anchoring the pattern to the level of recursion.
> 
> I am not sure if that is obvious to the readers, though.

Yes, its not explained to the reader that the leading slash is removed for the purpose of textual matching. But maybe this is not necessary in order to understand the effect of the pattern.

>  Especially
> because the "a leading slash matches the beginning of ..." which was
> in the original is still left and appears two bullet points after
> this one, the presentation order seem a bit suboptimal.

I agree. The paragraph "a leading slash matches the beginning of ..." should be deleted, because its already covered by the top rule plus an example.

Show 5 quoted lines
> 
> How about deleting that "A leading slash matches the beginning..."
> bullet, and then splitting the above bullet into two?  That is
> 
> - A pattern that contains a non-trailing slash is matched
is matched relative to the location of the `.gitignore` file.
>    For example, `doc/frotz/` matches `doc/frotz` directory, but not
>    `a/doc/frotz` (relative from the `.gitignore` file).
> 

I agree that the case of a leading slash is important and deserves its own paragraph, especially if we remove the last bullet.

> - A leading slash, if any, is implicitly removed before matching the
>    pattern with the pathname, but the pattern still counts as having
>    a non-trailing slash for the purpose of the above rule.  For

I would try to avoid ambiguous words like `implicitly removed ` and `pathname` that have not been used before. Also I am not sure if explaining the reader how the algorithm works is the best approach.

>    example, a pattern `{asterisk}.c` does not have any slash in it,
>    so it would match a file or a directory whose name ends with `.c`
>    anywhere in the directory that has `.gitignore` file in it
>    (e.g. `sub/foo.c`, `bar.c`).

A similar example is already in the "If the pattern contains no slash.." paragraph. I think it takes a bit too much space just to explain the difference when a leading slash appears.

> By prefixing a slash to make it
>    `/{asterisk}.c`, it can be limited to match only at the current
>    level (i.e. `bar.c` but not `sub/foo.c`).
How about we split it like this:
   - A pattern that contains a non-trailing slash is matched
     relative to the location of the `.gitignore` file.
     For example, `doc/frotz/` matches `doc/frotz` directory, but not
     `a/doc/frotz` (relative from the `.gitignore` file; note that the
     example has a trailing and a non-trailing slash at the same time).
   - Note: A pattern with a leading slash has a non-trailing slash
     and is therefore effected by the previous paragraph.
     For example, the pattern `/bar` only matches the file or
     folder `bar` that is at the same location as the `gitignore` file.
     Whereas the pattern `bar` would also match in folders below the
     `gitignore`  file.
     On the other hand,  the pattern `doc/frotz` and `/doc/frotz`
     have the same effect in any `.gitignore` file, because both
     have a non-trailing slash.
Show 18 quoted lines
> 
>> + - An asterisk "`*`" matches anything except a slash.
>> +   A pattern "foo/*", for example, matches "foo/test.json"
>> +   (a regular file), "foo/bar" (a diretory), but it does not match
>> +   "foo/bar/hello.c" (a regular file), as the asterisk in the
>> +   patter does not match "bar/hello.c" which has a slash in it.
> 
> s/patter/&n/
> 
>> +   The character "`?`" matches any one character except "`/`".
>> +   The range notation, e.g. `[a-zA-Z]`, can be used to match
>> +   one of the characters in a range. See fnmatch(3) and the
>> +   FNM_PATHNAME flag for a more detailed description.
>>   
>>    - A leading slash matches the beginning of the pathname.
>>      For example, "/{asterisk}.c" matches "cat-file.c" but not
> 
> Then this last paragraph can be removed.

Agree. -

Another thing that I noticed is that its not mentioned anywhere that the pattern use a slash as a directory separator (instead of a backslash), its only clear from the examples. Maybe its worth to mention it in the "PATTERN FORMAT" section. Also its maybe worth to introduce the term "leading slash" and "trailing slash" because they will be of importance of the following paragraphs. Something like this after the paragraph of "!":

     [...] for example, "\!important!.txt".
     A slash `/` is used as a directory separator.
     A leading slash (that is if the pattern begins with a slash)
     or a trailing slash (that is if the pattern ends with a slash)
     have special meaning and are explained below.
     If the pattern contains a trailing slash, it would only find
     a match with a directory. [...]
Dr. Adam Nielsen· May 17, 2019, 21:43 UTC · re: Dr. Adam Nielsen · lore

Re: [PATCH] make slash-rules more readable

Show 18 quoted lines
> Another thing that I noticed is that its not mentioned anywhere that the 
> pattern use a slash as a directory separator (instead of a backslash), 
> its only clear from the examples. Maybe its worth to mention it in the 
> "PATTERN FORMAT" section. Also its maybe worth to introduce the term 
> "leading slash" and "trailing slash" because they will be of importance 
> of the following paragraphs. Something like this after the paragraph of 
> "!":
> 
>      [...] for example, "\!important!.txt".
> 
>      A slash `/` is used as a directory separator.
>      A leading slash (that is if the pattern begins with a slash)
>      or a trailing slash (that is if the pattern ends with a slash)
>      have special meaning and are explained below.
> 
>      If the pattern contains a trailing slash, it would only find
>      a match with a directory. [...]
> 

I changed my mind about this last addition. I think it is not very readable and there is no need to explain leading/trailing slash. Maybe one could just note it like this:

       [...] for example, "\!important!.txt".
       A slash `/` is used as a directory separator.
       A leading and trailing slash have special meaning
       and are explained in the following.
       If the pattern ends with a slash, it would only find
       a match with a directory. [...]
then I would also add:
      If the pattern does not end with a slash, it would find a match
      with a file or directory.
Two notes about two sentences that I proposed a while ago:
 > + - If the pattern contains no slash "`/`" (except an optional 
trailing slash),
 > +   the ...

I think that this sentence is not very readable. The exceptional case in the brackets makes it over complicated.

 > + - A pattern that contains a non-trailing slash is matched

And I don't like this phrase either. I think its too easy to confuse it with "A pattern that contains no trailing slash".

So I would suggest to replace both with the following:
     If the pattern contains no slash or only a trailing slash, [...].
     Otherwise (when it contains a non-trailing slash) the pattern
     is matched [...].

All the best, Adam

Johannes Sixt· May 18, 2019, 06:42 UTC · re: Dr. Adam Nielsen · lore

Re: [PATCH] make slash-rules more readable

Am 17.05.19 um 23:43 schrieb Dr. Adam Nielsen:
Show 58 quoted lines
>> Another thing that I noticed is that its not mentioned anywhere that
>> the pattern use a slash as a directory separator (instead of a
>> backslash), its only clear from the examples. Maybe its worth to
>> mention it in the "PATTERN FORMAT" section. Also its maybe worth to
>> introduce the term "leading slash" and "trailing slash" because they
>> will be of importance of the following paragraphs. Something like this
>> after the paragraph of "!":
>>
>>      [...] for example, "\!important!.txt".
>>
>>      A slash `/` is used as a directory separator.
>>      A leading slash (that is if the pattern begins with a slash)
>>      or a trailing slash (that is if the pattern ends with a slash)
>>      have special meaning and are explained below.
>>
>>      If the pattern contains a trailing slash, it would only find
>>      a match with a directory. [...]
>>
> 
> 
> I changed my mind about this last addition. I think it is not very
> readable and there is no need to explain leading/trailing slash. Maybe
> one could just note it like this:
> 
>       [...] for example, "\!important!.txt".
> 
>       A slash `/` is used as a directory separator.
>       A leading and trailing slash have special meaning
>       and are explained in the following.
> 
>       If the pattern ends with a slash, it would only find
>       a match with a directory. [...]
> 
> then I would also add:
> 
>      If the pattern does not end with a slash, it would find a match
>      with a file or directory.
> 
> 
> Two notes about two sentences that I proposed a while ago:
> 
>> + - If the pattern contains no slash "`/`" (except an optional
> trailing slash),
>> +   the ...
> 
> I think that this sentence is not very readable. The exceptional case in
> the brackets makes it over complicated.
> 
>> + - A pattern that contains a non-trailing slash is matched
> 
> And I don't like this phrase either. I think its too easy to confuse it
> with "A pattern that contains no trailing slash".
> 
> So I would suggest to replace both with the following:
> 
>     If the pattern contains no slash or only a trailing slash, [...].
>     Otherwise (when it contains a non-trailing slash) the pattern
>     is matched [...].

With all those new "if"s, "but"s, "otherwise"s, "when"s, and "except"s, I have a feeling that the current way to say

   If .... ends with a slash, then ... only directories... The trailing
   slash is removed for the purpose of the remaining rules.

is still the best way to go forward. I do understand that this is a rather technical way to explain things than a colloquial one, but it also does remove a lot of conditionals and, therefore, mental burden.

-- Hannes
Dr. Adam Nielsen· May 18, 2019, 13:20 UTC · re: Johannes Sixt · lore

Re: [PATCH] make slash-rules more readable

On 18.05.19 08:42, Johannes Sixt wrote:
Show 67 quoted lines
> Am 17.05.19 um 23:43 schrieb Dr. Adam Nielsen:
>>> Another thing that I noticed is that its not mentioned anywhere that
>>> the pattern use a slash as a directory separator (instead of a
>>> backslash), its only clear from the examples. Maybe its worth to
>>> mention it in the "PATTERN FORMAT" section. Also its maybe worth to
>>> introduce the term "leading slash" and "trailing slash" because they
>>> will be of importance of the following paragraphs. Something like this
>>> after the paragraph of "!":
>>>
>>>       [...] for example, "\!important!.txt".
>>>
>>>       A slash `/` is used as a directory separator.
>>>       A leading slash (that is if the pattern begins with a slash)
>>>       or a trailing slash (that is if the pattern ends with a slash)
>>>       have special meaning and are explained below.
>>>
>>>       If the pattern contains a trailing slash, it would only find
>>>       a match with a directory. [...]
>>>
>>
>>
>> I changed my mind about this last addition. I think it is not very
>> readable and there is no need to explain leading/trailing slash. Maybe
>> one could just note it like this:
>>
>>        [...] for example, "\!important!.txt".
>>
>>        A slash `/` is used as a directory separator.
>>        A leading and trailing slash have special meaning
>>        and are explained in the following.
>>
>>        If the pattern ends with a slash, it would only find
>>        a match with a directory. [...]
>>
>> then I would also add:
>>
>>       If the pattern does not end with a slash, it would find a match
>>       with a file or directory.
>>
>>
>> Two notes about two sentences that I proposed a while ago:
>>
>>> + - If the pattern contains no slash "`/`" (except an optional
>> trailing slash),
>>> +   the ...
>>
>> I think that this sentence is not very readable. The exceptional case in
>> the brackets makes it over complicated.
>>
>>> + - A pattern that contains a non-trailing slash is matched
>>
>> And I don't like this phrase either. I think its too easy to confuse it
>> with "A pattern that contains no trailing slash".
>>
>> So I would suggest to replace both with the following:
>>
>>      If the pattern contains no slash or only a trailing slash, [...].
>>      Otherwise (when it contains a non-trailing slash) the pattern
>>      is matched [...].
> 
> With all those new "if"s, "but"s, "otherwise"s, "when"s, and "except"s,
> I have a feeling that the current way to say
> 
>     If .... ends with a slash, then ... only directories... The trailing
>     slash is removed for the purpose of the remaining rules.
> 
> is still the best way to go forward >
 > I do understand that this is a
 > rather technical way to explain things than a colloquial one, but it
 > also does remove a lot of conditionals and, therefore, mental burden.
 >
 > -- Hannes
 >

If one compares the current version with the new proposed one (including the updates from my last mail) word by word, then one finds that there is no additional "when", "except" and "but" and that the number of "if's" and "otherwise" has remained the same. So the other alternative does not "remove a lot of conditionals".

 >     [...] The trailing
 >     slash is removed for the purpose of the remaining rules.

has many downsides that I have explained in detail in the mail from 09.04.2019. The biggest issue is that the paragraphs do not stand for themselves alone anymore.

The only thing that would really change regarding the trailing slash is that we would say

     If the pattern contains no slash or only a trailing slash, [...}
instead of
 > - - If the pattern does not contain a slash '/',  [...]

I will send the current version with its latest changes to make it more clear how readable the latest version is.

Best regards Adam

← back to recent threads