builtin/ls-files: convert overlay_tree_on_cache to object_id
[gitweb.git] / ref-filter.c
index 9c82b5b9d632bbbe66332b6c54badb5bc88d28d8..56fc990a5267e6d6d5c9d8e6cf1a6d02cc058120 100644 (file)
@@ -677,13 +677,13 @@ int verify_ref_format(const char *format)
  * by the "struct object" representation, set *eaten as well---it is a
  * signal from parse_object_buffer to us not to free the buffer.
  */
-static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned long *sz, int *eaten)
+static void *get_obj(const struct object_id *oid, struct object **obj, unsigned long *sz, int *eaten)
 {
        enum object_type type;
-       void *buf = read_sha1_file(sha1, &type, sz);
+       void *buf = read_sha1_file(oid->hash, &type, sz);
 
        if (buf)
-               *obj = parse_object_buffer(sha1, type, *sz, buf, eaten);
+               *obj = parse_object_buffer(oid->hash, type, *sz, buf, eaten);
        else
                *obj = NULL;
        return buf;
@@ -1293,7 +1293,7 @@ static void populate_value(struct ref_array_item *ref)
        struct object *obj;
        int eaten, i;
        unsigned long size;
-       const unsigned char *tagged;
+       const struct object_id *tagged;
 
        ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
 
@@ -1366,14 +1366,14 @@ static void populate_value(struct ref_array_item *ref)
                                v->s = xstrdup(buf + 1);
                        }
                        continue;
-               } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
+               } else if (!deref && grab_objectname(name, ref->objectname.hash, v, atom)) {
                        continue;
                } else if (!strcmp(name, "HEAD")) {
                        const char *head;
-                       unsigned char sha1[20];
+                       struct object_id oid;
 
                        head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
-                                                 sha1, NULL);
+                                                 oid.hash, NULL);
                        if (head && !strcmp(ref->refname, head))
                                v->s = "*";
                        else
@@ -1415,13 +1415,13 @@ static void populate_value(struct ref_array_item *ref)
        return;
 
  need_obj:
-       buf = get_obj(ref->objectname, &obj, &size, &eaten);
+       buf = get_obj(&ref->objectname, &obj, &size, &eaten);
        if (!buf)
                die(_("missing object %s for %s"),
-                   sha1_to_hex(ref->objectname), ref->refname);
+                   oid_to_hex(&ref->objectname), ref->refname);
        if (!obj)
                die(_("parse_object_buffer failed on %s for %s"),
-                   sha1_to_hex(ref->objectname), ref->refname);
+                   oid_to_hex(&ref->objectname), ref->refname);
 
        grab_values(ref->value, 0, obj, buf, size);
        if (!eaten)
@@ -1438,7 +1438,7 @@ static void populate_value(struct ref_array_item *ref)
         * If it is a tag object, see if we use a value that derefs
         * the object, and if we do grab the object it refers to.
         */
-       tagged = ((struct tag *)obj)->tagged->oid.hash;
+       tagged = &((struct tag *)obj)->tagged->oid;
 
        /*
         * NEEDSWORK: This derefs tag only once, which
@@ -1449,10 +1449,10 @@ static void populate_value(struct ref_array_item *ref)
        buf = get_obj(tagged, &obj, &size, &eaten);
        if (!buf)
                die(_("missing object %s for %s"),
-                   sha1_to_hex(tagged), ref->refname);
+                   oid_to_hex(tagged), ref->refname);
        if (!obj)
                die(_("parse_object_buffer failed on %s for %s"),
-                   sha1_to_hex(tagged), ref->refname);
+                   oid_to_hex(tagged), ref->refname);
        grab_values(ref->value, 1, obj, buf, size);
        if (!eaten)
                free(buf);
@@ -1487,6 +1487,7 @@ struct ref_filter_cbdata {
        struct ref_array *array;
        struct ref_filter *filter;
        struct contains_cache contains_cache;
+       struct contains_cache no_contains_cache;
 };
 
 /*
@@ -1586,11 +1587,11 @@ static enum contains_result contains_tag_algo(struct commit *candidate,
 }
 
 static int commit_contains(struct ref_filter *filter, struct commit *commit,
-                          struct contains_cache *cache)
+                          struct commit_list *list, struct contains_cache *cache)
 {
        if (filter->with_commit_tag_algo)
-               return contains_tag_algo(commit, filter->with_commit, cache) == CONTAINS_YES;
-       return is_descendant_of(commit, filter->with_commit);
+               return contains_tag_algo(commit, list, cache) == CONTAINS_YES;
+       return is_descendant_of(commit, list);
 }
 
 /*
@@ -1677,22 +1678,22 @@ static int filter_pattern_match(struct ref_filter *filter, const char *refname)
  * the need to parse the object via parse_object(). peel_ref() might be a
  * more efficient alternative to obtain the pointee.
  */
-static const unsigned char *match_points_at(struct sha1_array *points_at,
-                                           const unsigned char *sha1,
-                                           const char *refname)
+static const struct object_id *match_points_at(struct oid_array *points_at,
+                                              const struct object_id *oid,
+                                              const char *refname)
 {
-       const unsigned char *tagged_sha1 = NULL;
+       const struct object_id *tagged_oid = NULL;
        struct object *obj;
 
-       if (sha1_array_lookup(points_at, sha1) >= 0)
-               return sha1;
-       obj = parse_object(sha1);
+       if (oid_array_lookup(points_at, oid) >= 0)
+               return oid;
+       obj = parse_object(oid->hash);
        if (!obj)
                die(_("malformed object at '%s'"), refname);
        if (obj->type == OBJ_TAG)
-               tagged_sha1 = ((struct tag *)obj)->tagged->oid.hash;
-       if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
-               return tagged_sha1;
+               tagged_oid = &((struct tag *)obj)->tagged->oid;
+       if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
+               return tagged_oid;
        return NULL;
 }
 
@@ -1703,7 +1704,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
 {
        struct ref_array_item *ref;
        FLEX_ALLOC_STR(ref, refname, refname);
-       hashcpy(ref->objectname, objectname);
+       hashcpy(ref->objectname.hash, objectname);
        ref->flag = flag;
 
        return ref;
@@ -1772,7 +1773,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
        if (!filter_pattern_match(filter, refname))
                return 0;
 
-       if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
+       if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
                return 0;
 
        /*
@@ -1780,13 +1781,17 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
         * obtain the commit using the 'oid' available and discard all
         * non-commits early. The actual filtering is done later.
         */
-       if (filter->merge_commit || filter->with_commit || filter->verbose) {
-               commit = lookup_commit_reference_gently(oid->hash, 1);
+       if (filter->merge_commit || filter->with_commit || filter->no_commit || filter->verbose) {
+               commit = lookup_commit_reference_gently(oid, 1);
                if (!commit)
                        return 0;
-               /* We perform the filtering for the '--contains' option */
+               /* We perform the filtering for the '--contains' option... */
                if (filter->with_commit &&
-                   !commit_contains(filter, commit, &ref_cbdata->contains_cache))
+                   !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
+                       return 0;
+               /* ...or for the `--no-contains' option */
+               if (filter->no_commit &&
+                   commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
                        return 0;
        }
 
@@ -1887,6 +1892,7 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
        filter->kind = type & FILTER_REFS_KIND_MASK;
 
        init_contains_cache(&ref_cbdata.contains_cache);
+       init_contains_cache(&ref_cbdata.no_contains_cache);
 
        /*  Simple per-ref filtering */
        if (!filter->kind)
@@ -1911,6 +1917,7 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
        }
 
        clear_contains_cache(&ref_cbdata.contains_cache);
+       clear_contains_cache(&ref_cbdata.no_contains_cache);
 
        /*  Filters that need revision walking */
        if (filter->merge_commit)
@@ -2083,16 +2090,25 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 {
        struct ref_filter *rf = opt->value;
-       unsigned char sha1[20];
+       struct object_id oid;
+       int no_merged = starts_with(opt->long_name, "no");
+
+       if (rf->merge) {
+               if (no_merged) {
+                       return opterror(opt, "is incompatible with --merged", 0);
+               } else {
+                       return opterror(opt, "is incompatible with --no-merged", 0);
+               }
+       }
 
-       rf->merge = starts_with(opt->long_name, "no")
+       rf->merge = no_merged
                ? REF_FILTER_MERGED_OMIT
                : REF_FILTER_MERGED_INCLUDE;
 
-       if (get_sha1(arg, sha1))
+       if (get_oid(arg, &oid))
                die(_("malformed object name %s"), arg);
 
-       rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
+       rf->merge_commit = lookup_commit_reference_gently(&oid, 0);
        if (!rf->merge_commit)
                return opterror(opt, "must point to a commit", 0);