git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Adam Spiers <git@adamspiers.org>
To: git list <git@vger.kernel.org>
Subject: [PATCH v3 12/19] dir.c: keep track of where patterns came from
Date: Thu, 27 Dec 2012 02:32:31 +0000	[thread overview]
Message-ID: <1356575558-2674-13-git-send-email-git@adamspiers.org> (raw)
In-Reply-To: <1356575558-2674-1-git-send-email-git@adamspiers.org>

For exclude patterns read in from files, the filename is stored in the
exclude list, and the originating line number is stored in the
individual exclude (counting starting at 1).

For exclude patterns provided on the command line, a string describing
the source of the patterns is stored in the exclude list, and the
sequence number assigned to each exclude pattern is negative, with
counting starting at -1.  So for example the 2nd pattern provided via
--exclude would be numbered -2.  This allows any future consumers of
that data to easily distinguish between exclude patterns from files
vs. from the CLI.

Signed-off-by: Adam Spiers <git@adamspiers.org>
---
 builtin/clean.c    |  4 ++--
 builtin/ls-files.c |  5 +++--
 dir.c              | 26 ++++++++++++++++++++------
 dir.h              | 21 +++++++++++++++++++--
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/builtin/clean.c b/builtin/clean.c
index bd18b88..72d2876 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -97,10 +97,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	if (!ignored)
 		setup_standard_excludes(&dir);
 
-	add_exclude_list(&dir, EXC_CMDL);
+	add_exclude_list(&dir, EXC_CMDL, "--exclude option");
 	for (i = 0; i < exclude_list.nr; i++)
 		add_exclude(exclude_list.items[i].string, "", 0,
-			    &dir.exclude_list_groups[EXC_CMDL].ary[0]);
+			    &dir.exclude_list_groups[EXC_CMDL].ary[0], -(i+1));
 
 	pathspec = get_pathspec(prefix, argv);
 
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index c448e06..d4e55c2 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -35,6 +35,7 @@ static int error_unmatch;
 static char *ps_matched;
 static const char *with_tree;
 static int exc_given;
+static int exclude_args;
 
 static const char *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -423,7 +424,7 @@ static int option_parse_exclude(const struct option *opt,
 	struct exclude_list_group *group = opt->value;
 
 	exc_given = 1;
-	add_exclude(arg, "", 0, &group->ary[0]);
+	add_exclude(arg, "", 0, &group->ary[0], --exclude_args);
 
 	return 0;
 }
@@ -524,7 +525,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	if (read_cache() < 0)
 		die("index file corrupt");
 
-	add_exclude_list(&dir, EXC_CMDL);
+	add_exclude_list(&dir, EXC_CMDL, "--exclude option");
 	argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
 			ls_files_usage, 0);
 	if (show_tag || show_valid_bit) {
diff --git a/dir.c b/dir.c
index 3a9d89b..aefe2bb 100644
--- a/dir.c
+++ b/dir.c
@@ -349,7 +349,7 @@ void parse_exclude_pattern(const char **pattern,
 }
 
 void add_exclude(const char *string, const char *base,
-		 int baselen, struct exclude_list *el)
+		 int baselen, struct exclude_list *el, int srcpos)
 {
 	struct exclude *x;
 	int patternlen;
@@ -373,8 +373,10 @@ void add_exclude(const char *string, const char *base,
 	x->base = base;
 	x->baselen = baselen;
 	x->flags = flags;
+	x->srcpos = srcpos;
 	ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
 	el->excludes[el->nr++] = x;
+	x->el = el;
 }
 
 static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
@@ -425,7 +427,7 @@ int add_excludes_from_file_to_list(const char *fname,
 				   int check_index)
 {
 	struct stat st;
-	int fd, i;
+	int fd, i, lineno = 1;
 	size_t size = 0;
 	char *buf, *entry;
 
@@ -467,15 +469,17 @@ int add_excludes_from_file_to_list(const char *fname,
 		if (buf[i] == '\n') {
 			if (entry != buf + i && entry[0] != '#') {
 				buf[i - (i && buf[i-1] == '\r')] = 0;
-				add_exclude(entry, base, baselen, el);
+				add_exclude(entry, base, baselen, el, lineno);
 			}
+			lineno++;
 			entry = buf + i + 1;
 		}
 	}
 	return 0;
 }
 
-struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
+struct exclude_list *add_exclude_list(struct dir_struct *dir,
+				      int group_type, const char *src)
 {
 	struct exclude_list *el;
 	struct exclude_list_group *group;
@@ -484,6 +488,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
 	ALLOC_GROW(group->ary, group->nr + 1, group->alloc);
 	el = &group->ary[group->nr++];
 	memset(el, 0, sizeof(*el));
+	el->src = src;
 	return el;
 }
 
@@ -493,7 +498,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type)
 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 {
 	struct exclude_list *el;
-	el = add_exclude_list(dir, EXC_FILE);
+	el = add_exclude_list(dir, EXC_FILE, fname);
 	if (add_excludes_from_file_to_list(fname, "", 0, el, 0) < 0)
 		die("cannot use %s as an exclude file", fname);
 }
@@ -522,6 +527,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 			break;
 		el = &group->ary[dir->exclude_stack->exclude_ix];
 		dir->exclude_stack = stk->prev;
+		free((char *)el->src); /* see strdup() below */
 		clear_exclude_list(el);
 		free(stk);
 		group->nr--;
@@ -548,7 +554,15 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 		memcpy(dir->basebuf + current, base + current,
 		       stk->baselen - current);
 		strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
-		el = add_exclude_list(dir, EXC_DIRS);
+		/*
+		 * dir->basebuf gets reused by the traversal, but we
+		 * need fname to remain unchanged to ensure the src
+		 * member of each struct exclude correctly
+		 * back-references its source file.  Other invocations
+		 * of add_exclude_list provide stable strings, so we
+		 * strdup() and free() here in the caller.
+		 */
+		el = add_exclude_list(dir, EXC_DIRS, strdup(dir->basebuf));
 		stk->exclude_ix = group->nr - 1;
 		add_excludes_from_file_to_list(dir->basebuf,
 					       dir->basebuf, stk->baselen,
diff --git a/dir.h b/dir.h
index 9408fbe..f91770a 100644
--- a/dir.h
+++ b/dir.h
@@ -25,16 +25,32 @@ struct dir_entry {
 struct exclude_list {
 	int nr;
 	int alloc;
+
 	/* remember pointer to exclude file contents so we can free() */
 	char *filebuf;
 
+	/* origin of list, e.g. path to filename, or descriptive string */
+	const char *src;
+
 	struct exclude {
+		/*
+		 * This allows callers of last_exclude_matching() etc.
+		 * to determine the origin of the matching pattern.
+		 */
+		struct exclude_list *el;
+
 		const char *pattern;
 		int patternlen;
 		int nowildcardlen;
 		const char *base;
 		int baselen;
 		int flags;
+
+		/*
+		 * Counting starts from 1 for line numbers in ignore files,
+		 * and from -1 decrementing for patterns from CLI args.
+		 */
+		int srcpos;
 	} **excludes;
 };
 
@@ -144,13 +160,14 @@ extern struct exclude *last_exclude_matching_path(struct path_exclude_check *, c
 extern int is_path_excluded(struct path_exclude_check *, const char *, int namelen, int *dtype);
 
 
-extern struct exclude_list *add_exclude_list(struct dir_struct *dir, int group_type);
+extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
+					     int group_type, const char *src);
 extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
 					  struct exclude_list *el, int check_index);
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
 extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
 extern void add_exclude(const char *string, const char *base,
-			int baselen, struct exclude_list *el);
+			int baselen, struct exclude_list *el, int srcpos);
 extern void clear_exclude_list(struct exclude_list *el);
 extern int file_exists(const char *);
 
-- 
1.7.11.2.249.g31c7954

  parent reply	other threads:[~2012-12-27  2:33 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-27  2:32 [PATCH v3 00/19] new git check-ignore sub-command Adam Spiers
2012-12-27  2:32 ` [PATCH v3 01/19] api-directory-listing.txt: update to match code Adam Spiers
2012-12-27  2:32 ` [PATCH v3 02/19] Improve documentation and comments regarding directory traversal API Adam Spiers
2013-01-01 20:52   ` Junio C Hamano
2013-01-02 12:54     ` Adam Spiers
2013-01-06 12:02       ` Adam Spiers
2012-12-27  2:32 ` [PATCH v3 03/19] dir.c: rename cryptic 'which' variable to more consistent name Adam Spiers
2012-12-27  2:32 ` [PATCH v3 04/19] dir.c: rename path_excluded() to is_path_excluded() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 05/19] dir.c: rename excluded_from_list() to is_excluded_from_list() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 06/19] dir.c: rename excluded() to is_excluded() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 07/19] dir.c: refactor is_excluded_from_list() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 08/19] dir.c: refactor is_excluded() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 09/19] dir.c: refactor is_path_excluded() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 10/19] dir.c: rename free_excludes() to clear_exclude_list() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 11/19] dir.c: use a single struct exclude_list per source of excludes Adam Spiers
2013-01-04 21:03   ` Junio C Hamano
2013-01-05  7:54     ` Junio C Hamano
2013-01-06 15:27       ` Adam Spiers
2013-01-06 15:35         ` [PATCH] api-allocation-growing.txt: encourage better variable naming Adam Spiers
2013-01-06 20:29           ` Junio C Hamano
2013-01-06 20:52             ` Adam Spiers
2013-01-06 20:58               ` Junio C Hamano
2013-01-06 15:20     ` [PATCH v3 11/19] dir.c: use a single struct exclude_list per source of excludes Adam Spiers
2013-01-06 20:25       ` Junio C Hamano
2013-01-06 22:53         ` Adam Spiers
2013-01-06 23:17           ` Adam Spiers
2013-01-06 23:19           ` Junio C Hamano
2012-12-27  2:32 ` Adam Spiers [this message]
2012-12-27  2:32 ` [PATCH v3 13/19] dir.c: provide clear_directory() for reclaiming dir_struct memory Adam Spiers
2012-12-27  2:32 ` [PATCH v3 14/19] add.c: refactor treat_gitlinks() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 15/19] add.c: remove unused argument from validate_pathspec() Adam Spiers
2012-12-27  2:32 ` [PATCH v3 16/19] pathspec.c: move reusable code from builtin/add.c Adam Spiers
2012-12-28 20:32   ` Junio C Hamano
2012-12-28 20:45     ` Adam Spiers
2012-12-29  0:40       ` Adam Spiers
2012-12-28 20:48   ` Junio C Hamano
2012-12-28 21:15     ` Adam Spiers
2012-12-27  2:32 ` [PATCH v3 17/19] pathspec.c: extract new validate_path() for reuse Adam Spiers
2012-12-28 20:44   ` Junio C Hamano
2012-12-28 21:08     ` Adam Spiers
2012-12-27  2:32 ` [PATCH v3 18/19] setup.c: document get_pathspec() Adam Spiers
2012-12-28 20:36   ` Junio C Hamano
2012-12-28 20:40     ` Adam Spiers
2012-12-29  0:52       ` Adam Spiers
2012-12-29  1:36         ` Junio C Hamano
2012-12-27  2:32 ` [PATCH v3 19/19] Add git-check-ignore sub-command Adam Spiers
2012-12-28 21:21   ` Junio C Hamano
2012-12-29  1:23     ` Adam Spiers
2012-12-29  3:32       ` Adam Spiers
2012-12-27  5:15 ` [PATCH v3 00/19] new git check-ignore sub-command Michael Leal
2012-12-28 18:50 ` Junio C Hamano
2012-12-28 19:39   ` Adam Spiers
2012-12-28 20:15     ` Antoine Pelisse
2012-12-28 21:31       ` Junio C Hamano
2012-12-28 21:23 ` 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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1356575558-2674-13-git-send-email-git@adamspiers.org \
    --to=git@adamspiers.org \
    --cc=git@vger.kernel.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).