2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
12 struct path_simplify {
17 static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
18 int check_only, const struct path_simplify *simplify);
19 static int get_dtype(struct dirent *de, const char *path, int len);
21 /* helper string functions with support for the ignore_case flag */
22 int strcmp_icase(const char *a, const char *b)
24 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
27 int strncmp_icase(const char *a, const char *b, size_t count)
29 return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
32 int fnmatch_icase(const char *pattern, const char *string, int flags)
34 return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
37 static int common_prefix(const char **pathspec)
39 const char *path, *slash, *next;
46 slash = strrchr(path, '/');
51 * The first 'prefix' characters of 'path' are common leading
52 * path components among the pathspecs we have seen so far,
53 * including the trailing slash.
55 prefix = slash - path + 1;
56 while ((next = *++pathspec) != NULL) {
57 int len, last_matching_slash = -1;
58 for (len = 0; len < prefix && next[len] == path[len]; len++)
60 last_matching_slash = len;
63 if (last_matching_slash < 0)
65 prefix = last_matching_slash + 1;
70 int fill_directory(struct dir_struct *dir, const char **pathspec)
76 * Calculate common prefix for the pathspec, and
77 * use that to optimize the directory walk
79 len = common_prefix(pathspec);
83 path = xmemdupz(*pathspec, len);
85 /* Read the directory and prune it */
86 read_directory(dir, path, len, pathspec);
90 int within_depth(const char *name, int namelen,
91 int depth, int max_depth)
93 const char *cp = name, *cpe = name + namelen;
99 if (depth > max_depth)
106 * Does 'match' match the given name?
107 * A match is found if
109 * (1) the 'match' string is leading directory of 'name', or
110 * (2) the 'match' string is a wildcard and matches 'name', or
111 * (3) the 'match' string is exactly the same as 'name'.
113 * and the return value tells which case it was.
115 * It returns 0 when there is no match.
117 static int match_one(const char *match, const char *name, int namelen)
121 /* If the match was just the prefix, we matched */
123 return MATCHED_RECURSIVELY;
127 unsigned char c1 = tolower(*match);
128 unsigned char c2 = tolower(*name);
129 if (c1 == '\0' || is_glob_special(c1))
139 unsigned char c1 = *match;
140 unsigned char c2 = *name;
141 if (c1 == '\0' || is_glob_special(c1))
153 * If we don't match the matchstring exactly,
154 * we need to match by fnmatch
156 matchlen = strlen(match);
157 if (strncmp_icase(match, name, matchlen))
158 return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
160 if (namelen == matchlen)
161 return MATCHED_EXACTLY;
162 if (match[matchlen-1] == '/' || name[matchlen] == '/')
163 return MATCHED_RECURSIVELY;
168 * Given a name and a list of pathspecs, see if the name matches
169 * any of the pathspecs. The caller is also interested in seeing
170 * all pathspec matches some names it calls this function with
171 * (otherwise the user could have mistyped the unmatched pathspec),
172 * and a mark is left in seen[] array for pathspec element that
173 * actually matched anything.
175 int match_pathspec(const char **pathspec, const char *name, int namelen,
176 int prefix, char *seen)
186 for (i = 0; pathspec[i] != NULL; i++) {
188 const char *match = pathspec[i] + prefix;
189 if (seen && seen[i] == MATCHED_EXACTLY)
191 how = match_one(match, name, namelen);
195 if (seen && seen[i] < how)
202 static int no_wildcard(const char *string)
204 return string[strcspn(string, "*?[{\\")] == '\0';
207 void add_exclude(const char *string, const char *base,
208 int baselen, struct exclude_list *which)
215 if (*string == '!') {
219 len = strlen(string);
220 if (len && string[len - 1] == '/') {
222 x = xmalloc(sizeof(*x) + len);
224 memcpy(s, string, len - 1);
228 flags = EXC_FLAG_MUSTBEDIR;
230 x = xmalloc(sizeof(*x));
233 x->to_exclude = to_exclude;
234 x->patternlen = strlen(string);
236 x->baselen = baselen;
238 if (!strchr(string, '/'))
239 x->flags |= EXC_FLAG_NODIR;
240 if (no_wildcard(string))
241 x->flags |= EXC_FLAG_NOWILDCARD;
242 if (*string == '*' && no_wildcard(string+1))
243 x->flags |= EXC_FLAG_ENDSWITH;
244 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
245 which->excludes[which->nr++] = x;
248 static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
252 enum object_type type;
254 struct index_state *istate = &the_index;
257 pos = index_name_pos(istate, path, len);
260 if (!ce_skip_worktree(istate->cache[pos]))
262 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
263 if (!data || type != OBJ_BLOB) {
271 void free_excludes(struct exclude_list *el)
275 for (i = 0; i < el->nr; i++)
276 free(el->excludes[i]);
283 int add_excludes_from_file_to_list(const char *fname,
287 struct exclude_list *which,
295 fd = open(fname, O_RDONLY);
296 if (fd < 0 || fstat(fd, &st) < 0) {
300 (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
306 if (buf[size-1] != '\n') {
307 buf = xrealloc(buf, size+1);
312 size = xsize_t(st.st_size);
317 buf = xmalloc(size+1);
318 if (read_in_full(fd, buf, size) != size) {
330 for (i = 0; i < size; i++) {
331 if (buf[i] == '\n') {
332 if (entry != buf + i && entry[0] != '#') {
333 buf[i - (i && buf[i-1] == '\r')] = 0;
334 add_exclude(entry, base, baselen, which);
342 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
344 if (add_excludes_from_file_to_list(fname, "", 0, NULL,
345 &dir->exclude_list[EXC_FILE], 0) < 0)
346 die("cannot use %s as an exclude file", fname);
349 static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
351 struct exclude_list *el;
352 struct exclude_stack *stk = NULL;
355 if ((!dir->exclude_per_dir) ||
356 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
357 return; /* too long a path -- ignore */
359 /* Pop the ones that are not the prefix of the path being checked. */
360 el = &dir->exclude_list[EXC_DIRS];
361 while ((stk = dir->exclude_stack) != NULL) {
362 if (stk->baselen <= baselen &&
363 !strncmp(dir->basebuf, base, stk->baselen))
365 dir->exclude_stack = stk->prev;
366 while (stk->exclude_ix < el->nr)
367 free(el->excludes[--el->nr]);
372 /* Read from the parent directories and push them down. */
373 current = stk ? stk->baselen : -1;
374 while (current < baselen) {
375 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
383 cp = strchr(base + current + 1, '/');
385 die("oops in prep_exclude");
388 stk->prev = dir->exclude_stack;
389 stk->baselen = cp - base;
390 stk->exclude_ix = el->nr;
391 memcpy(dir->basebuf + current, base + current,
392 stk->baselen - current);
393 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
394 add_excludes_from_file_to_list(dir->basebuf,
395 dir->basebuf, stk->baselen,
396 &stk->filebuf, el, 1);
397 dir->exclude_stack = stk;
398 current = stk->baselen;
400 dir->basebuf[baselen] = '\0';
403 /* Scan the list and let the last match determine the fate.
404 * Return 1 for exclude, 0 for include and -1 for undecided.
406 int excluded_from_list(const char *pathname,
407 int pathlen, const char *basename, int *dtype,
408 struct exclude_list *el)
413 for (i = el->nr - 1; 0 <= i; i--) {
414 struct exclude *x = el->excludes[i];
415 const char *exclude = x->pattern;
416 int to_exclude = x->to_exclude;
418 if (x->flags & EXC_FLAG_MUSTBEDIR) {
419 if (*dtype == DT_UNKNOWN)
420 *dtype = get_dtype(NULL, pathname, pathlen);
421 if (*dtype != DT_DIR)
425 if (x->flags & EXC_FLAG_NODIR) {
427 if (x->flags & EXC_FLAG_NOWILDCARD) {
428 if (!strcmp_icase(exclude, basename))
430 } else if (x->flags & EXC_FLAG_ENDSWITH) {
431 if (x->patternlen - 1 <= pathlen &&
432 !strcmp_icase(exclude + 1, pathname + pathlen - x->patternlen + 1))
435 if (fnmatch_icase(exclude, basename, 0) == 0)
440 /* match with FNM_PATHNAME:
441 * exclude has base (baselen long) implicitly
444 int baselen = x->baselen;
448 if (pathlen < baselen ||
449 (baselen && pathname[baselen-1] != '/') ||
450 strncmp_icase(pathname, x->base, baselen))
453 if (x->flags & EXC_FLAG_NOWILDCARD) {
454 if (!strcmp_icase(exclude, pathname + baselen))
457 if (fnmatch_icase(exclude, pathname+baselen,
464 return -1; /* undecided */
467 int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
469 int pathlen = strlen(pathname);
471 const char *basename = strrchr(pathname, '/');
472 basename = (basename) ? basename+1 : pathname;
474 prep_exclude(dir, pathname, basename-pathname);
475 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
476 switch (excluded_from_list(pathname, pathlen, basename,
477 dtype_p, &dir->exclude_list[st])) {
487 static struct dir_entry *dir_entry_new(const char *pathname, int len)
489 struct dir_entry *ent;
491 ent = xmalloc(sizeof(*ent) + len + 1);
493 memcpy(ent->name, pathname, len);
498 static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
500 if (cache_name_exists(pathname, len, ignore_case))
503 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
504 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
507 struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
509 if (!cache_name_is_other(pathname, len))
512 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
513 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
517 index_nonexistent = 0,
523 * Do not use the alphabetically stored index to look up
524 * the directory name; instead, use the case insensitive
527 static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
529 struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
530 unsigned char endchar;
533 return index_nonexistent;
534 endchar = ce->name[len];
537 * The cache_entry structure returned will contain this dirname
538 * and possibly additional path components.
541 return index_directory;
544 * If there are no additional path components, then this cache_entry
545 * represents a submodule. Submodules, despite being directories,
546 * are stored in the cache without a closing slash.
548 if (!endchar && S_ISGITLINK(ce->ce_mode))
551 /* This should never be hit, but it exists just in case. */
552 return index_nonexistent;
556 * The index sorts alphabetically by entry name, which
557 * means that a gitlink sorts as '\0' at the end, while
558 * a directory (which is defined not as an entry, but as
559 * the files it contains) will sort with the '/' at the
562 static enum exist_status directory_exists_in_index(const char *dirname, int len)
567 return directory_exists_in_index_icase(dirname, len);
569 pos = cache_name_pos(dirname, len);
572 while (pos < active_nr) {
573 struct cache_entry *ce = active_cache[pos++];
574 unsigned char endchar;
576 if (strncmp(ce->name, dirname, len))
578 endchar = ce->name[len];
582 return index_directory;
583 if (!endchar && S_ISGITLINK(ce->ce_mode))
586 return index_nonexistent;
590 * When we find a directory when traversing the filesystem, we
591 * have three distinct cases:
594 * - see it as a directory
597 * and which one we choose depends on a combination of existing
598 * git index contents and the flags passed into the directory
601 * Case 1: If we *already* have entries in the index under that
602 * directory name, we always recurse into the directory to see
605 * Case 2: If we *already* have that directory name as a gitlink,
606 * we always continue to see it as a gitlink, regardless of whether
607 * there is an actual git directory there or not (it might not
608 * be checked out as a subproject!)
610 * Case 3: if we didn't have it in the index previously, we
611 * have a few sub-cases:
613 * (a) if "show_other_directories" is true, we show it as
614 * just a directory, unless "hide_empty_directories" is
615 * also true and the directory is empty, in which case
616 * we just ignore it entirely.
617 * (b) if it looks like a git directory, and we don't have
618 * 'no_gitlinks' set we treat it as a gitlink, and show it
620 * (c) otherwise, we recurse into it.
622 enum directory_treatment {
625 recurse_into_directory
628 static enum directory_treatment treat_directory(struct dir_struct *dir,
629 const char *dirname, int len,
630 const struct path_simplify *simplify)
632 /* The "len-1" is to strip the final '/' */
633 switch (directory_exists_in_index(dirname, len-1)) {
634 case index_directory:
635 return recurse_into_directory;
638 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
639 return ignore_directory;
640 return show_directory;
642 case index_nonexistent:
643 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
645 if (!(dir->flags & DIR_NO_GITLINKS)) {
646 unsigned char sha1[20];
647 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
648 return show_directory;
650 return recurse_into_directory;
653 /* This is the "show_other_directories" case */
654 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
655 return show_directory;
656 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
657 return ignore_directory;
658 return show_directory;
662 * This is an inexact early pruning of any recursive directory
663 * reading - if the path cannot possibly be in the pathspec,
664 * return true, and we'll skip it early.
666 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
670 const char *match = simplify->path;
671 int len = simplify->len;
677 if (!memcmp(path, match, len))
687 * This function tells us whether an excluded path matches a
688 * list of "interesting" pathspecs. That is, whether a path matched
689 * by any of the pathspecs could possibly be ignored by excluding
690 * the specified path. This can happen if:
692 * 1. the path is mentioned explicitly in the pathspec
694 * 2. the path is a directory prefix of some element in the
697 static int exclude_matches_pathspec(const char *path, int len,
698 const struct path_simplify *simplify)
701 for (; simplify->path; simplify++) {
702 if (len == simplify->len
703 && !memcmp(path, simplify->path, len))
705 if (len < simplify->len
706 && simplify->path[len] == '/'
707 && !memcmp(path, simplify->path, len))
714 static int get_index_dtype(const char *path, int len)
717 struct cache_entry *ce;
719 ce = cache_name_exists(path, len, 0);
721 if (!ce_uptodate(ce))
723 if (S_ISGITLINK(ce->ce_mode))
726 * Nobody actually cares about the
727 * difference between DT_LNK and DT_REG
732 /* Try to look it up as a directory */
733 pos = cache_name_pos(path, len);
737 while (pos < active_nr) {
738 ce = active_cache[pos++];
739 if (strncmp(ce->name, path, len))
741 if (ce->name[len] > '/')
743 if (ce->name[len] < '/')
745 if (!ce_uptodate(ce))
746 break; /* continue? */
752 static int get_dtype(struct dirent *de, const char *path, int len)
754 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
757 if (dtype != DT_UNKNOWN)
759 dtype = get_index_dtype(path, len);
760 if (dtype != DT_UNKNOWN)
762 if (lstat(path, &st))
764 if (S_ISREG(st.st_mode))
766 if (S_ISDIR(st.st_mode))
768 if (S_ISLNK(st.st_mode))
773 enum path_treatment {
779 static enum path_treatment treat_one_path(struct dir_struct *dir,
780 char *path, int *len,
781 const struct path_simplify *simplify,
782 int dtype, struct dirent *de)
784 int exclude = excluded(dir, path, &dtype);
785 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
786 && exclude_matches_pathspec(path, *len, simplify))
787 dir_add_ignored(dir, path, *len);
790 * Excluded? If we don't explicitly want to show
791 * ignored files, ignore it
793 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
796 if (dtype == DT_UNKNOWN)
797 dtype = get_dtype(de, path, *len);
800 * Do we want to see just the ignored files?
801 * We still need to recurse into directories,
802 * even if we don't ignore them, since the
803 * directory may contain files that we do..
805 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
814 memcpy(path + *len, "/", 2);
816 switch (treat_directory(dir, path, *len, simplify)) {
818 if (exclude != !!(dir->flags
822 case recurse_into_directory:
824 case ignore_directory:
835 static enum path_treatment treat_path(struct dir_struct *dir,
837 char *path, int path_max,
839 const struct path_simplify *simplify,
844 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
846 *len = strlen(de->d_name);
847 /* Ignore overly long pathnames! */
848 if (*len + baselen + 8 > path_max)
850 memcpy(path + baselen, de->d_name, *len + 1);
852 if (simplify_away(path, *len, simplify))
856 return treat_one_path(dir, path, len, simplify, dtype, de);
860 * Read a directory tree. We currently ignore anything but
861 * directories, regular files and symlinks. That's because git
862 * doesn't handle them at all yet. Maybe that will change some
865 * Also, we ignore the name ".git" (even if it is not a directory).
866 * That likely will not change.
868 static int read_directory_recursive(struct dir_struct *dir,
869 const char *base, int baselen,
871 const struct path_simplify *simplify)
873 DIR *fdir = opendir(*base ? base : ".");
878 char path[PATH_MAX + 1];
879 memcpy(path, base, baselen);
881 while ((de = readdir(fdir)) != NULL) {
883 switch (treat_path(dir, de, path, sizeof(path),
884 baselen, simplify, &len)) {
886 contents += read_directory_recursive
887 (dir, path, len, 0, simplify);
898 dir_add_name(dir, path, len);
907 static int cmp_name(const void *p1, const void *p2)
909 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
910 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
912 return cache_name_compare(e1->name, e1->len,
917 * Return the length of the "simple" part of a path match limiter.
919 static int simple_length(const char *match)
924 unsigned char c = *match++;
926 if (c == '\0' || is_glob_special(c))
931 static struct path_simplify *create_simplify(const char **pathspec)
934 struct path_simplify *simplify = NULL;
939 for (nr = 0 ; ; nr++) {
942 alloc = alloc_nr(alloc);
943 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
948 simplify[nr].path = match;
949 simplify[nr].len = simple_length(match);
951 simplify[nr].path = NULL;
952 simplify[nr].len = 0;
956 static void free_simplify(struct path_simplify *simplify)
961 static int treat_leading_path(struct dir_struct *dir,
962 const char *path, int len,
963 const struct path_simplify *simplify)
965 char pathbuf[PATH_MAX];
969 while (len && path[len - 1] == '/')
975 cp = path + baselen + !!baselen;
976 cp = memchr(cp, '/', path + len - cp);
981 memcpy(pathbuf, path, baselen);
982 pathbuf[baselen] = '\0';
983 if (!is_directory(pathbuf))
985 if (simplify_away(pathbuf, baselen, simplify))
988 if (treat_one_path(dir, pathbuf, &blen, simplify,
989 DT_DIR, NULL) == path_ignored)
990 return 0; /* do not recurse into it */
992 return 1; /* finished checking */
996 int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
998 struct path_simplify *simplify;
1000 if (has_symlink_leading_path(path, len))
1003 simplify = create_simplify(pathspec);
1004 if (!len || treat_leading_path(dir, path, len, simplify))
1005 read_directory_recursive(dir, path, len, 0, simplify);
1006 free_simplify(simplify);
1007 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1008 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1012 int file_exists(const char *f)
1015 return lstat(f, &sb) == 0;
1019 * get_relative_cwd() gets the prefix of the current working directory
1020 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
1022 * As a convenience, it also returns NULL if 'dir' is already NULL. The
1023 * reason for this behaviour is that it is natural for functions returning
1024 * directory names to return NULL to say "this directory does not exist"
1025 * or "this directory is invalid". These cases are usually handled the
1026 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1027 * returns NULL for both of them.
1029 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1030 * unifies the handling of "outside work tree" with "no work tree at all".
1032 char *get_relative_cwd(char *buffer, int size, const char *dir)
1038 if (!getcwd(buffer, size))
1039 die_errno("can't find the current directory");
1041 if (!is_absolute_path(dir))
1042 dir = make_absolute_path(dir);
1044 while (*dir && *dir == *cwd) {
1057 * dir can end with a path separator when it's root
1058 * directory. Return proper prefix in that case.
1066 int is_inside_dir(const char *dir)
1068 char buffer[PATH_MAX];
1069 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1072 int is_empty_dir(const char *path)
1074 DIR *dir = opendir(path);
1081 while ((e = readdir(dir)) != NULL)
1082 if (!is_dot_or_dotdot(e->d_name)) {
1091 int remove_dir_recursively(struct strbuf *path, int flag)
1095 int ret = 0, original_len = path->len, len;
1096 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1097 unsigned char submodule_head[20];
1099 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1100 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
1101 /* Do not descend and nuke a nested git work tree. */
1104 dir = opendir(path->buf);
1107 if (path->buf[original_len - 1] != '/')
1108 strbuf_addch(path, '/');
1111 while ((e = readdir(dir)) != NULL) {
1113 if (is_dot_or_dotdot(e->d_name))
1116 strbuf_setlen(path, len);
1117 strbuf_addstr(path, e->d_name);
1118 if (lstat(path->buf, &st))
1120 else if (S_ISDIR(st.st_mode)) {
1121 if (!remove_dir_recursively(path, only_empty))
1122 continue; /* happy */
1123 } else if (!only_empty && !unlink(path->buf))
1124 continue; /* happy, too */
1126 /* path too long, stat fails, or non-directory still exists */
1132 strbuf_setlen(path, original_len);
1134 ret = rmdir(path->buf);
1138 void setup_standard_excludes(struct dir_struct *dir)
1142 dir->exclude_per_dir = ".gitignore";
1143 path = git_path("info/exclude");
1144 if (!access(path, R_OK))
1145 add_excludes_from_file(dir, path);
1146 if (excludes_file && !access(excludes_file, R_OK))
1147 add_excludes_from_file(dir, excludes_file);
1150 int remove_path(const char *name)
1154 if (unlink(name) && errno != ENOENT)
1157 slash = strrchr(name, '/');
1159 char *dirs = xstrdup(name);
1160 slash = dirs + (slash - name);
1163 } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1169 static int pathspec_item_cmp(const void *a_, const void *b_)
1171 struct pathspec_item *a, *b;
1173 a = (struct pathspec_item *)a_;
1174 b = (struct pathspec_item *)b_;
1175 return strcmp(a->match, b->match);
1178 int init_pathspec(struct pathspec *pathspec, const char **paths)
1180 const char **p = paths;
1183 memset(pathspec, 0, sizeof(*pathspec));
1188 pathspec->raw = paths;
1189 pathspec->nr = p - paths;
1193 pathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);
1194 for (i = 0; i < pathspec->nr; i++) {
1195 struct pathspec_item *item = pathspec->items+i;
1196 const char *path = paths[i];
1199 item->len = strlen(path);
1200 item->has_wildcard = !no_wildcard(path);
1201 if (item->has_wildcard)
1202 pathspec->has_wildcard = 1;
1205 qsort(pathspec->items, pathspec->nr,
1206 sizeof(struct pathspec_item), pathspec_item_cmp);
1211 void free_pathspec(struct pathspec *pathspec)
1213 free(pathspec->items);
1214 pathspec->items = NULL;