@matheusbsbs / matheus-bagliano / commits / 6ecb1ee28a
Fork 0

git-apply:--include=pathspec

This allows --include=pathspec, similar to --exclude=pathspec. The rule when one or both of these are used is that the include/exclude patterns are examined in the order they are given on the command line, and the first match determines if a patch to each path is used or not. Hence: $ git apply --include='specific.h' --exclude='*.h' <diff would apply the patch to specific.h header file, but all other patches in the input file to other header files are ignored. 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. This originally came from Joe Perches, but both the design of the semantics and the implementation have been redone complately. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 25, 2008 at 01:05 UTC 6ecb1ee28a9479af1d39cfab170b5d93e238c277
2 files changed +46 -16
Documentation/git-apply.txt
+13 -1
@@ -14,7 +14,8 @@ SYNOPSIS
14 [--allow-binary-replacement | --binary] [--reject] [-z]
15 [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
16 [--whitespace=<nowarn|warn|fix|error|error-all>]
17 - [--exclude=PATH] [--directory=<root>] [--verbose] [<patch>...]
17 + [--exclude=PATH] [--include=PATH] [--directory=<root>]
18 + [--verbose] [<patch>...]
19
20 DESCRIPTION
21 -----------
@@ -137,6 +138,17 @@ discouraged.
138 be useful when importing patchsets, where you want to exclude certain
139 files or directories.
140
141 +--include=<path-pattern>::
142 + Apply changes to files matching the given path pattern. This can
143 + be useful when importing patchsets, where you want to include certain
144 + files or directories.
145 ++
146 +When --exclude and --include patterns are used, they are examined in the
147 +order they appear on the command line, and the first match determines if a
148 +patch to each path is used. A patch to a path that does not match any
149 +include/exclude pattern is used by default if there is no include pattern
150 +on the command line, and ignored if there is any include pattern.
151 +
152 --whitespace=<action>::
153 When applying a patch, detect a new or modified line that has
154 whitespace errors. What are considered whitespace errors is
builtin-apply.c
+33 -15
@@ -2994,29 +2994,45 @@ static int write_out_results(struct patch *list, int skipped_patch)
2994
2995 static struct lock_file lock_file;
2996
2997 -static struct excludes {
2998 - struct excludes *next;
2999 - const char *path;
3000 -} *excludes;
2997 +static struct string_list limit_by_name;
2998 +static int has_include;
2999 +static void add_name_limit(const char *name, int exclude)
3000 +{
3001 + struct string_list_item *it;
3002 +
3003 + it = string_list_append(name, &limit_by_name);
3004 + it->util = exclude ? NULL : (void *) 1;
3005 +}
3006
3007 static int use_patch(struct patch *p)
3008 {
3009 const char *pathname = p->new_name ? p->new_name : p->old_name;
3005 - struct excludes *x = excludes;
3006 - while (x) {
3007 - if (fnmatch(x->path, pathname, 0) == 0)
3008 - return 0;
3009 - x = x->next;
3010 - }
3010 + int i;
3011 +
3012 + /* Paths outside are not touched regardless of "--include" */
3013 if (0 < prefix_length) {
3014 int pathlen = strlen(pathname);
3015 if (pathlen <= prefix_length ||
3016 memcmp(prefix, pathname, prefix_length))
3017 return 0;
3018 }
3017 - return 1;
3019 +
3020 + /* See if it matches any of exclude/include rule */
3021 + for (i = 0; i < limit_by_name.nr; i++) {
3022 + struct string_list_item *it = &limit_by_name.items[i];
3023 + if (!fnmatch(it->string, pathname, 0))
3024 + return (it->util != NULL);
3025 + }
3026 +
3027 + /*
3028 + * If we had any include, a path that does not match any rule is
3029 + * not used. Otherwise, we saw bunch of exclude rules (or none)
3030 + * and such a path is used.
3031 + */
3032 + return !has_include;
3033 }
3034
3035 +
3036 static void prefix_one(char **name)
3037 {
3038 char *old_name = *name;
@@ -3157,10 +3173,12 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
3173 continue;
3174 }
3175 if (!prefixcmp(arg, "--exclude=")) {
3160 - struct excludes *x = xmalloc(sizeof(*x));
3161 - x->path = arg + 10;
3162 - x->next = excludes;
3163 - excludes = x;
3176 + add_name_limit(arg + 10, 1);
3177 + continue;
3178 + }
3179 + if (!prefixcmp(arg, "--include=")) {
3180 + add_name_limit(arg + 10, 0);
3181 + has_include = 1;
3182 continue;
3183 }
3184 if (!prefixcmp(arg, "-p")) {