]> asedeno.scripts.mit.edu Git - git.git/blobdiff - read-cache.c
Merge branch 'maint'
[git.git] / read-cache.c
index 6339a278da1ae1b323b5abf7d2604c7afdfde4e4..d2f332a6222cc3543bde8fa661c54c5c904cf561 100644 (file)
@@ -5,6 +5,7 @@
  */
 #include "cache.h"
 #include "cache-tree.h"
+#include "refs.h"
 
 /* Index extensions.
  *
@@ -24,8 +25,6 @@ unsigned int active_nr, active_alloc, active_cache_changed;
 
 struct cache_tree *active_cache_tree;
 
-int cache_errno;
-
 static void *cache_mmap;
 static size_t cache_mmap_size;
 
@@ -93,6 +92,23 @@ static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
        return match;
 }
 
+static int ce_compare_gitlink(struct cache_entry *ce)
+{
+       unsigned char sha1[20];
+
+       /*
+        * We don't actually require that the .git directory
+        * under DIRLNK directory be a valid git directory. It
+        * might even be missing (in case nobody populated that
+        * sub-project).
+        *
+        * If so, we consider it always to match.
+        */
+       if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
+               return 0;
+       return hashcmp(sha1, ce->sha1);
+}
+
 static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
 {
        switch (st->st_mode & S_IFMT) {
@@ -104,6 +120,9 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
                if (ce_compare_link(ce, xsize_t(st->st_size)))
                        return DATA_CHANGED;
                break;
+       case S_IFDIR:
+               if (S_ISDIRLNK(ntohl(ce->ce_mode)))
+                       return 0;
        default:
                return TYPE_CHANGED;
        }
@@ -129,6 +148,12 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
                    (has_symlinks || !S_ISREG(st->st_mode)))
                        changed |= TYPE_CHANGED;
                break;
+       case S_IFDIRLNK:
+               if (!S_ISDIR(st->st_mode))
+                       changed |= TYPE_CHANGED;
+               else if (ce_compare_gitlink(ce))
+                       changed |= DATA_CHANGED;
+               return changed;
        default:
                die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
        }
@@ -327,7 +352,7 @@ int remove_file_from_cache(const char *path)
        return 0;
 }
 
-int add_file_to_index(const char *path, int verbose)
+int add_file_to_cache(const char *path, int verbose)
 {
        int size, namelen;
        struct stat st;
@@ -336,10 +361,14 @@ int add_file_to_index(const char *path, int verbose)
        if (lstat(path, &st))
                die("%s: unable to stat (%s)", path, strerror(errno));
 
-       if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
-               die("%s: can only add regular files or symbolic links", path);
+       if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
+               die("%s: can only add regular files, symbolic links or git-directories", path);
 
        namelen = strlen(path);
+       if (S_ISDIR(st.st_mode)) {
+               while (namelen && path[namelen-1] == '/')
+                       namelen--;
+       }
        size = cache_entry_size(namelen);
        ce = xcalloc(1, size);
        memcpy(ce->name, path, namelen);
@@ -487,6 +516,8 @@ static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replac
                        continue;
                if (p->name[len] != '/')
                        continue;
+               if (!ce_stage(p) && !p->ce_mode)
+                       continue;
                retval = -1;
                if (!ok_to_replace)
                        break;
@@ -519,26 +550,37 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
 
                pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
                if (pos >= 0) {
-                       retval = -1;
-                       if (!ok_to_replace)
-                               break;
-                       remove_cache_entry_at(pos);
-                       continue;
+                       /*
+                        * Found one, but not so fast.  This could
+                        * be a marker that says "I was here, but
+                        * I am being removed".  Such an entry is
+                        * not a part of the resulting tree, and
+                        * it is Ok to have a directory at the same
+                        * path.
+                        */
+                       if (stage || active_cache[pos]->ce_mode) {
+                               retval = -1;
+                               if (!ok_to_replace)
+                                       break;
+                               remove_cache_entry_at(pos);
+                               continue;
+                       }
                }
+               else
+                       pos = -pos-1;
 
                /*
                 * Trivial optimization: if we find an entry that
                 * already matches the sub-directory, then we know
                 * we're ok, and we can exit.
                 */
-               pos = -pos-1;
                while (pos < active_nr) {
                        struct cache_entry *p = active_cache[pos];
                        if ((ce_namelen(p) <= len) ||
                            (p->name[len] != '/') ||
                            memcmp(p->name, name, len))
                                break; /* not our subdirectory */
-                       if (ce_stage(p) == stage)
+                       if (ce_stage(p) == stage && (stage || p->ce_mode))
                                /* p is at the same stage as our entry, and
                                 * is a subdirectory of what we are looking
                                 * at, so we cannot have conflicts at our
@@ -562,12 +604,21 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
  */
 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
 {
+       int retval;
+
+       /*
+        * When ce is an "I am going away" entry, we allow it to be added
+        */
+       if (!ce_stage(ce) && !ce->ce_mode)
+               return 0;
+
        /*
         * We check if the path is a sub-path of a subsequent pathname
         * first, since removing those will not change the position
-        * in the array
+        * in the array.
         */
-       int retval = has_file_name(ce, pos, ok_to_replace);
+       retval = has_file_name(ce, pos, ok_to_replace);
+
        /*
         * Then check if the path might have a clashing sub-directory
         * before it.
@@ -643,14 +694,15 @@ int add_cache_entry(struct cache_entry *ce, int option)
  * For example, you'd want to do this after doing a "git-read-tree",
  * to link up the stat cache details with the proper files.
  */
-struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
+static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really, int *err)
 {
        struct stat st;
        struct cache_entry *updated;
        int changed, size;
 
        if (lstat(ce->name, &st) < 0) {
-               cache_errno = errno;
+               if (err)
+                       *err = errno;
                return NULL;
        }
 
@@ -664,7 +716,8 @@ struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
        }
 
        if (ce_modified(ce, &st, really)) {
-               cache_errno = EINVAL;
+               if (err)
+                       *err = EINVAL;
                return NULL;
        }
 
@@ -696,6 +749,8 @@ int refresh_cache(unsigned int flags)
 
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce, *new;
+               int cache_errno = 0;
+
                ce = active_cache[i];
                if (ce_stage(ce)) {
                        while ((i < active_nr) &&
@@ -709,7 +764,7 @@ int refresh_cache(unsigned int flags)
                        continue;
                }
 
-               new = refresh_cache_entry(ce, really);
+               new = refresh_cache_ent(ce, really, &cache_errno);
                if (new == ce)
                        continue;
                if (!new) {
@@ -737,6 +792,11 @@ int refresh_cache(unsigned int flags)
        return has_errors;
 }
 
+struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
+{
+       return refresh_cache_ent(ce, really, NULL);
+}
+
 static int verify_hdr(struct cache_header *hdr, unsigned long size)
 {
        SHA_CTX c;