apply: hoist use_patch() helper for path exclusion up

We will be adding a caller to the function a bit earlier in this file in a later patch. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 6, 2014 at 13:11 UTC 3ee2ad14c6a6823d16eeda2aa48496d4b6e16f70
1 file changed +43 -38
builtin/apply.c
+43 -38
@@ -1937,6 +1937,49 @@ static void prefix_patch(struct patch *p)
1937 prefix_one(&p->old_name);
1938 }
1939
1940 +/*
1941 + * include/exclude
1942 + */
1943 +
1944 +static struct string_list limit_by_name;
1945 +static int has_include;
1946 +static void add_name_limit(const char *name, int exclude)
1947 +{
1948 + struct string_list_item *it;
1949 +
1950 + it = string_list_append(&limit_by_name, name);
1951 + it->util = exclude ? NULL : (void *) 1;
1952 +}
1953 +
1954 +static int use_patch(struct patch *p)
1955 +{
1956 + const char *pathname = p->new_name ? p->new_name : p->old_name;
1957 + int i;
1958 +
1959 + /* Paths outside are not touched regardless of "--include" */
1960 + if (0 < prefix_length) {
1961 + int pathlen = strlen(pathname);
1962 + if (pathlen <= prefix_length ||
1963 + memcmp(prefix, pathname, prefix_length))
1964 + return 0;
1965 + }
1966 +
1967 + /* See if it matches any of exclude/include rule */
1968 + for (i = 0; i < limit_by_name.nr; i++) {
1969 + struct string_list_item *it = &limit_by_name.items[i];
1970 + if (!fnmatch(it->string, pathname, 0))
1971 + return (it->util != NULL);
1972 + }
1973 +
1974 + /*
1975 + * If we had any include, a path that does not match any rule is
1976 + * not used. Otherwise, we saw bunch of exclude rules (or none)
1977 + * and such a path is used.
1978 + */
1979 + return !has_include;
1980 +}
1981 +
1982 +
1983 /*
1984 * Read the patch text in "buffer" that extends for "size" bytes; stop
1985 * reading after seeing a single patch (i.e. changes to a single file).
@@ -4145,44 +4188,6 @@ static int write_out_results(struct patch *list)
4188
4189 static struct lock_file lock_file;
4190
4148 -static struct string_list limit_by_name;
4149 -static int has_include;
4150 -static void add_name_limit(const char *name, int exclude)
4151 -{
4152 - struct string_list_item *it;
4153 -
4154 - it = string_list_append(&limit_by_name, name);
4155 - it->util = exclude ? NULL : (void *) 1;
4156 -}
4157 -
4158 -static int use_patch(struct patch *p)
4159 -{
4160 - const char *pathname = p->new_name ? p->new_name : p->old_name;
4161 - int i;
4162 -
4163 - /* Paths outside are not touched regardless of "--include" */
4164 - if (0 < prefix_length) {
4165 - int pathlen = strlen(pathname);
4166 - if (pathlen <= prefix_length ||
4167 - memcmp(prefix, pathname, prefix_length))
4168 - return 0;
4169 - }
4170 -
4171 - /* See if it matches any of exclude/include rule */
4172 - for (i = 0; i < limit_by_name.nr; i++) {
4173 - struct string_list_item *it = &limit_by_name.items[i];
4174 - if (!fnmatch(it->string, pathname, 0))
4175 - return (it->util != NULL);
4176 - }
4177 -
4178 - /*
4179 - * If we had any include, a path that does not match any rule is
4180 - * not used. Otherwise, we saw bunch of exclude rules (or none)
4181 - * and such a path is used.
4182 - */
4183 - return !has_include;
4184 -}
4185 -
4191 #define INACCURATE_EOF (1<<0)
4192 #define RECOUNT (1<<1)
4193