Merge branch 'jc/maint-1.6.0-read-tree-overlay'
authorJunio C Hamano <gitster@pobox.com>
Wed, 18 Mar 2009 01:58:55 +0000 (18:58 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 18 Mar 2009 01:58:55 +0000 (18:58 -0700)
* jc/maint-1.6.0-read-tree-overlay:
read-tree A B C: do not create a bogus index and do not segfault

1  2 
unpack-trees.c
diff --combined unpack-trees.c
index da2e3c09158195e98c20a08fb97f9b37a5d5a5a5,5b0a8c1728b30ebe06d12cc0a1e8c1a08e6caaef..86e28650b887b4fcc1b05cc4d3102367df18a9ac
@@@ -49,20 -49,39 +49,20 @@@ static void add_entry(struct unpack_tre
        memcpy(new, ce, size);
        new->next = NULL;
        new->ce_flags = (new->ce_flags & ~clear) | set;
-       add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
+       add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
  }
  
 -/* Unlink the last component and attempt to remove leading
 - * directories, in case this unlink is the removal of the
 - * last entry in the directory -- empty directories are removed.
 +/*
 + * Unlink the last component and schedule the leading directories for
 + * removal, such that empty directories get removed.
   */
  static void unlink_entry(struct cache_entry *ce)
  {
 -      char *cp, *prev;
 -      char *name = ce->name;
 -
 -      if (has_symlink_leading_path(ce_namelen(ce), ce->name))
 +      if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
                return;
 -      if (unlink(name))
 +      if (unlink(ce->name))
                return;
 -      prev = NULL;
 -      while (1) {
 -              int status;
 -              cp = strrchr(name, '/');
 -              if (prev)
 -                      *prev = '/';
 -              if (!cp)
 -                      break;
 -
 -              *cp = 0;
 -              status = rmdir(name);
 -              if (status) {
 -                      *cp = '/';
 -                      break;
 -              }
 -              prev = cp;
 -      }
 +      schedule_dir_for_removal(ce->name, ce_namelen(ce));
  }
  
  static struct checkout state;
@@@ -93,10 -112,11 +93,10 @@@ static int check_updates(struct unpack_
                        display_progress(progress, ++cnt);
                        if (o->update)
                                unlink_entry(ce);
 -                      remove_index_entry_at(&o->result, i);
 -                      i--;
 -                      continue;
                }
        }
 +      remove_marked_cache_entries(&o->result);
 +      remove_scheduled_dirs();
  
        for (i = 0; i < index->cache_nr; i++) {
                struct cache_entry *ce = index->cache[i];
@@@ -220,11 -240,8 +220,11 @@@ static struct cache_entry *create_ce_en
        return ce;
  }
  
 -static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
 -      const struct name_entry *names, const struct traverse_info *info)
 +static int unpack_nondirectories(int n, unsigned long mask,
 +                               unsigned long dirmask,
 +                               struct cache_entry **src,
 +                               const struct name_entry *names,
 +                               const struct traverse_info *info)
  {
        int i;
        struct unpack_trees_options *o = info->data;
        if (o->merge)
                return call_unpack_fn(src, o);
  
-       n += o->merge;
        for (i = 0; i < n; i++)
-               add_entry(o, src[i], 0, 0);
+               if (src[i] && src[i] != o->df_conflict_entry)
+                       add_entry(o, src[i], 0, 0);
        return 0;
  }
  
  static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
  {
 -      struct cache_entry *src[5] = { NULL, };
 +      struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
        struct unpack_trees_options *o = info->data;
        const struct name_entry *p = names;
  
@@@ -335,7 -352,7 +335,7 @@@ static int unpack_failed(struct unpack_
        discard_index(&o->result);
        if (!o->gently) {
                if (message)
 -                      return error(message);
 +                      return error("%s", message);
                return -1;
        }
        return -1;
@@@ -359,15 -376,12 +359,15 @@@ int unpack_trees(unsigned len, struct t
        state.refresh_cache = 1;
  
        memset(&o->result, 0, sizeof(o->result));
 -      if (o->src_index)
 -              o->result.timestamp = o->src_index->timestamp;
 +      o->result.initialized = 1;
 +      if (o->src_index) {
 +              o->result.timestamp.sec = o->src_index->timestamp.sec;
 +              o->result.timestamp.nsec = o->src_index->timestamp.nsec;
 +      }
        o->merge_size = len;
  
        if (!dfc)
 -              dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
 +              dfc = xcalloc(1, cache_entry_size(0));
        o->df_conflict_entry = dfc;
  
        if (len) {
@@@ -428,7 -442,7 +428,7 @@@ static int verify_uptodate(struct cache
  {
        struct stat st;
  
 -      if (o->index_only || o->reset)
 +      if (o->index_only || o->reset || ce_uptodate(ce))
                return 0;
  
        if (!lstat(ce->name, &st)) {
@@@ -479,7 -493,7 +479,7 @@@ static int verify_clean_subdirectory(st
         * anything in the existing directory there.
         */
        int namelen;
 -      int pos, i;
 +      int i;
        struct dir_struct d;
        char *pathbuf;
        int cnt = 0;
         * in that directory.
         */
        namelen = strlen(ce->name);
 -      pos = index_name_pos(o->src_index, ce->name, namelen);
 -      if (0 <= pos)
 -              return cnt; /* we have it as nondirectory */
 -      pos = -pos - 1;
 -      for (i = pos; i < o->src_index->cache_nr; i++) {
 -              struct cache_entry *ce = o->src_index->cache[i];
 -              int len = ce_namelen(ce);
 +      for (i = o->pos; i < o->src_index->cache_nr; i++) {
 +              struct cache_entry *ce2 = o->src_index->cache[i];
 +              int len = ce_namelen(ce2);
                if (len < namelen ||
 -                  strncmp(ce->name, ce->name, namelen) ||
 -                  ce->name[namelen] != '/')
 +                  strncmp(ce->name, ce2->name, namelen) ||
 +                  ce2->name[namelen] != '/')
                        break;
                /*
 -               * ce->name is an entry in the subdirectory.
 +               * ce2->name is an entry in the subdirectory.
                 */
 -              if (!ce_stage(ce)) {
 -                      if (verify_uptodate(ce, o))
 +              if (!ce_stage(ce2)) {
 +                      if (verify_uptodate(ce2, o))
                                return -1;
 -                      add_entry(o, ce, CE_REMOVE, 0);
 +                      add_entry(o, ce2, CE_REMOVE, 0);
                }
                cnt++;
        }
@@@ -565,11 -583,11 +565,11 @@@ static int verify_absent(struct cache_e
        if (o->index_only || o->reset || !o->update)
                return 0;
  
 -      if (has_symlink_leading_path(ce_namelen(ce), ce->name))
 +      if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
                return 0;
  
        if (!lstat(ce->name, &st)) {
 -              int cnt;
 +              int ret;
                int dtype = ce_to_dtype(ce);
                struct cache_entry *result;
  
                         * files that are in "foo/" we would lose
                         * it.
                         */
 -                      cnt = verify_clean_subdirectory(ce, action, o);
 +                      ret = verify_clean_subdirectory(ce, action, o);
 +                      if (ret < 0)
 +                              return ret;
  
                        /*
                         * If this removed entries from the index,
                         * what that means is:
                         *
 -                       * (1) the caller unpack_trees_rec() saw path/foo
 +                       * (1) the caller unpack_callback() saw path/foo
                         * in the index, and it has not removed it because
                         * it thinks it is handling 'path' as blob with
                         * D/F conflict;
                         * We need to increment it by the number of
                         * deleted entries here.
                         */
 -                      o->pos += cnt;
 +                      o->pos += ret;
                        return 0;
                }
  
@@@ -924,17 -940,8 +924,17 @@@ int twoway_merge(struct cache_entry **s
                        return -1;
                }
        }
 -      else if (newtree)
 +      else if (newtree) {
 +              if (oldtree && !o->initial_checkout) {
 +                      /*
 +                       * deletion of the path was staged;
 +                       */
 +                      if (same(oldtree, newtree))
 +                              return 1;
 +                      return reject_merge(oldtree, o);
 +              }
                return merged_entry(newtree, current, o);
 +      }
        return deleted_entry(oldtree, current, o);
  }