Merge branch 'jc/apply-include-exclude'

* jc/apply-include-exclude:
  git-apply:--include=pathspec
This commit is contained in:
Shawn O. Pearce
2008-09-25 09:09:41 -07:00
2 changed files with 46 additions and 16 deletions

View File

@ -14,7 +14,8 @@ SYNOPSIS
[--allow-binary-replacement | --binary] [--reject] [-z] [--allow-binary-replacement | --binary] [--reject] [-z]
[-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached] [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
[--whitespace=<nowarn|warn|fix|error|error-all>] [--whitespace=<nowarn|warn|fix|error|error-all>]
[--exclude=PATH] [--directory=<root>] [--verbose] [<patch>...] [--exclude=PATH] [--include=PATH] [--directory=<root>]
[--verbose] [<patch>...]
DESCRIPTION DESCRIPTION
----------- -----------
@ -137,6 +138,17 @@ discouraged.
be useful when importing patchsets, where you want to exclude certain be useful when importing patchsets, where you want to exclude certain
files or directories. files or directories.
--include=<path-pattern>::
Apply changes to files matching the given path pattern. This can
be useful when importing patchsets, where you want to include certain
files or directories.
+
When --exclude and --include patterns are used, they are examined in the
order they appear on the command line, and the first match determines if a
patch to each path is used. A patch to a path that does not match any
include/exclude pattern is used by default if there is no include pattern
on the command line, and ignored if there is any include pattern.
--whitespace=<action>:: --whitespace=<action>::
When applying a patch, detect a new or modified line that has When applying a patch, detect a new or modified line that has
whitespace errors. What are considered whitespace errors is whitespace errors. What are considered whitespace errors is

View File

@ -2994,29 +2994,45 @@ static int write_out_results(struct patch *list, int skipped_patch)
static struct lock_file lock_file; static struct lock_file lock_file;
static struct excludes { static struct string_list limit_by_name;
struct excludes *next; static int has_include;
const char *path; static void add_name_limit(const char *name, int exclude)
} *excludes; {
struct string_list_item *it;
it = string_list_append(name, &limit_by_name);
it->util = exclude ? NULL : (void *) 1;
}
static int use_patch(struct patch *p) static int use_patch(struct patch *p)
{ {
const char *pathname = p->new_name ? p->new_name : p->old_name; const char *pathname = p->new_name ? p->new_name : p->old_name;
struct excludes *x = excludes; int i;
while (x) {
if (fnmatch(x->path, pathname, 0) == 0) /* Paths outside are not touched regardless of "--include" */
return 0;
x = x->next;
}
if (0 < prefix_length) { if (0 < prefix_length) {
int pathlen = strlen(pathname); int pathlen = strlen(pathname);
if (pathlen <= prefix_length || if (pathlen <= prefix_length ||
memcmp(prefix, pathname, prefix_length)) memcmp(prefix, pathname, prefix_length))
return 0; 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) static void prefix_one(char **name)
{ {
char *old_name = *name; char *old_name = *name;
@ -3157,10 +3173,12 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
continue; continue;
} }
if (!prefixcmp(arg, "--exclude=")) { if (!prefixcmp(arg, "--exclude=")) {
struct excludes *x = xmalloc(sizeof(*x)); add_name_limit(arg + 10, 1);
x->path = arg + 10; continue;
x->next = excludes; }
excludes = x; if (!prefixcmp(arg, "--include=")) {
add_name_limit(arg + 10, 0);
has_include = 1;
continue; continue;
} }
if (!prefixcmp(arg, "-p")) { if (!prefixcmp(arg, "-p")) {