Merge branch 'fc/do-not-use-the-index-in-add-to-index' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 3 Jul 2013 22:40:38 +0000 (15:40 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Jul 2013 22:40:38 +0000 (15:40 -0700)
* fc/do-not-use-the-index-in-add-to-index:
read-cache: trivial style cleanups
read-cache: fix wrong 'the_index' usage

1  2 
read-cache.c
diff --combined read-cache.c
index 04ed561bfe7dd8fbf1988190f95eb1cc3530ff58,3bb5c91d06f3a1f0a83e05778d29d80fca1e3e9c..7040e7962bc598d706f1c37dff953bd52c7ab42f
@@@ -46,7 -46,7 +46,7 @@@ static void replace_index_entry(struct 
  {
        struct cache_entry *old = istate->cache[nr];
  
 -      remove_name_hash(old);
 +      remove_name_hash(istate, old);
        set_index_entry(istate, nr, ce);
        istate->cache_changed = 1;
  }
@@@ -197,25 -197,21 +197,25 @@@ static int ce_match_stat_basic(struct c
        }
        if (ce->ce_mtime.sec != (unsigned int)st->st_mtime)
                changed |= MTIME_CHANGED;
 -      if (trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime)
 +      if (trust_ctime && check_stat &&
 +          ce->ce_ctime.sec != (unsigned int)st->st_ctime)
                changed |= CTIME_CHANGED;
  
  #ifdef USE_NSEC
 -      if (ce->ce_mtime.nsec != ST_MTIME_NSEC(*st))
 +      if (check_stat && ce->ce_mtime.nsec != ST_MTIME_NSEC(*st))
                changed |= MTIME_CHANGED;
 -      if (trust_ctime && ce->ce_ctime.nsec != ST_CTIME_NSEC(*st))
 +      if (trust_ctime && check_stat &&
 +          ce->ce_ctime.nsec != ST_CTIME_NSEC(*st))
                changed |= CTIME_CHANGED;
  #endif
  
 -      if (ce->ce_uid != (unsigned int) st->st_uid ||
 -          ce->ce_gid != (unsigned int) st->st_gid)
 -              changed |= OWNER_CHANGED;
 -      if (ce->ce_ino != (unsigned int) st->st_ino)
 -              changed |= INODE_CHANGED;
 +      if (check_stat) {
 +              if (ce->ce_uid != (unsigned int) st->st_uid ||
 +                      ce->ce_gid != (unsigned int) st->st_gid)
 +                      changed |= OWNER_CHANGED;
 +              if (ce->ce_ino != (unsigned int) st->st_ino)
 +                      changed |= INODE_CHANGED;
 +      }
  
  #ifdef USE_STDEV
        /*
         * clients will have different views of what "device"
         * the filesystem is on
         */
 -      if (ce->ce_dev != (unsigned int) st->st_dev)
 -              changed |= INODE_CHANGED;
 +      if (check_stat && ce->ce_dev != (unsigned int) st->st_dev)
 +                      changed |= INODE_CHANGED;
  #endif
  
        if (ce->ce_size != (unsigned int) st->st_size)
@@@ -460,7 -456,7 +460,7 @@@ int remove_index_entry_at(struct index_
        struct cache_entry *ce = istate->cache[pos];
  
        record_resolve_undo(istate, ce);
 -      remove_name_hash(ce);
 +      remove_name_hash(istate, ce);
        istate->cache_changed = 1;
        istate->cache_nr--;
        if (pos >= istate->cache_nr)
@@@ -483,7 -479,7 +483,7 @@@ void remove_marked_cache_entries(struc
  
        for (i = j = 0; i < istate->cache_nr; i++) {
                if (ce_array[i]->ce_flags & CE_REMOVE)
 -                      remove_name_hash(ce_array[i]);
 +                      remove_name_hash(istate, ce_array[i]);
                else
                        ce_array[j++] = ce_array[i];
        }
@@@ -626,7 -622,7 +626,7 @@@ int add_to_index(struct index_state *is
                        if (*ptr == '/') {
                                struct cache_entry *foundce;
                                ++ptr;
-                               foundce = index_name_exists(&the_index, ce->name, ptr - ce->name, ignore_case);
+                               foundce = index_name_exists(istate, ce->name, ptr - ce->name, ignore_case);
                                if (foundce) {
                                        memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr);
                                        startPtr = ptr;
@@@ -979,7 -975,7 +979,7 @@@ int add_index_entry(struct index_state 
        if (istate->cache_nr == istate->cache_alloc) {
                istate->cache_alloc = alloc_nr(istate->cache_alloc);
                istate->cache = xrealloc(istate->cache,
-                                       istate->cache_alloc * sizeof(struct cache_entry *));
+                                       istate->cache_alloc * sizeof(*istate->cache));
        }
  
        /* Add it in.. */
@@@ -1260,7 -1256,7 +1260,7 @@@ static int verify_hdr(struct cache_head
        if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
                return error("bad signature");
        hdr_version = ntohl(hdr->hdr_version);
 -      if (hdr_version < 2 || 4 < hdr_version)
 +      if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
                return error("bad index version %d", hdr_version);
        git_SHA1_Init(&c);
        git_SHA1_Update(&c, hdr, size - 20);
@@@ -1449,7 -1445,7 +1449,7 @@@ int read_index_from(struct index_state 
        istate->version = ntohl(hdr->hdr_version);
        istate->cache_nr = ntohl(hdr->hdr_entries);
        istate->cache_alloc = alloc_nr(istate->cache_nr);
-       istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
+       istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
        istate->initialized = 1;
  
        if (istate->version == 4)
@@@ -1515,7 -1511,8 +1515,7 @@@ int discard_index(struct index_state *i
        istate->cache_changed = 0;
        istate->timestamp.sec = 0;
        istate->timestamp.nsec = 0;
 -      istate->name_hash_initialized = 0;
 -      free_hash(&istate->name_hash);
 +      free_name_hash(istate);
        cache_tree_free(&(istate->cache_tree));
        istate->initialized = 0;
  
@@@ -1899,37 -1896,3 +1899,37 @@@ int index_name_is_other(const struct in
        }
        return 1;
  }
 +
 +void *read_blob_data_from_index(struct index_state *istate, const char *path, unsigned long *size)
 +{
 +      int pos, len;
 +      unsigned long sz;
 +      enum object_type type;
 +      void *data;
 +
 +      len = strlen(path);
 +      pos = index_name_pos(istate, path, len);
 +      if (pos < 0) {
 +              /*
 +               * We might be in the middle of a merge, in which
 +               * case we would read stage #2 (ours).
 +               */
 +              int i;
 +              for (i = -pos - 1;
 +                   (pos < 0 && i < istate->cache_nr &&
 +                    !strcmp(istate->cache[i]->name, path));
 +                   i++)
 +                      if (ce_stage(istate->cache[i]) == 2)
 +                              pos = i;
 +      }
 +      if (pos < 0)
 +              return NULL;
 +      data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
 +      if (!data || type != OBJ_BLOB) {
 +              free(data);
 +              return NULL;
 +      }
 +      if (size)
 +              *size = sz;
 +      return data;
 +}