Merge branch 'bw/grep-recurse-submodules'
[gitweb.git] / tree-diff.c
index e164e532b2f76617d822c3bef9492b9525b56e83..2357f72899f8f47e497ffd1bb66a0d76d7dbe012 100644 (file)
 } while(0)
 
 static struct combine_diff_path *ll_diff_tree_paths(
-       struct combine_diff_path *p, const unsigned char *sha1,
-       const unsigned char **parents_sha1, int nparent,
+       struct combine_diff_path *p, const struct object_id *oid,
+       const struct object_id **parents_oid, int nparent,
        struct strbuf *base, struct diff_options *opt);
-static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
-                            struct strbuf *base, struct diff_options *opt);
+static int ll_diff_tree_oid(const struct object_id *old_oid,
+                           const struct object_id *new_oid,
+                           struct strbuf *base, struct diff_options *opt);
 
 /*
  * Compare two tree entries, taking into account only path/S_ISDIR(mode),
@@ -74,25 +75,25 @@ static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_
 {
        struct combine_diff_parent *p0 = &p->parent[0];
        if (p->mode && p0->mode) {
-               opt->change(opt, p0->mode, p->mode, p0->oid.hash, p->oid.hash,
+               opt->change(opt, p0->mode, p->mode, &p0->oid, &p->oid,
                        1, 1, p->path, 0, 0);
        }
        else {
-               const unsigned char *sha1;
+               const struct object_id *oid;
                unsigned int mode;
                int addremove;
 
                if (p->mode) {
                        addremove = '+';
-                       sha1 = p->oid.hash;
+                       oid = &p->oid;
                        mode = p->mode;
                } else {
                        addremove = '-';
-                       sha1 = p0->oid.hash;
+                       oid = &p0->oid;
                        mode = p0->mode;
                }
 
-               opt->add_remove(opt, addremove, mode, sha1, 1, p->path, 0);
+               opt->add_remove(opt, addremove, mode, oid, 1, p->path, 0);
        }
 
        return 0;       /* we are done with p */
@@ -131,7 +132,7 @@ static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_
  */
 static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
        int nparent, const struct strbuf *base, const char *path, int pathlen,
-       unsigned mode, const unsigned char *sha1)
+       unsigned mode, const struct object_id *oid)
 {
        struct combine_diff_path *p;
        size_t len = st_add(base->len, pathlen);
@@ -140,8 +141,7 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
        /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
        p = last->next;
        if (p && (alloclen > (intptr_t)p->next)) {
-               free(p);
-               p = NULL;
+               FREE_AND_NULL(p);
        }
 
        if (!p) {
@@ -161,7 +161,7 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
        memcpy(p->path + base->len, path, pathlen);
        p->path[len] = 0;
        p->mode = mode;
-       hashcpy(p->oid.hash, sha1 ? sha1 : null_sha1);
+       oidcpy(&p->oid, oid ? oid : &null_oid);
 
        return p;
 }
@@ -183,7 +183,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
 {
        unsigned mode;
        const char *path;
-       const unsigned char *sha1;
+       const struct object_id *oid;
        int pathlen;
        int old_baselen = base->len;
        int i, isdir, recurse = 0, emitthis = 1;
@@ -193,7 +193,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
 
        if (t) {
                /* path present in resulting tree */
-               sha1 = tree_entry_extract(t, &path, &mode)->hash;
+               oid = tree_entry_extract(t, &path, &mode);
                pathlen = tree_entry_len(&t->entry);
                isdir = S_ISDIR(mode);
        } else {
@@ -208,7 +208,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
                pathlen = tree_entry_len(&tp[imin].entry);
 
                isdir = S_ISDIR(mode);
-               sha1 = NULL;
+               oid = NULL;
                mode = 0;
        }
 
@@ -220,7 +220,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
        if (emitthis) {
                int keep;
                struct combine_diff_path *pprev = p;
-               p = path_appendnew(p, nparent, base, path, pathlen, mode, sha1);
+               p = path_appendnew(p, nparent, base, path, pathlen, mode, oid);
 
                for (i = 0; i < nparent; ++i) {
                        /*
@@ -229,7 +229,7 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
                         */
                        int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
 
-                       const unsigned char *sha1_i;
+                       const struct object_id *oid_i;
                        unsigned mode_i;
 
                        p->parent[i].status =
@@ -239,16 +239,16 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
                                                DIFF_STATUS_ADDED;
 
                        if (tpi_valid) {
-                               sha1_i = tp[i].entry.oid->hash;
+                               oid_i = tp[i].entry.oid;
                                mode_i = tp[i].entry.mode;
                        }
                        else {
-                               sha1_i = NULL;
+                               oid_i = &null_oid;
                                mode_i = 0;
                        }
 
                        p->parent[i].mode = mode_i;
-                       hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1);
+                       oidcpy(&p->parent[i].oid, oid_i);
                }
 
                keep = 1;
@@ -273,21 +273,20 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *p,
        }
 
        if (recurse) {
-               const unsigned char **parents_sha1;
+               const struct object_id **parents_oid;
 
-               FAST_ARRAY_ALLOC(parents_sha1, nparent);
+               FAST_ARRAY_ALLOC(parents_oid, nparent);
                for (i = 0; i < nparent; ++i) {
                        /* same rule as in emitthis */
                        int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
 
-                       parents_sha1[i] = tpi_valid ? tp[i].entry.oid->hash
-                                                   : NULL;
+                       parents_oid[i] = tpi_valid ? tp[i].entry.oid : NULL;
                }
 
                strbuf_add(base, path, pathlen);
                strbuf_addch(base, '/');
-               p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt);
-               FAST_ARRAY_FREE(parents_sha1, nparent);
+               p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
+               FAST_ARRAY_FREE(parents_oid, nparent);
        }
 
        strbuf_setlen(base, old_baselen);
@@ -312,7 +311,7 @@ static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
 
 
 /*
- * generate paths for combined diff D(sha1,parents_sha1[])
+ * generate paths for combined diff D(sha1,parents_oid[])
  *
  * Resulting paths are appended to combine_diff_path linked list, and also, are
  * emitted on the go via opt->pathchange() callback, so it is possible to
@@ -404,8 +403,8 @@ static inline void update_tp_entries(struct tree_desc *tp, int nparent)
 }
 
 static struct combine_diff_path *ll_diff_tree_paths(
-       struct combine_diff_path *p, const unsigned char *sha1,
-       const unsigned char **parents_sha1, int nparent,
+       struct combine_diff_path *p, const struct object_id *oid,
+       const struct object_id **parents_oid, int nparent,
        struct strbuf *base, struct diff_options *opt)
 {
        struct tree_desc t, *tp;
@@ -419,11 +418,12 @@ static struct combine_diff_path *ll_diff_tree_paths(
         * load parents first, as they are probably already cached.
         *
         * ( log_tree_diff() parses commit->parent before calling here via
-        *   diff_tree_sha1(parent, commit) )
+        *   diff_tree_oid(parent, commit) )
         */
        for (i = 0; i < nparent; ++i)
-               tptree[i] = fill_tree_descriptor(&tp[i], parents_sha1[i]);
-       ttree = fill_tree_descriptor(&t, sha1);
+               tptree[i] = fill_tree_descriptor(&tp[i],
+                               parents_oid[i] ? parents_oid[i]->hash : NULL);
+       ttree = fill_tree_descriptor(&t, oid ? oid->hash : NULL);
 
        /* Enable recursion indefinitely */
        opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
@@ -548,19 +548,18 @@ static struct combine_diff_path *ll_diff_tree_paths(
 }
 
 struct combine_diff_path *diff_tree_paths(
-       struct combine_diff_path *p, const unsigned char *sha1,
-       const unsigned char **parents_sha1, int nparent,
+       struct combine_diff_path *p, const struct object_id *oid,
+       const struct object_id **parents_oid, int nparent,
        struct strbuf *base, struct diff_options *opt)
 {
-       p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt);
+       p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
 
        /*
         * free pre-allocated last element, if any
         * (see path_appendnew() for details about why)
         */
        if (p->next) {
-               free(p->next);
-               p->next = NULL;
+               FREE_AND_NULL(p->next);
        }
 
        return p;
@@ -577,7 +576,9 @@ static inline int diff_might_be_rename(void)
                !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
 }
 
-static void try_to_follow_renames(const unsigned char *old, const unsigned char *new, struct strbuf *base, struct diff_options *opt)
+static void try_to_follow_renames(const struct object_id *old_oid,
+                                 const struct object_id *new_oid,
+                                 struct strbuf *base, struct diff_options *opt)
 {
        struct diff_options diff_opts;
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -615,7 +616,7 @@ static void try_to_follow_renames(const unsigned char *old, const unsigned char
        diff_opts.break_opt = opt->break_opt;
        diff_opts.rename_score = opt->rename_score;
        diff_setup_done(&diff_opts);
-       ll_diff_tree_sha1(old, new, base, &diff_opts);
+       ll_diff_tree_oid(old_oid, new_oid, base, &diff_opts);
        diffcore_std(&diff_opts);
        clear_pathspec(&diff_opts.pathspec);
 
@@ -674,15 +675,16 @@ static void try_to_follow_renames(const unsigned char *old, const unsigned char
        q->nr = 1;
 }
 
-static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
-                            struct strbuf *base, struct diff_options *opt)
+static int ll_diff_tree_oid(const struct object_id *old_oid,
+                           const struct object_id *new_oid,
+                           struct strbuf *base, struct diff_options *opt)
 {
        struct combine_diff_path phead, *p;
        pathchange_fn_t pathchange_old = opt->pathchange;
 
        phead.next = NULL;
        opt->pathchange = emit_diff_first_parent_only;
-       diff_tree_paths(&phead, new, &old, 1, base, opt);
+       diff_tree_paths(&phead, new_oid, &old_oid, 1, base, opt);
 
        for (p = phead.next; p;) {
                struct combine_diff_path *pprev = p;
@@ -694,7 +696,9 @@ static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
        return 0;
 }
 
-int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base_str, struct diff_options *opt)
+int diff_tree_oid(const struct object_id *old_oid,
+                 const struct object_id *new_oid,
+                 const char *base_str, struct diff_options *opt)
 {
        struct strbuf base;
        int retval;
@@ -702,16 +706,16 @@ int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const cha
        strbuf_init(&base, PATH_MAX);
        strbuf_addstr(&base, base_str);
 
-       retval = ll_diff_tree_sha1(old, new, &base, opt);
+       retval = ll_diff_tree_oid(old_oid, new_oid, &base, opt);
        if (!*base_str && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename())
-               try_to_follow_renames(old, new, &base, opt);
+               try_to_follow_renames(old_oid, new_oid, &base, opt);
 
        strbuf_release(&base);
 
        return retval;
 }
 
-int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
+int diff_root_tree_oid(const struct object_id *new_oid, const char *base, struct diff_options *opt)
 {
-       return diff_tree_sha1(NULL, new, base, opt);
+       return diff_tree_oid(NULL, new_oid, base, opt);
 }