Skip to content

gitignore: ignore comments on the same line as a pattern #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Documentation/gitignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ PATTERN FORMAT
for readability.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Chris Zehner via GitGitGadget" <[email protected]> writes:

> -   that begin with a hash.
> +   Put a backslash ("`\`") in front of each hash for patterns
> +   containing a hash.
> +
> + - A # after a pattern will be treated as the start of a comment.
> +   Put a backslash ("`\`") in front of each hash for patterns
> +   containing a hash.

Besides being backward incompatible, this looks somewhat misdesigned
by lacking a way to escape a backslash (i.e. what should a project
do if they want a patttern with backslash followed by a hash
literally in there?).

> diff --git a/dir.c b/dir.c
> index cab9c2a458..aeefe142bc 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -658,6 +658,38 @@ void clear_pattern_list(struct pattern_list *pl)
>  	memset(pl, 0, sizeof(*pl));
>  }
>  
> +static void trim_trailing_comments(char *buf)
> +{
> +	char *p, *last_hash = NULL;
> +	int escape_seq = 0;
> +
> +	for (p = buf; *p; p++)
> +	{

Style (see Documentation/CodingGuidelines).  The opening parenthesis
of a control structure like if/for/switch are placed on the same
line, i.e.

	for (p = buf; *p; p++) {

Do we even need a separate 'p', instead of scanning with 'buf' itself?

> +		if (!*p)
> +			return;

What happens when an entry ends with '\' followed by an EOL?  IOW,
what if escape_seq is true here?

> +		switch (*p) {
> +		case '#':
> +			if (escape_seq)
> +			{
> +				escape_seq = 0;
> +				p++;
> +				break;
> +			}
> +			if (!last_hash)
> +				last_hash = p;
> +			break;
> +		case '\\':
> +			escape_seq = 1;
> +			break;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbzehner did you see this reply?


- A line starting with # serves as a comment.
Put a backslash ("`\`") in front of the first hash for patterns
that begin with a hash.
Put a backslash ("`\`") in front of each hash for patterns
containing a hash.

- A # after a pattern will be treated as the start of a comment.
Put a backslash ("`\`") in front of each hash for patterns
containing a hash.

- Trailing spaces are ignored unless they are quoted with backslash
("`\`").
Expand Down
33 changes: 33 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,38 @@ void clear_pattern_list(struct pattern_list *pl)
memset(pl, 0, sizeof(*pl));
}

static void trim_trailing_comments(char *buf)
{
char *p, *last_hash = NULL;
int escape_seq = 0;

for (p = buf; *p; p++)
{
if (!*p)
return;
switch (*p) {
case '#':
if (escape_seq)
{
escape_seq = 0;
p++;
break;
}
if (!last_hash)
last_hash = p;
break;
case '\\':
escape_seq = 1;
break;
default:
escape_seq = 0;
}
}

if (last_hash)
*last_hash = '\0';
}

static void trim_trailing_spaces(char *buf)
{
char *p, *last_space = NULL;
Expand Down Expand Up @@ -859,6 +891,7 @@ static int add_patterns_from_buffer(char *buf, size_t size,
if (buf[i] == '\n') {
if (entry != buf + i && entry[0] != '#') {
buf[i - (i && buf[i-1] == '\r')] = 0;
trim_trailing_comments(entry);
trim_trailing_spaces(entry);
add_pattern(entry, base, baselen, pl, lineno);
}
Expand Down