git-svn: t0000: add -f flag to checkout
[gitweb.git] / builtin-read-tree.c
index da0731ca0e3e22dd8cbb9fbde9201c66de406b4a..bb50fbd274e2d7e73fc3c6f86a73bd22f636400e 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "object.h"
 #include "tree.h"
+#include "tree-walk.h"
+#include "cache-tree.h"
 #include <sys/time.h>
 #include <signal.h>
 #include "builtin.h"
@@ -28,7 +30,17 @@ static int merge_size = 0;
 
 static struct object_list *trees = NULL;
 
-static struct cache_entry df_conflict_entry = { 
+static struct cache_entry df_conflict_entry = {
+};
+
+struct tree_entry_list {
+       struct tree_entry_list *next;
+       unsigned directory : 1;
+       unsigned executable : 1;
+       unsigned symlink : 1;
+       unsigned int mode;
+       const char *name;
+       const unsigned char *sha1;
 };
 
 static struct tree_entry_list df_conflict_list = {
@@ -38,6 +50,34 @@ static struct tree_entry_list df_conflict_list = {
 
 typedef int (*merge_fn_t)(struct cache_entry **src);
 
+static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
+{
+       struct tree_desc desc;
+       struct name_entry one;
+       struct tree_entry_list *ret = NULL;
+       struct tree_entry_list **list_p = &ret;
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &one)) {
+               struct tree_entry_list *entry;
+
+               entry = xmalloc(sizeof(struct tree_entry_list));
+               entry->name = one.path;
+               entry->sha1 = one.sha1;
+               entry->mode = one.mode;
+               entry->directory = S_ISDIR(one.mode) != 0;
+               entry->executable = (one.mode & S_IXUSR) != 0;
+               entry->symlink = S_ISLNK(one.mode) != 0;
+               entry->next = NULL;
+
+               *list_p = entry;
+               list_p = &entry->next;
+       }
+       return ret;
+}
+
 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
 {
        int len1 = strlen(name1);
@@ -336,6 +376,7 @@ static void check_updates(struct cache_entry **src, int nr)
                                        fprintf(stderr, "%4u%% (%u/%u) done\r",
                                                percent, cnt, total);
                                        last_percent = percent;
+                                       progress_update = 0;
                                }
                        }
                }
@@ -428,6 +469,12 @@ static void verify_uptodate(struct cache_entry *ce)
        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 }
 
+static void invalidate_ce_path(struct cache_entry *ce)
+{
+       if (ce)
+               cache_tree_invalidate_path(active_cache_tree, ce->name);
+}
+
 /*
  * We do not want to remove or overwrite a working tree file that
  * is not tracked.
@@ -458,10 +505,13 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
                        *merge = *old;
                } else {
                        verify_uptodate(old);
+                       invalidate_ce_path(old);
                }
        }
-       else
+       else {
                verify_absent(merge->name, "overwritten");
+               invalidate_ce_path(merge);
+       }
 
        merge->ce_flags &= ~htons(CE_STAGEMASK);
        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
@@ -476,6 +526,7 @@ static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
                verify_absent(ce->name, "removed");
        ce->ce_mode = 0;
        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
+       invalidate_ce_path(ce);
        return 1;
 }
 
@@ -741,29 +792,68 @@ static int oneway_merge(struct cache_entry **src)
 
 static int read_cache_unmerged(void)
 {
-       int i, deleted;
+       int i;
        struct cache_entry **dst;
+       struct cache_entry *last = NULL;
 
        read_cache();
        dst = active_cache;
-       deleted = 0;
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce = active_cache[i];
                if (ce_stage(ce)) {
-                       deleted++;
-                       continue;
+                       if (last && !strcmp(ce->name, last->name))
+                               continue;
+                       invalidate_ce_path(ce);
+                       last = ce;
+                       ce->ce_mode = 0;
+                       ce->ce_flags &= ~htons(CE_STAGEMASK);
+               }
+               *dst++ = ce;
+       }
+       active_nr = dst - active_cache;
+       return !!last;
+}
+
+static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
+{
+       struct tree_desc desc;
+       struct name_entry entry;
+       int cnt;
+
+       memcpy(it->sha1, tree->object.sha1, 20);
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+       cnt = 0;
+       while (tree_entry(&desc, &entry)) {
+               if (!S_ISDIR(entry.mode))
+                       cnt++;
+               else {
+                       struct cache_tree_sub *sub;
+                       struct tree *subtree = lookup_tree(entry.sha1);
+                       if (!subtree->object.parsed)
+                               parse_tree(subtree);
+                       sub = cache_tree_sub(it, entry.path);
+                       sub->cache_tree = cache_tree();
+                       prime_cache_tree_rec(sub->cache_tree, subtree);
+                       cnt += sub->cache_tree->entry_count;
                }
-               if (deleted)
-                       *dst = ce;
-               dst++;
        }
-       active_nr -= deleted;
-       return deleted;
+       it->entry_count = cnt;
+}
+
+static void prime_cache_tree(void)
+{
+       struct tree *tree = (struct tree *)trees->item;
+       if (!tree)
+               return;
+       active_cache_tree = cache_tree();
+       prime_cache_tree_rec(active_cache_tree, tree);
+
 }
 
 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int cmd_read_tree(int argc, const char **argv, char **envp)
 {
@@ -774,9 +864,9 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
        setup_git_directory();
        git_config(git_default_config);
 
-       newfd = hold_index_file_for_update(&cache_file, get_index_file());
+       newfd = hold_lock_file_for_update(&lock_file, get_index_file());
        if (newfd < 0)
-               die("unable to create new cachefile");
+               die("unable to create new index file");
 
        git_config(git_default_config);
 
@@ -806,7 +896,10 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
                        continue;
                }
 
-               /* This differs from "-m" in that we'll silently ignore unmerged entries */
+               /* This differs from "-m" in that we'll silently ignore
+                * unmerged entries and overwrite working tree files that
+                * correspond to them.
+                */
                if (!strcmp(arg, "--reset")) {
                        if (stage || merge)
                                usage(read_tree_usage);
@@ -862,10 +955,9 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
                        fn = twoway_merge;
                        break;
                case 3:
-                       fn = threeway_merge;
-                       break;
                default:
                        fn = threeway_merge;
+                       cache_tree_free(&active_cache_tree);
                        break;
                }
 
@@ -876,8 +968,20 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
        }
 
        unpack_trees(fn);
+
+       /*
+        * When reading only one tree (either the most basic form,
+        * "-m ent" or "--reset ent" form), we can obtain a fully
+        * valid cache-tree because the index must match exactly
+        * what came from the tree.
+        */
+       if (trees && trees->item && (!merge || (stage == 2))) {
+               cache_tree_free(&active_cache_tree);
+               prime_cache_tree();
+       }
+
        if (write_cache(newfd, active_cache, active_nr) ||
-           commit_index_file(&cache_file))
+           commit_lock_file(&lock_file))
                die("unable to write new index file");
        return 0;
 }