rm: absorb a submodules git dir before deletion
[gitweb.git] / dir.c
diff --git a/dir.c b/dir.c
index 69e0be6aa28d6e9bcfa3baf4f80de5bcc3158fe0..d872cc1570989bf22d8bd5d741d817372e76018f 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -53,26 +53,16 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        int check_only, const struct path_simplify *simplify);
 static int get_dtype(struct dirent *de, const char *path, int len);
 
-static struct trace_key trace_exclude = TRACE_KEY_INIT(EXCLUDE);
-
-/* helper string functions with support for the ignore_case flag */
-int strcmp_icase(const char *a, const char *b)
+int fspathcmp(const char *a, const char *b)
 {
        return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
 }
 
-int strncmp_icase(const char *a, const char *b, size_t count)
+int fspathncmp(const char *a, const char *b, size_t count)
 {
        return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
 }
 
-int fnmatch_icase(const char *pattern, const char *string, int flags)
-{
-       return wildmatch(pattern, string,
-                        flags | (ignore_case ? WM_CASEFOLD : 0),
-                        NULL);
-}
-
 int git_fnmatch(const struct pathspec_item *item,
                const char *pattern, const char *string,
                int prefix)
@@ -217,8 +207,9 @@ int within_depth(const char *name, int namelen,
        return 1;
 }
 
-#define DO_MATCH_EXCLUDE   1
-#define DO_MATCH_DIRECTORY 2
+#define DO_MATCH_EXCLUDE   (1<<0)
+#define DO_MATCH_DIRECTORY (1<<1)
+#define DO_MATCH_SUBMODULE (1<<2)
 
 /*
  * Does 'match' match the given name?
@@ -293,6 +284,32 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
                         item->nowildcard_len - prefix))
                return MATCHED_FNMATCH;
 
+       /* Perform checks to see if "name" is a super set of the pathspec */
+       if (flags & DO_MATCH_SUBMODULE) {
+               /* name is a literal prefix of the pathspec */
+               if ((namelen < matchlen) &&
+                   (match[namelen] == '/') &&
+                   !ps_strncmp(item, match, name, namelen))
+                       return MATCHED_RECURSIVELY;
+
+               /* name" doesn't match up to the first wild character */
+               if (item->nowildcard_len < item->len &&
+                   ps_strncmp(item, match, name,
+                              item->nowildcard_len - prefix))
+                       return 0;
+
+               /*
+                * Here is where we would perform a wildmatch to check if
+                * "name" can be matched as a directory (or a prefix) against
+                * the pathspec.  Since wildmatch doesn't have this capability
+                * at the present we have to punt and say that it is a match,
+                * potentially returning a false positive
+                * The submodules themselves will be able to perform more
+                * accurate matching to determine if the pathspec matches.
+                */
+               return MATCHED_RECURSIVELY;
+       }
+
        return 0;
 }
 
@@ -396,6 +413,21 @@ int match_pathspec(const struct pathspec *ps,
        return negative ? 0 : positive;
 }
 
+/**
+ * Check if a submodule is a superset of the pathspec
+ */
+int submodule_path_match(const struct pathspec *ps,
+                        const char *submodule_name,
+                        char *seen)
+{
+       int matched = do_match_pathspec(ps, submodule_name,
+                                       strlen(submodule_name),
+                                       0, seen,
+                                       DO_MATCH_DIRECTORY |
+                                       DO_MATCH_SUBMODULE);
+       return matched;
+}
+
 int report_path_error(const char *ps_matched,
                      const struct pathspec *pathspec,
                      const char *prefix)
@@ -459,7 +491,7 @@ int no_wildcard(const char *string)
 
 void parse_exclude_pattern(const char **pattern,
                           int *patternlen,
-                          int *flags,
+                          unsigned *flags,
                           int *nowildcardlen)
 {
        const char *p = *pattern;
@@ -500,7 +532,7 @@ void add_exclude(const char *string, const char *base,
 {
        struct exclude *x;
        int patternlen;
-       int flags;
+       unsigned flags;
        int nowildcardlen;
 
        parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
@@ -516,7 +548,6 @@ void add_exclude(const char *string, const char *base,
        x->baselen = baselen;
        x->flags = flags;
        x->srcpos = srcpos;
-       string_list_init(&x->sticky_paths, 1);
        ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
        el->excludes[el->nr++] = x;
        x->el = el;
@@ -536,7 +567,7 @@ static void *read_skip_worktree_file_from_index(const char *path, size_t *size,
                return NULL;
        if (!ce_skip_worktree(active_cache[pos]))
                return NULL;
-       data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
+       data = read_sha1_file(active_cache[pos]->oid.hash, &type, &sz);
        if (!data || type != OBJ_BLOB) {
                free(data);
                return NULL;
@@ -544,7 +575,7 @@ static void *read_skip_worktree_file_from_index(const char *path, size_t *size,
        *size = xsize_t(sz);
        if (sha1_stat) {
                memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
-               hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
+               hashcpy(sha1_stat->sha1, active_cache[pos]->oid.hash);
        }
        return data;
 }
@@ -557,10 +588,8 @@ void clear_exclude_list(struct exclude_list *el)
 {
        int i;
 
-       for (i = 0; i < el->nr; i++) {
-               string_list_clear(&el->excludes[i]->sticky_paths, 0);
+       for (i = 0; i < el->nr; i++)
                free(el->excludes[i]);
-       }
        free(el->excludes);
        free(el->filebuf);
 
@@ -726,7 +755,8 @@ static int add_excludes(const char *fname, const char *base, int baselen,
                                 !ce_stage(active_cache[pos]) &&
                                 ce_uptodate(active_cache[pos]) &&
                                 !would_convert_to_git(fname))
-                               hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
+                               hashcpy(sha1_stat->sha1,
+                                       active_cache[pos]->oid.hash);
                        else
                                hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
                        fill_stat_data(&sha1_stat->stat, &st);
@@ -803,16 +833,16 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 
 int match_basename(const char *basename, int basenamelen,
                   const char *pattern, int prefix, int patternlen,
-                  int flags)
+                  unsigned flags)
 {
        if (prefix == patternlen) {
                if (patternlen == basenamelen &&
-                   !strncmp_icase(pattern, basename, basenamelen))
+                   !fspathncmp(pattern, basename, basenamelen))
                        return 1;
        } else if (flags & EXC_FLAG_ENDSWITH) {
                /* "*literal" matching against "fooliteral" */
                if (patternlen - 1 <= basenamelen &&
-                   !strncmp_icase(pattern + 1,
+                   !fspathncmp(pattern + 1,
                                   basename + basenamelen - (patternlen - 1),
                                   patternlen - 1))
                        return 1;
@@ -828,7 +858,7 @@ int match_basename(const char *basename, int basenamelen,
 int match_pathname(const char *pathname, int pathlen,
                   const char *base, int baselen,
                   const char *pattern, int prefix, int patternlen,
-                  int flags)
+                  unsigned flags)
 {
        const char *name;
        int namelen;
@@ -849,7 +879,7 @@ int match_pathname(const char *pathname, int pathlen,
         */
        if (pathlen < baselen + 1 ||
            (baselen && pathname[baselen] != '/') ||
-           strncmp_icase(pathname, base, baselen))
+           fspathncmp(pathname, base, baselen))
                return 0;
 
        namelen = baselen ? pathlen - baselen - 1 : pathlen;
@@ -863,7 +893,7 @@ int match_pathname(const char *pathname, int pathlen,
                if (prefix > namelen)
                        return 0;
 
-               if (strncmp_icase(pattern, name, prefix))
+               if (fspathncmp(pattern, name, prefix))
                        return 0;
                pattern += prefix;
                patternlen -= prefix;
@@ -875,7 +905,7 @@ int match_pathname(const char *pathname, int pathlen,
                 * then our prefix match is all we need; we
                 * do not need to call fnmatch at all.
                 */
-               if (!patternlen && (!namelen || *name == '/'))
+               if (!patternlen && !namelen)
                        return 1;
        }
 
@@ -884,113 +914,6 @@ int match_pathname(const char *pathname, int pathlen,
                                 WM_PATHNAME) == 0;
 }
 
-static void add_sticky(struct exclude *exc, const char *pathname, int pathlen)
-{
-       struct strbuf sb = STRBUF_INIT;
-       int i;
-
-       for (i = exc->sticky_paths.nr - 1; i >= 0; i--) {
-               const char *sticky = exc->sticky_paths.items[i].string;
-               int len = strlen(sticky);
-
-               if (pathlen < len && sticky[pathlen] == '/' &&
-                   !strncmp(pathname, sticky, pathlen))
-                       return;
-       }
-
-       strbuf_add(&sb, pathname, pathlen);
-       string_list_append_nodup(&exc->sticky_paths, strbuf_detach(&sb, NULL));
-}
-
-static int match_sticky(struct exclude *exc, const char *pathname, int pathlen, int dtype)
-{
-       int i;
-
-       for (i = exc->sticky_paths.nr - 1; i >= 0; i--) {
-               const char *sticky = exc->sticky_paths.items[i].string;
-               int len = strlen(sticky);
-
-               if (pathlen == len && dtype == DT_DIR &&
-                   !strncmp(pathname, sticky, len))
-                       return 1;
-
-               if (pathlen > len && pathname[len] == '/' &&
-                   !strncmp(pathname, sticky, len))
-                       return 1;
-       }
-
-       return 0;
-}
-
-static inline int different_decisions(const struct exclude *a,
-                                     const struct exclude *b)
-{
-       return (a->flags & EXC_FLAG_NEGATIVE) != (b->flags & EXC_FLAG_NEGATIVE);
-}
-
-/*
- * Return non-zero if pathname is a directory and an ancestor of the
- * literal path in a pattern.
- */
-static int match_directory_part(const char *pathname, int pathlen,
-                               int *dtype, struct exclude *x)
-{
-       const char      *base       = x->base;
-       int              baselen    = x->baselen ? x->baselen - 1 : 0;
-       const char      *pattern    = x->pattern;
-       int              prefix     = x->nowildcardlen;
-       int              patternlen = x->patternlen;
-
-       if (*dtype == DT_UNKNOWN)
-               *dtype = get_dtype(NULL, pathname, pathlen);
-       if (*dtype != DT_DIR)
-               return 0;
-
-       if (*pattern == '/') {
-               pattern++;
-               patternlen--;
-               prefix--;
-       }
-
-       if (baselen) {
-               if (((pathlen < baselen && base[pathlen] == '/') ||
-                    pathlen == baselen) &&
-                   !strncmp_icase(pathname, base, pathlen))
-                       return 1;
-               pathname += baselen + 1;
-               pathlen  -= baselen + 1;
-       }
-
-
-       if (prefix &&
-           (((pathlen < prefix && pattern[pathlen] == '/') ||
-             pathlen == prefix) &&
-            !strncmp_icase(pathname, pattern, pathlen)))
-               return 1;
-
-       return 0;
-}
-
-static struct exclude *should_descend(const char *pathname, int pathlen,
-                                     int *dtype, struct exclude_list *el,
-                                     struct exclude *exc)
-{
-       int i;
-
-       for (i = el->nr - 1; 0 <= i; i--) {
-               struct exclude *x = el->excludes[i];
-
-               if (x == exc)
-                       break;
-
-               if (!(x->flags & EXC_FLAG_NODIR) &&
-                   different_decisions(x, exc) &&
-                   match_directory_part(pathname, pathlen, dtype, x))
-                       return x;
-       }
-       return NULL;
-}
-
 /*
  * Scan the given exclude list in reverse to see whether pathname
  * should be ignored.  The first match (i.e. the last on the list), if
@@ -1004,32 +927,16 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
                                                       struct exclude_list *el)
 {
        struct exclude *exc = NULL; /* undecided */
-       int i, maybe_descend = 0;
+       int i;
 
        if (!el->nr)
                return NULL;    /* undefined */
 
-       trace_printf_key(&trace_exclude, "exclude: from %s\n", el->src);
-
        for (i = el->nr - 1; 0 <= i; i--) {
                struct exclude *x = el->excludes[i];
                const char *exclude = x->pattern;
                int prefix = x->nowildcardlen;
 
-               if (!maybe_descend && i < el->nr - 1 &&
-                   different_decisions(x, el->excludes[i+1]))
-                       maybe_descend = 1;
-
-               if (x->sticky_paths.nr) {
-                       if (*dtype == DT_UNKNOWN)
-                               *dtype = get_dtype(NULL, pathname, pathlen);
-                       if (match_sticky(x, pathname, pathlen, *dtype)) {
-                               exc = x;
-                               break;
-                       }
-                       continue;
-               }
-
                if (x->flags & EXC_FLAG_MUSTBEDIR) {
                        if (*dtype == DT_UNKNOWN)
                                *dtype = get_dtype(NULL, pathname, pathlen);
@@ -1056,45 +963,6 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
                        break;
                }
        }
-
-       if (!exc) {
-               trace_printf_key(&trace_exclude, "exclude: %.*s => n/a\n",
-                                pathlen, pathname);
-               return NULL;
-       }
-
-       /*
-        * We have found a matching pattern "exc" that may exclude whole
-        * directory. We also found that there may be a pattern that matches
-        * something inside the directory and reincludes stuff.
-        *
-        * Go through the patterns again, find that pattern and double check.
-        * If it's true, return "undecided" and keep descending in. "exc" is
-        * marked sticky so that it continues to match inside the directory.
-        */
-       if (!(exc->flags & EXC_FLAG_NEGATIVE) && maybe_descend) {
-               struct exclude *x;
-
-               if (*dtype == DT_UNKNOWN)
-                       *dtype = get_dtype(NULL, pathname, pathlen);
-
-               if (*dtype == DT_DIR &&
-                   (x = should_descend(pathname, pathlen, dtype, el, exc))) {
-                       add_sticky(exc, pathname, pathlen);
-                       trace_printf_key(&trace_exclude,
-                                        "exclude: %.*s vs %s at line %d => %s,"
-                                        " forced open by %s at line %d => n/a\n",
-                                        pathlen, pathname, exc->pattern, exc->srcpos,
-                                        exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes",
-                                        x->pattern, x->srcpos);
-                       return NULL;
-               }
-       }
-
-       trace_printf_key(&trace_exclude, "exclude: %.*s vs %s at line %d => %s%s\n",
-                        pathlen, pathname, exc->pattern, exc->srcpos,
-                        exc->flags & EXC_FLAG_NEGATIVE ? "no" : "yes",
-                        exc->sticky_paths.nr ? " (stuck)" : "");
        return exc;
 }
 
@@ -1840,13 +1708,9 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        struct cached_dir cdir;
        enum path_treatment state, subdir_state, dir_state = path_none;
        struct strbuf path = STRBUF_INIT;
-       static int level = 0;
 
        strbuf_add(&path, base, baselen);
 
-       trace_printf_key(&trace_exclude, "exclude: [%d] enter '%.*s'\n",
-                        level++, baselen, base);
-
        if (open_cached_dir(&cdir, dir, untracked, &path, check_only))
                goto out;
 
@@ -1910,8 +1774,6 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
        }
        close_cached_dir(&cdir);
  out:
-       trace_printf_key(&trace_exclude, "exclude: [%d] leave '%.*s'\n",
-                        --level, baselen, base);
        strbuf_release(&path);
 
        return dir_state;
@@ -2148,25 +2010,6 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
        return root;
 }
 
-static void clear_sticky(struct dir_struct *dir)
-{
-       struct exclude_list_group *g;
-       struct exclude_list *el;
-       struct exclude *x;
-       int i, j, k;
-
-       for (i = EXC_CMDL; i <= EXC_FILE; i++) {
-               g = &dir->exclude_list_group[i];
-               for (j = g->nr - 1; j >= 0; j--) {
-                       el = &g->el[j];
-                       for (k = el->nr - 1; 0 <= k; k--) {
-                               x = el->excludes[k];
-                               string_list_clear(&x->sticky_paths, 0);
-                       }
-               }
-       }
-}
-
 int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)
 {
        struct path_simplify *simplify;
@@ -2187,12 +2030,6 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
        if (has_symlink_leading_path(path, len))
                return dir->nr;
 
-       /*
-        * Stay on the safe side. if read_directory() has run once on
-        * "dir", some sticky flag may have been left. Clear them all.
-        */
-       clear_sticky(dir);
-
        /*
         * exclude patterns are treated like positive ones in
         * create_simplify. Usually exclude patterns should be a
@@ -2210,8 +2047,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
        if (!len || treat_leading_path(dir, path, len, simplify))
                read_directory_recursive(dir, path, len, untracked, 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);
+       QSORT(dir->entries, dir->nr, cmp_name);
+       QSORT(dir->ignored, dir->ignored_nr, cmp_name);
        if (dir->untracked) {
                static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
                trace_printf_key(&trace_untracked_stats,
@@ -2400,8 +2237,6 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
-       const char *path;
-
        dir->exclude_per_dir = ".gitignore";
 
        /* core.excludefile defaulting to $XDG_HOME/git/ignore */
@@ -2412,10 +2247,12 @@ void setup_standard_excludes(struct dir_struct *dir)
                                         dir->untracked ? &dir->ss_excludes_file : NULL);
 
        /* per repository user preference */
-       path = git_path_info_exclude();
-       if (!access_or_warn(path, R_OK, 0))
-               add_excludes_from_file_1(dir, path,
-                                        dir->untracked ? &dir->ss_info_exclude : NULL);
+       if (startup_info->have_repository) {
+               const char *path = git_path_info_exclude();
+               if (!access_or_warn(path, R_OK, 0))
+                       add_excludes_from_file_1(dir, path,
+                                                dir->untracked ? &dir->ss_info_exclude : NULL);
+       }
 }
 
 int remove_path(const char *name)
@@ -2570,7 +2407,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
 
        varint_len = encode_varint(untracked->ident.len, varbuf);
        strbuf_add(out, varbuf, varint_len);
-       strbuf_add(out, untracked->ident.buf, untracked->ident.len);
+       strbuf_addbuf(out, &untracked->ident);
 
        strbuf_add(out, ouc, ouc_size(len));
        free(ouc);
@@ -2911,3 +2748,40 @@ void untracked_cache_add_to_index(struct index_state *istate,
 {
        untracked_cache_invalidate_path(istate, path);
 }
+
+/* Update gitfile and core.worktree setting to connect work tree and git dir */
+void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)
+{
+       struct strbuf file_name = STRBUF_INIT;
+       struct strbuf rel_path = STRBUF_INIT;
+       char *git_dir = xstrdup(real_path(git_dir_));
+       char *work_tree = xstrdup(real_path(work_tree_));
+
+       /* Update gitfile */
+       strbuf_addf(&file_name, "%s/.git", work_tree);
+       write_file(file_name.buf, "gitdir: %s",
+                  relative_path(git_dir, work_tree, &rel_path));
+
+       /* Update core.worktree setting */
+       strbuf_reset(&file_name);
+       strbuf_addf(&file_name, "%s/config", git_dir);
+       git_config_set_in_file(file_name.buf, "core.worktree",
+                              relative_path(work_tree, git_dir, &rel_path));
+
+       strbuf_release(&file_name);
+       strbuf_release(&rel_path);
+       free(work_tree);
+       free(git_dir);
+}
+
+/*
+ * Migrate the git directory of the given path from old_git_dir to new_git_dir.
+ */
+void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir)
+{
+       if (rename(old_git_dir, new_git_dir) < 0)
+               die_errno(_("could not migrate git directory from '%s' to '%s'"),
+                       old_git_dir, new_git_dir);
+
+       connect_work_tree_and_git_dir(path, new_git_dir);
+}