Fix random segfaults in pack-objects.
[gitweb.git] / unpack-trees.c
index e496d8cd31b54dd3d278499d7d85439478181ce8..2e2232cbb07e61de3be74302fba67142a58a857b 100644 (file)
@@ -1,6 +1,5 @@
-#include <signal.h>
-#include <sys/time.h>
 #include "cache.h"
+#include "dir.h"
 #include "tree.h"
 #include "tree-walk.h"
 #include "cache-tree.h"
@@ -77,6 +76,12 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 {
        int baselen = strlen(base);
        int src_size = len + 1;
+       int i_stk = i_stk;
+       int retval = 0;
+
+       if (o->dir)
+               i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
+
        do {
                int i;
                const char *first;
@@ -143,7 +148,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                }
                /* No name means we're done */
                if (!first)
-                       return 0;
+                       goto leave_directory;
 
                pathlen = strlen(first);
                ce_size = cache_entry_size(baselen + pathlen);
@@ -200,7 +205,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
 
                        any_files = 1;
 
-                       memcpy(ce->sha1, posns[i]->sha1, 20);
+                       hashcpy(ce->sha1, posns[i]->sha1);
                        src[i + o->merge] = ce;
                        subposns[i] = df_conflict_list;
                        posns[i] = posns[i]->next;
@@ -240,13 +245,20 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
                        newbase[baselen + pathlen] = '/';
                        newbase[baselen + pathlen + 1] = '\0';
                        if (unpack_trees_rec(subposns, len, newbase, o,
-                                            indpos, df_conflict_list))
-                               return -1;
+                                            indpos, df_conflict_list)) {
+                               retval = -1;
+                               goto leave_directory;
+                       }
                        free(newbase);
                }
                free(subposns);
                free(src);
        } while (1);
+
+ leave_directory:
+       if (o->dir)
+               pop_exclude_per_directory(o->dir, i_stk);
+       return retval;
 }
 
 /* Unlink the last component and attempt to remove leading
@@ -278,7 +290,7 @@ static void unlink_entry(char *name)
        }
 }
 
-static volatile sig_atomic_t progress_update = 0;
+static volatile sig_atomic_t progress_update;
 
 static void progress_interval(int signum)
 {
@@ -370,7 +382,7 @@ int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
        int i;
        struct object_list *posn = trees;
        struct tree_entry_list df_conflict_list;
-       struct cache_entry df_conflict_entry;
+       static struct cache_entry *dfc;
 
        memset(&df_conflict_list, 0, sizeof(df_conflict_list));
        df_conflict_list.next = &df_conflict_list;
@@ -381,8 +393,10 @@ int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
        state.refresh_cache = 1;
 
        o->merge_size = len;
-       memset(&df_conflict_entry, 0, sizeof(df_conflict_entry));
-       o->df_conflict_entry = &df_conflict_entry;
+
+       if (!dfc)
+               dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
+       o->df_conflict_entry = dfc;
 
        if (len) {
                posns = xmalloc(len * sizeof(struct tree_entry_list *));
@@ -417,7 +431,7 @@ static int same(struct cache_entry *a, struct cache_entry *b)
        if (!a && !b)
                return 1;
        return a->ce_mode == b->ce_mode &&
-               !memcmp(a->sha1, b->sha1, 20);
+              !hashcmp(a->sha1, b->sha1);
 }
 
 
@@ -456,7 +470,7 @@ static void invalidate_ce_path(struct cache_entry *ce)
 
 /*
  * We do not want to remove or overwrite a working tree file that
- * is not tracked.
+ * is not tracked, unless it is ignored.
  */
 static void verify_absent(const char *path, const char *action,
                struct unpack_trees_options *o)
@@ -465,7 +479,7 @@ static void verify_absent(const char *path, const char *action,
 
        if (o->index_only || o->reset || !o->update)
                return;
-       if (!lstat(path, &st))
+       if (!lstat(path, &st) && !(o->dir && excluded(o->dir, path)))
                die("Untracked working tree file '%s' "
                    "would be %s by merge.", path, action);
 }
@@ -642,7 +656,7 @@ int threeway_merge(struct cache_entry **stages,
                    (remote_deleted && head && head_match)) {
                        if (index)
                                return deleted_entry(index, index, o);
-                       else if (path)
+                       else if (path && !head_deleted)
                                verify_absent(path, "removed", o);
                        return 0;
                }
@@ -661,8 +675,6 @@ int threeway_merge(struct cache_entry **stages,
        if (index) {
                verify_uptodate(index, o);
        }
-       else if (path)
-               verify_absent(path, "overwritten", o);
 
        o->nontrivial_merge = 1;