git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Chris Zehner via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Chris Zehner <cbzehner@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Chris Zehner <cbzehner@gmail.com>
Subject: [PATCH 1/1] gitignore: ignore comments on the same line as a pattern
Date: Tue, 01 Oct 2019 20:13:13 -0700 (PDT)	[thread overview]
Message-ID: <e448214e87b46dc649781b10727043bc390d9aab.1569985991.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.370.git.gitgitgadget@gmail.com>

From: Chris Zehner <cbzehner@gmail.com>

Signed-off-by: Chris Zehner <cbzehner@gmail.com>
---
 Documentation/gitignore.txt |  8 ++++++--
 dir.c                       | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index d47b1ae296..1778cf1d38 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -74,8 +74,12 @@ PATTERN FORMAT
    for readability.
 
  - 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
    ("`\`").
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++)
+	{
+		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;
@@ -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);
 			}
-- 
gitgitgadget

  reply	other threads:[~2019-10-02  3:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-02  3:13 [PATCH 0/1] gitignore: ignore comments on the same line as a pattern Chris Zehner via GitGitGadget
2019-10-02  3:13 ` Chris Zehner via GitGitGadget [this message]
2019-10-02  7:02   ` [PATCH 1/1] " Junio C Hamano
2019-10-02  6:51 ` [PATCH 0/1] " Junio C Hamano

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=e448214e87b46dc649781b10727043bc390d9aab.1569985991.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=cbzehner@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).