From: Junio C Hamano Date: Mon, 25 Aug 2008 08:05:31 GMT Subject: Re: [PATCH] git-apply - Add --include=PATH Message-ID: <7vhc99h644.fsf@gitster.siamese.dyndns.org> In-Reply-To: <1219615063.18365.141.camel@localhost> Joe Perches writes: >> > @@ -2996,10 +2996,16 @@ static struct excludes { >> > const char *path; >> > } *excludes; >> > >> > +static struct includes { >> > + struct includes *next; >> > + const char *path; >> > +} *includes; >> >> Now this is ugly. You can just add a new variable "*includes" that is of >> exactly the same type as existing "*excludes" without introducing a new >> type. > > Yes, it's slightly ugly, but it was less work and much easier for > a human to parse. Another consideration is what should happen when you give contradicting excludes and includes list. For example, it is very plausible you might want to say "apply to all but header files, except that you want the part to one specific header file to also get applied). Something like: $ git apply --include='specific-one.h' --exclude='*.h' --include='*' util = exclude ? NULL : (void *) 1; +} static int use_patch(struct patch *p) { const char *pathname = p->new_name ? p->new_name : p->old_name; - struct excludes *x = excludes; - while (x) { - if (fnmatch(x->path, pathname, 0) == 0) - return 0; - x = x->next; - } + int i; + + /* Paths outside are not touched regardless of "--include" */ if (0 < prefix_length) { int pathlen = strlen(pathname); if (pathlen <= prefix_length || memcmp(prefix, pathname, prefix_length)) return 0; } - return 1; + + /* See if it matches any of exclude/include rule */ + for (i = 0; i < limit_by_name.nr; i++) { + struct string_list_item *it = &limit_by_name.items[i]; + if (!fnmatch(it->string, pathname, 0)) + return (it->util != NULL); + } + + /* + * If we had any include, a path that does not match any rule is + * not used. Otherwise, we saw bunch of exclude rules (or none) + * and such a path is used. + */ + return !has_include; } + static void prefix_one(char **name) { char *old_name = *name; @@ -3154,10 +3170,12 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) continue; } if (!prefixcmp(arg, "--exclude=")) { - struct excludes *x = xmalloc(sizeof(*x)); - x->path = arg + 10; - x->next = excludes; - excludes = x; + add_name_limit(arg + 10, 1); + continue; + } + if (!prefixcmp(arg, "--include=")) { + add_name_limit(arg + 10, 0); + has_include = 1; continue; } if (!prefixcmp(arg, "-p")) {