733
*/
734
if (!patternlen && !namelen)
735
return 1;
736
+ /*
737
+ * This can happen when we ignore some exclude rules
738
+ * on directories in other to see if negative rules
739
+ * may match. E.g.
740
+ *
741
+ * /abc
742
+ * !/abc/def/ghi
743
+ *
744
+ * The pattern of interest is "/abc". On the first
745
+ * try, we should match path "abc" with this pattern
746
+ * in the "if" statement right above, but the caller
747
+ * ignores it.
748
+ *
749
+ * On the second try with paths within "abc",
750
+ * e.g. "abc/xyz", we come here and try to match it
751
+ * with "/abc".
752
+ */
753
+ if (!patternlen && namelen && *name == '/')
754
+ return 1;
755
}
756
757
return fnmatch_icase_mem(pattern, patternlen,
759
WM_PATHNAME) == 0;
760
}
761
762
+/*
763
+ * Return non-zero if pathname is a directory and an ancestor of the
764
+ * literal path in a (negative) pattern. This is used to keep
765
+ * descending in "foo" and "foo/bar" when the pattern is
766
+ * "!foo/bar/.gitignore". "foo/notbar" will not be descended however.
767
+ */
768
+static int match_neg_path(const char *pathname, int pathlen, int *dtype,
769
+ const char *base, int baselen,
770
+ const char *pattern, int prefix, int patternlen,
771
+ int flags)
772
+{
773
+ assert((flags & EXC_FLAG_NEGATIVE) && !(flags & EXC_FLAG_NODIR));
774
+
775
+ if (*dtype == DT_UNKNOWN)
776
+ *dtype = get_dtype(NULL, pathname, pathlen);
777
+ if (*dtype != DT_DIR)
778
+ return 0;
779
+
780
+ if (*pattern == '/') {
781
+ pattern++;
782
+ patternlen--;
783
+ prefix--;
784
+ }
785
+
786
+ if (baselen) {
787
+ if (((pathlen < baselen && base[pathlen] == '/') ||
788
+ pathlen == baselen) &&
789
+ !strncmp_icase(pathname, base, pathlen))
790
+ return 1;
791
+ pathname += baselen + 1;
792
+ pathlen -= baselen + 1;
793
+ }
794
+
795
+
796
+ if (prefix &&
797
+ ((pathlen < prefix && pattern[pathlen] == '/') &&
798
+ !strncmp_icase(pathname, pattern, pathlen)))
799
+ return 1;
800
+
801
+ return 0;
802
+}
803
+
804
/*
805
* Scan the given exclude list in reverse to see whether pathname
806
* should be ignored. The first match (i.e. the last on the list), if
814
struct exclude_list *el)
815
{
816
struct exclude *exc = NULL; /* undecided */
756
- int i;
817
+ int i, matched_negative_path = 0;
818
819
if (!el->nr)
820
return NULL; /* undefined */
849
exc = x;
850
break;
851
}
852
+
853
+ if ((x->flags & EXC_FLAG_NEGATIVE) && !matched_negative_path &&
854
+ match_neg_path(pathname, pathlen, dtype, x->base,
855
+ x->baselen ? x->baselen - 1 : 0,
856
+ exclude, prefix, x->patternlen, x->flags))
857
+ matched_negative_path = 1;
858
}
859
+ if (exc &&
860
+ !(exc->flags & EXC_FLAG_NEGATIVE) &&
861
+ !(exc->flags & EXC_FLAG_NODIR) &&
862
+ matched_negative_path)
863
+ exc = NULL;
864
return exc;
865
}
866