rev-list.c: fix non-grammatical comments.
[gitweb.git] / read-tree.c
index fed2a3c544c0090d04175042fe5999d9c86bc1a9..52f06e312ab5b8808401affed6291051d7a746d5 100644 (file)
@@ -3,6 +3,8 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
+#define DBRT_DEBUG 1
+
 #include "cache.h"
 
 #include "object.h"
 
 static int merge = 0;
 static int update = 0;
+static int index_only = 0;
+static int nontrivial_merge = 0;
+static int trivial_merges_only = 0;
+static int aggressive = 0;
 
 static int head_idx = -1;
 static int merge_size = 0;
@@ -47,8 +53,6 @@ static int entcmp(char *name1, int dir1, char *name2, int dir2)
        return ret;
 }
 
-#define DBRT_DEBUG 0
-
 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                            const char *base, merge_fn_t fn, int *indpos)
 {
@@ -101,14 +105,14 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                        }
                }
 
-#if DBRT_DEBUG
+#if DBRT_DEBUG > 1
                if (first)
                        printf("index %s\n", first);
 #endif
                for (i = 0; i < len; i++) {
                        if (!posns[i] || posns[i] == &df_conflict_list)
                                continue;
-#if DBRT_DEBUG
+#if DBRT_DEBUG > 1
                        printf("%d %s\n", i + 1, posns[i]->name);
 #endif
                        if (!first || entcmp(first, firstdir,
@@ -188,7 +192,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                        if (merge) {
                                int ret;
 
-#if DBRT_DEBUG
+#if DBRT_DEBUG > 1
                                printf("%s:\n", first);
                                for (i = 0; i < src_size; i++) {
                                        printf(" %d ", i);
@@ -200,7 +204,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 #endif
                                ret = fn(src);
                                
-#if DBRT_DEBUG
+#if DBRT_DEBUG > 1
                                printf("Added %d entries\n", ret);
 #endif
                                *indpos += ret;
@@ -221,6 +225,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                        if (unpack_trees_rec(subposns, len, newbase, fn,
                                             indpos))
                                return -1;
+                       free(newbase);
                }
                free(subposns);
                free(src);
@@ -233,6 +238,35 @@ static void reject_merge(struct cache_entry *ce)
            ce->name);
 }
 
+/* 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.
+ */
+static void unlink_entry(char *name)
+{
+       char *cp, *prev;
+
+       if (unlink(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;
+       }
+}
+
 static void check_updates(struct cache_entry **src, int nr)
 {
        static struct checkout state = {
@@ -246,7 +280,7 @@ static void check_updates(struct cache_entry **src, int nr)
                struct cache_entry *ce = *src++;
                if (!ce->ce_mode) {
                        if (update)
-                               unlink(ce->name);
+                               unlink_entry(ce->name);
                        continue;
                }
                if (ce->ce_flags & mask) {
@@ -261,17 +295,23 @@ static int unpack_trees(merge_fn_t fn)
 {
        int indpos = 0;
        unsigned len = object_list_length(trees);
-       struct tree_entry_list **posns = 
-               xmalloc(len * sizeof(struct tree_entry_list *));
+       struct tree_entry_list **posns;
        int i;
        struct object_list *posn = trees;
        merge_size = len;
-       for (i = 0; i < len; i++) {
-               posns[i] = ((struct tree *) posn->item)->entries;
-               posn = posn->next;
+
+       if (len) {
+               posns = xmalloc(len * sizeof(struct tree_entry_list *));
+               for (i = 0; i < len; i++) {
+                       posns[i] = ((struct tree *) posn->item)->entries;
+                       posn = posn->next;
+               }
+               if (unpack_trees_rec(posns, len, "", fn, &indpos))
+                       return -1;
        }
-       if (unpack_trees_rec(posns, len, "", fn, &indpos))
-               return -1;
+
+       if (trivial_merges_only && nontrivial_merge)
+               die("Merge requires file-level merging");
 
        check_updates(active_cache, active_nr);
        return 0;
@@ -305,8 +345,11 @@ static void verify_uptodate(struct cache_entry *ce)
 {
        struct stat st;
 
+       if (index_only)
+               return;
+
        if (!lstat(ce->name, &st)) {
-               unsigned changed = ce_match_stat(ce, &st);
+               unsigned changed = ce_match_stat(ce, &st, 1);
                if (!changed)
                        return;
                errno = 0;
@@ -353,6 +396,22 @@ static int keep_entry(struct cache_entry *ce)
        return 1;
 }
 
+#if DBRT_DEBUG
+static void show_stage_entry(FILE *o,
+                            const char *label, const struct cache_entry *ce)
+{
+       if (!ce)
+               fprintf(o, "%s (missing)\n", label);
+       else
+               fprintf(o, "%s%06o %s %d\t%s\n",
+                       label,
+                       ntohl(ce->ce_mode),
+                       sha1_to_hex(ce->sha1),
+                       ce_stage(ce),
+                       ce->name);
+}
+#endif
+
 static int threeway_merge(struct cache_entry **stages)
 {
        struct cache_entry *index;
@@ -366,11 +425,14 @@ static int threeway_merge(struct cache_entry **stages)
        int df_conflict_remote = 0;
 
        int any_anc_missing = 0;
+       int no_anc_exists = 1;
        int i;
 
        for (i = 1; i < head_idx; i++) {
                if (!stages[i])
                        any_anc_missing = 1;
+               else
+                       no_anc_exists = 0;
        }
 
        index = stages[0];
@@ -392,10 +454,10 @@ static int threeway_merge(struct cache_entry **stages)
        if (!same(remote, head)) {
                for (i = 1; i < head_idx; i++) {
                        if (same(stages[i], head)) {
-                               head_match = 1;
+                               head_match = i;
                        }
                        if (same(stages[i], remote)) {
-                               remote_match = 1;
+                               remote_match = i;
                        }
                }
        }
@@ -431,6 +493,29 @@ static int threeway_merge(struct cache_entry **stages)
        if (!head && !remote && any_anc_missing)
                return 0;
 
+       /* Under the new "aggressive" rule, we resolve mostly trivial
+        * cases that we historically had git-merge-one-file resolve.
+        */
+       if (aggressive) {
+               int head_deleted = !head && !df_conflict_head;
+               int remote_deleted = !remote && !df_conflict_remote;
+               /*
+                * Deleted in both.
+                * Deleted in one and unchanged in the other.
+                */
+               if ((head_deleted && remote_deleted) ||
+                   (head_deleted && remote && remote_match) ||
+                   (remote_deleted && head && head_match))
+                       return 0;
+
+               /*
+                * Added in both, identically.
+                */
+               if (no_anc_exists && head && remote && same(head, remote))
+                       return merged_entry(head, index);
+
+       }
+
        /* Below are "no merge" cases, which require that the index be
         * up-to-date to avoid the files getting overwritten with
         * conflict resolution files. 
@@ -439,6 +524,8 @@ static int threeway_merge(struct cache_entry **stages)
                verify_uptodate(index);
        }
 
+       nontrivial_merge = 1;
+
        /* #2, #3, #4, #6, #7, #9, #11. */
        count = 0;
        if (!head_match || !remote_match) {
@@ -450,6 +537,13 @@ static int threeway_merge(struct cache_entry **stages)
                        }
                }
        }
+#if DBRT_DEBUG
+       else {
+               fprintf(stderr, "read-tree: warning #16 detected\n");
+               show_stage_entry(stderr, "head   ", stages[head_match]);
+               show_stage_entry(stderr, "remote ", stages[remote_match]);
+       }
+#endif
        if (head) { count += keep_entry(head); }
        if (remote) { count += keep_entry(remote); }
        return count;
@@ -555,7 +649,7 @@ static int read_cache_unmerged(void)
        return deleted;
 }
 
-static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
+static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
 
 static struct cache_file cache_file;
 
@@ -565,21 +659,35 @@ int main(int argc, char **argv)
        unsigned char sha1[20];
        merge_fn_t fn = NULL;
 
+       setup_git_directory();
+
        newfd = hold_index_file_for_update(&cache_file, get_index_file());
        if (newfd < 0)
                die("unable to create new cachefile");
 
+       git_config(git_default_config);
+
        merge = 0;
        reset = 0;
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
-               /* "-u" means "update", meaning that a merge will update the working directory */
+               /* "-u" means "update", meaning that a merge will update
+                * the working tree.
+                */
                if (!strcmp(arg, "-u")) {
                        update = 1;
                        continue;
                }
 
+               /* "-i" means "index only", meaning that a merge will
+                * not even look at the working tree.
+                */
+               if (!strcmp(arg, "-i")) {
+                       index_only = 1;
+                       continue;
+               }
+
                /* This differs from "-m" in that we'll silently ignore unmerged entries */
                if (!strcmp(arg, "--reset")) {
                        if (stage || merge)
@@ -591,9 +699,14 @@ int main(int argc, char **argv)
                        continue;
                }
 
-               if (!strcmp(arg, "--head")) {
-                       head_idx = stage - 1;
-                       fn = threeway_merge;
+               if (!strcmp(arg, "--trivial")) {
+                       trivial_merges_only = 1;
+                       continue;
+               }
+
+               if (!strcmp(arg, "--aggressive")) {
+                       aggressive = 1;
+                       continue;
                }
 
                /* "-m" stands for "merge", meaning we start in stage 1 */
@@ -607,15 +720,20 @@ int main(int argc, char **argv)
                        continue;
                }
 
+               /* using -u and -i at the same time makes no sense */
+               if (1 < index_only + update)
+                       usage(read_tree_usage);
+
                if (get_sha1(arg, sha1) < 0)
                        usage(read_tree_usage);
                if (list_tree(sha1) < 0)
                        die("failed to unpack tree object %s", arg);
                stage++;
        }
-       if (update && !merge)
+       if ((update||index_only) && !merge)
                usage(read_tree_usage);
-       if (merge && !fn) {
+
+       if (merge) {
                if (stage < 2)
                        die("just how do you expect me to merge %d trees?", stage-1);
                switch (stage - 1) {
@@ -632,9 +750,7 @@ int main(int argc, char **argv)
                        fn = threeway_merge;
                        break;
                }
-       }
 
-       if (head_idx < 0) {
                if (stage - 1 >= 3)
                        head_idx = stage - 2;
                else