Code

config: Add new option to open an editor.
[git.git] / read-cache.c
index 22a814311d2cfc032129b43f6da56706c7026b9c..940ec76fdf231ac1345079ca2dc5da88925bcfb6 100644 (file)
@@ -99,27 +99,21 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st)
 static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
 {
        int match = -1;
-       char *target;
        void *buffer;
        unsigned long size;
        enum object_type type;
-       int len;
+       struct strbuf sb = STRBUF_INIT;
 
-       target = xmalloc(expected_size);
-       len = readlink(ce->name, target, expected_size);
-       if (len != expected_size) {
-               free(target);
+       if (strbuf_readlink(&sb, ce->name, expected_size))
                return -1;
-       }
+
        buffer = read_sha1_file(ce->sha1, &type, &size);
-       if (!buffer) {
-               free(target);
-               return -1;
+       if (buffer) {
+               if (size == sb.len)
+                       match = memcmp(buffer, sb.buf, size);
+               free(buffer);
        }
-       if (size == expected_size)
-               match = memcmp(buffer, target, size);
-       free(buffer);
-       free(target);
+       strbuf_release(&sb);
        return match;
 }
 
@@ -257,6 +251,14 @@ int ie_match_stat(const struct index_state *istate,
        if (!ignore_valid && (ce->ce_flags & CE_VALID))
                return 0;
 
+       /*
+        * Intent-to-add entries have not been added, so the index entry
+        * by definition never matches what is in the work tree until it
+        * actually gets added.
+        */
+       if (ce->ce_flags & CE_INTENT_TO_ADD)
+               return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
+
        changed = ce_match_stat_basic(ce, st);
 
        /*
@@ -546,6 +548,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
        ce->ce_flags = namelen;
        if (!intent_only)
                fill_stat_cache_info(ce, st);
+       else
+               ce->ce_flags |= CE_INTENT_TO_ADD;
 
        if (trust_executable_bit && has_symlinks)
                ce->ce_mode = create_ce_mode(st_mode);
@@ -1098,7 +1102,7 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
 
        if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
                return error("bad signature");
-       if (hdr->hdr_version != htonl(2))
+       if (hdr->hdr_version != htonl(2) && hdr->hdr_version != htonl(3))
                return error("bad index version");
        git_SHA1_Init(&c);
        git_SHA1_Update(&c, hdr, size - 20);
@@ -1133,6 +1137,7 @@ int read_index(struct index_state *istate)
 static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_entry *ce)
 {
        size_t len;
+       const char *name;
 
        ce->ce_ctime = ntohl(ondisk->ctime.sec);
        ce->ce_mtime = ntohl(ondisk->mtime.sec);
@@ -1145,19 +1150,31 @@ static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_en
        /* On-disk flags are just 16 bits */
        ce->ce_flags = ntohs(ondisk->flags);
 
-       /* For future extension: we do not understand this entry yet */
-       if (ce->ce_flags & CE_EXTENDED)
-               die("Unknown index entry format");
        hashcpy(ce->sha1, ondisk->sha1);
 
        len = ce->ce_flags & CE_NAMEMASK;
+
+       if (ce->ce_flags & CE_EXTENDED) {
+               struct ondisk_cache_entry_extended *ondisk2;
+               int extended_flags;
+               ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
+               extended_flags = ntohs(ondisk2->flags2) << 16;
+               /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
+               if (extended_flags & ~CE_EXTENDED_FLAGS)
+                       die("Unknown index entry format %08x", extended_flags);
+               ce->ce_flags |= extended_flags;
+               name = ondisk2->name;
+       }
+       else
+               name = ondisk->name;
+
        if (len == CE_NAMEMASK)
-               len = strlen(ondisk->name);
+               len = strlen(name);
        /*
         * NEEDSWORK: If the original index is crafted, this copy could
         * go unchecked.
         */
-       memcpy(ce->name, ondisk->name, len + 1);
+       memcpy(ce->name, name, len + 1);
 }
 
 static inline size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
@@ -1422,6 +1439,7 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
 {
        int size = ondisk_ce_size(ce);
        struct ondisk_cache_entry *ondisk = xcalloc(1, size);
+       char *name;
 
        ondisk->ctime.sec = htonl(ce->ce_ctime);
        ondisk->ctime.nsec = 0;
@@ -1435,7 +1453,15 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
        ondisk->size = htonl(ce->ce_size);
        hashcpy(ondisk->sha1, ce->sha1);
        ondisk->flags = htons(ce->ce_flags);
-       memcpy(ondisk->name, ce->name, ce_namelen(ce));
+       if (ce->ce_flags & CE_EXTENDED) {
+               struct ondisk_cache_entry_extended *ondisk2;
+               ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
+               ondisk2->flags2 = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
+               name = ondisk2->name;
+       }
+       else
+               name = ondisk->name;
+       memcpy(name, ce->name, ce_namelen(ce));
 
        return ce_write(c, fd, ondisk, size);
 }
@@ -1444,16 +1470,25 @@ int write_index(const struct index_state *istate, int newfd)
 {
        git_SHA_CTX c;
        struct cache_header hdr;
-       int i, err, removed;
+       int i, err, removed, extended;
        struct cache_entry **cache = istate->cache;
        int entries = istate->cache_nr;
 
-       for (i = removed = 0; i < entries; i++)
+       for (i = removed = extended = 0; i < entries; i++) {
                if (cache[i]->ce_flags & CE_REMOVE)
                        removed++;
 
+               /* reduce extended entries if possible */
+               cache[i]->ce_flags &= ~CE_EXTENDED;
+               if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) {
+                       extended++;
+                       cache[i]->ce_flags |= CE_EXTENDED;
+               }
+       }
+
        hdr.hdr_signature = htonl(CACHE_SIGNATURE);
-       hdr.hdr_version = htonl(2);
+       /* for extended format, increase version so older git won't try to read it */
+       hdr.hdr_version = htonl(extended ? 3 : 2);
        hdr.hdr_entries = htonl(entries - removed);
 
        git_SHA1_Init(&c);
@@ -1486,7 +1521,7 @@ int write_index(const struct index_state *istate, int newfd)
 
 /*
  * Read the index file that is potentially unmerged into given
- * index_state, dropping any unmerged entries.  Returns true is
+ * index_state, dropping any unmerged entries.  Returns true if
  * the index is unmerged.  Callers who want to refuse to work
  * from an unmerged state can call this and check its return value,
  * instead of calling read_cache().
@@ -1539,6 +1574,26 @@ static void update_callback(struct diff_queue_struct *q,
                default:
                        die("unexpected diff status %c", p->status);
                case DIFF_STATUS_UNMERGED:
+                       /*
+                        * ADD_CACHE_IGNORE_REMOVAL is unset if "git
+                        * add -u" is calling us, In such a case, a
+                        * missing work tree file needs to be removed
+                        * if there is an unmerged entry at stage #2,
+                        * but such a diff record is followed by
+                        * another with DIFF_STATUS_DELETED (and if
+                        * there is no stage #2, we won't see DELETED
+                        * nor MODIFIED).  We can simply continue
+                        * either way.
+                        */
+                       if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL))
+                               continue;
+                       /*
+                        * Otherwise, it is "git add path" is asking
+                        * to explicitly add it; we fall through.  A
+                        * missing work tree file is an error and is
+                        * caught by add_file_to_index() in such a
+                        * case.
+                        */
                case DIFF_STATUS_MODIFIED:
                case DIFF_STATUS_TYPE_CHANGED:
                        if (add_file_to_index(&the_index, path, data->flags)) {