git-am: Add am.keepcr and --no-keep-cr to override it
[gitweb.git] / dir.c
diff --git a/dir.c b/dir.c
index 3a8d3e67a529c03659dc950d64f4b0d6226f9925..133c333df61be37e7908f77367f63c85cfc9c548 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -242,6 +242,14 @@ int add_excludes_from_file_to_list(const char *fname,
                if (!check_index ||
                    (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
                        return -1;
+               if (size == 0) {
+                       free(buf);
+                       return 0;
+               }
+               if (buf[size-1] != '\n') {
+                       buf = xrealloc(buf, size+1);
+                       buf[size++] = '\n';
+               }
        }
        else {
                size = xsize_t(st.st_size);
@@ -249,19 +257,21 @@ int add_excludes_from_file_to_list(const char *fname,
                        close(fd);
                        return 0;
                }
-               buf = xmalloc(size);
+               buf = xmalloc(size+1);
                if (read_in_full(fd, buf, size) != size) {
+                       free(buf);
                        close(fd);
                        return -1;
                }
+               buf[size++] = '\n';
                close(fd);
        }
 
        if (buf_p)
                *buf_p = buf;
        entry = buf;
-       for (i = 0; i <= size; i++) {
-               if (i == size || buf[i] == '\n') {
+       for (i = 0; i < size; i++) {
+               if (buf[i] == '\n') {
                        if (entry != buf + i && entry[0] != '#') {
                                buf[i - (i && buf[i-1] == '\r')] = 0;
                                add_exclude(entry, base, baselen, which);
@@ -655,6 +665,92 @@ static int get_dtype(struct dirent *de, const char *path, int len)
        return dtype;
 }
 
+enum path_treatment {
+       path_ignored,
+       path_handled,
+       path_recurse,
+};
+
+static enum path_treatment treat_one_path(struct dir_struct *dir,
+                                         char *path, int *len,
+                                         const struct path_simplify *simplify,
+                                         int dtype, struct dirent *de)
+{
+       int exclude = excluded(dir, path, &dtype);
+       if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
+           && in_pathspec(path, *len, simplify))
+               dir_add_ignored(dir, path, *len);
+
+       /*
+        * Excluded? If we don't explicitly want to show
+        * ignored files, ignore it
+        */
+       if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
+               return path_ignored;
+
+       if (dtype == DT_UNKNOWN)
+               dtype = get_dtype(de, path, *len);
+
+       /*
+        * Do we want to see just the ignored files?
+        * We still need to recurse into directories,
+        * even if we don't ignore them, since the
+        * directory may contain files that we do..
+        */
+       if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
+               if (dtype != DT_DIR)
+                       return path_ignored;
+       }
+
+       switch (dtype) {
+       default:
+               return path_ignored;
+       case DT_DIR:
+               memcpy(path + *len, "/", 2);
+               (*len)++;
+               switch (treat_directory(dir, path, *len, simplify)) {
+               case show_directory:
+                       if (exclude != !!(dir->flags
+                                         & DIR_SHOW_IGNORED))
+                               return path_ignored;
+                       break;
+               case recurse_into_directory:
+                       return path_recurse;
+               case ignore_directory:
+                       return path_ignored;
+               }
+               break;
+       case DT_REG:
+       case DT_LNK:
+               break;
+       }
+       return path_handled;
+}
+
+static enum path_treatment treat_path(struct dir_struct *dir,
+                                     struct dirent *de,
+                                     char *path, int path_max,
+                                     int baselen,
+                                     const struct path_simplify *simplify,
+                                     int *len)
+{
+       int dtype;
+
+       if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
+               return path_ignored;
+       *len = strlen(de->d_name);
+       /* Ignore overly long pathnames! */
+       if (*len + baselen + 8 > path_max)
+               return path_ignored;
+       memcpy(path + baselen, de->d_name, *len + 1);
+       *len += baselen;
+       if (simplify_away(path, *len, simplify))
+               return path_ignored;
+
+       dtype = DTYPE(de);
+       return treat_one_path(dir, path, len, simplify, dtype, de);
+}
+
 /*
  * Read a directory tree. We currently ignore anything but
  * directories, regular files and symlinks. That's because git
@@ -664,7 +760,10 @@ static int get_dtype(struct dirent *de, const char *path, int len)
  * Also, we ignore the name ".git" (even if it is not a directory).
  * That likely will not change.
  */
-static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
+static int read_directory_recursive(struct dir_struct *dir,
+                                   const char *base, int baselen,
+                                   int check_only,
+                                   const struct path_simplify *simplify)
 {
        DIR *fdir = opendir(*base ? base : ".");
        int contents = 0;
@@ -675,70 +774,16 @@ static int read_directory_recursive(struct dir_struct *dir, const char *base, in
                memcpy(path, base, baselen);
 
                while ((de = readdir(fdir)) != NULL) {
-                       int len, dtype;
-                       int exclude;
-
-                       if (is_dot_or_dotdot(de->d_name) ||
-                            !strcmp(de->d_name, ".git"))
-                               continue;
-                       len = strlen(de->d_name);
-                       /* Ignore overly long pathnames! */
-                       if (len + baselen + 8 > sizeof(path))
-                               continue;
-                       memcpy(path + baselen, de->d_name, len+1);
-                       len = baselen + len;
-                       if (simplify_away(path, len, simplify))
-                               continue;
-
-                       dtype = DTYPE(de);
-                       exclude = excluded(dir, path, &dtype);
-                       if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
-                           && in_pathspec(path, len, simplify))
-                               dir_add_ignored(dir, path,len);
-
-                       /*
-                        * Excluded? If we don't explicitly want to show
-                        * ignored files, ignore it
-                        */
-                       if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
+                       int len;
+                       switch (treat_path(dir, de, path, sizeof(path),
+                                          baselen, simplify, &len)) {
+                       case path_recurse:
+                               contents += read_directory_recursive
+                                       (dir, path, len, 0, simplify);
                                continue;
-
-                       if (dtype == DT_UNKNOWN)
-                               dtype = get_dtype(de, path, len);
-
-                       /*
-                        * Do we want to see just the ignored files?
-                        * We still need to recurse into directories,
-                        * even if we don't ignore them, since the
-                        * directory may contain files that we do..
-                        */
-                       if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
-                               if (dtype != DT_DIR)
-                                       continue;
-                       }
-
-                       switch (dtype) {
-                       default:
+                       case path_ignored:
                                continue;
-                       case DT_DIR:
-                               memcpy(path + len, "/", 2);
-                               len++;
-                               switch (treat_directory(dir, path, len, simplify)) {
-                               case show_directory:
-                                       if (exclude != !!(dir->flags
-                                                       & DIR_SHOW_IGNORED))
-                                               continue;
-                                       break;
-                               case recurse_into_directory:
-                                       contents += read_directory_recursive(dir,
-                                               path, len, 0, simplify);
-                                       continue;
-                               case ignore_directory:
-                                       continue;
-                               }
-                               break;
-                       case DT_REG:
-                       case DT_LNK:
+                       case path_handled:
                                break;
                        }
                        contents++;
@@ -808,6 +853,41 @@ static void free_simplify(struct path_simplify *simplify)
        free(simplify);
 }
 
+static int treat_leading_path(struct dir_struct *dir,
+                             const char *path, int len,
+                             const struct path_simplify *simplify)
+{
+       char pathbuf[PATH_MAX];
+       int baselen, blen;
+       const char *cp;
+
+       while (len && path[len - 1] == '/')
+               len--;
+       if (!len)
+               return 1;
+       baselen = 0;
+       while (1) {
+               cp = path + baselen + !!baselen;
+               cp = memchr(cp, '/', path + len - cp);
+               if (!cp)
+                       baselen = len;
+               else
+                       baselen = cp - path;
+               memcpy(pathbuf, path, baselen);
+               pathbuf[baselen] = '\0';
+               if (!is_directory(pathbuf))
+                       return 0;
+               if (simplify_away(pathbuf, baselen, simplify))
+                       return 0;
+               blen = baselen;
+               if (treat_one_path(dir, pathbuf, &blen, simplify,
+                                  DT_DIR, NULL) == path_ignored)
+                       return 0; /* do not recurse into it */
+               if (len <= baselen)
+                       return 1; /* finished checking */
+       }
+}
+
 int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
 {
        struct path_simplify *simplify;
@@ -816,7 +896,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const char
                return dir->nr;
 
        simplify = create_simplify(pathspec);
-       read_directory_recursive(dir, path, len, 0, simplify);
+       if (!len || treat_leading_path(dir, path, len, simplify))
+               read_directory_recursive(dir, path, len, 0, simplify);
        free_simplify(simplify);
        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
@@ -963,7 +1044,7 @@ int remove_path(const char *name)
                slash = dirs + (slash - name);
                do {
                        *slash = '\0';
-               } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
+               } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
                free(dirs);
        }
        return 0;