]> git.ipfire.org Git - thirdparty/git.git/blobdiff - ref-filter.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / ref-filter.c
index 7838bd22b8db1bd52859361a4f3b7789c2cf4b5b..57a5884aec787bdf12402b46c2b7bc77349d92a8 100644 (file)
@@ -1,9 +1,14 @@
-#include "builtin.h"
 #include "cache.h"
+#include "alloc.h"
+#include "environment.h"
+#include "gettext.h"
+#include "hex.h"
 #include "parse-options.h"
 #include "refs.h"
 #include "wildmatch.h"
+#include "object-name.h"
 #include "object-store.h"
+#include "oid-array.h"
 #include "repository.h"
 #include "commit.h"
 #include "remote.h"
@@ -13,7 +18,6 @@
 #include "ref-filter.h"
 #include "revision.h"
 #include "utf8.h"
-#include "git-compat-util.h"
 #include "version.h"
 #include "trailer.h"
 #include "wt-status.h"
@@ -89,7 +93,7 @@ struct ref_to_worktree_entry {
        struct worktree *wt; /* key is wt->head_ref */
 };
 
-static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata,
+static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED,
                                      const struct hashmap_entry *eptr,
                                      const struct hashmap_entry *kptr,
                                      const void *keydata_aka_refname)
@@ -228,6 +232,22 @@ static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
        return ret;
 }
 
+static int err_no_arg(struct strbuf *sb, const char *name)
+{
+       size_t namelen = strchrnul(name, ':') - name;
+       strbuf_addf(sb, _("%%(%.*s) does not take arguments"),
+                   (int)namelen, name);
+       return -1;
+}
+
+static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg)
+{
+       size_t namelen = strchrnul(name, ':') - name;
+       strbuf_addf(sb, _("unrecognized %%(%.*s) argument: %s"),
+                   (int)namelen, name, arg);
+       return -1;
+}
+
 static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
                             const char *color_value, struct strbuf *err)
 {
@@ -262,11 +282,12 @@ static int refname_atom_parser_internal(struct refname_atom *atom, const char *a
                if (strtol_i(arg, 10, &atom->rstrip))
                        return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
        } else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
+               return err_bad_arg(err, name, arg);
        return 0;
 }
 
-static int remote_ref_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int remote_ref_atom_parser(struct ref_format *format UNUSED,
+                                 struct used_atom *atom,
                                  const char *arg, struct strbuf *err)
 {
        struct string_list params = STRING_LIST_INIT_DUP;
@@ -313,11 +334,12 @@ static int remote_ref_atom_parser(struct ref_format *format, struct used_atom *a
        return 0;
 }
 
-static int objecttype_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int objecttype_atom_parser(struct ref_format *format UNUSED,
+                                 struct used_atom *atom,
                                  const char *arg, struct strbuf *err)
 {
        if (arg)
-               return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
+               return err_no_arg(err, "objecttype");
        if (*atom->name == '*')
                oi_deref.info.typep = &oi_deref.type;
        else
@@ -325,7 +347,8 @@ static int objecttype_atom_parser(struct ref_format *format, struct used_atom *a
        return 0;
 }
 
-static int objectsize_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int objectsize_atom_parser(struct ref_format *format UNUSED,
+                                 struct used_atom *atom,
                                  const char *arg, struct strbuf *err)
 {
        if (!arg) {
@@ -341,15 +364,16 @@ static int objectsize_atom_parser(struct ref_format *format, struct used_atom *a
                else
                        oi.info.disk_sizep = &oi.disk_size;
        } else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "objectsize", arg);
+               return err_bad_arg(err, "objectsize", arg);
        return 0;
 }
 
-static int deltabase_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int deltabase_atom_parser(struct ref_format *format UNUSED,
+                                struct used_atom *atom,
                                 const char *arg, struct strbuf *err)
 {
        if (arg)
-               return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
+               return err_no_arg(err, "deltabase");
        if (*atom->name == '*')
                oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
        else
@@ -357,16 +381,18 @@ static int deltabase_atom_parser(struct ref_format *format, struct used_atom *at
        return 0;
 }
 
-static int body_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int body_atom_parser(struct ref_format *format UNUSED,
+                           struct used_atom *atom,
                            const char *arg, struct strbuf *err)
 {
        if (arg)
-               return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
+               return err_no_arg(err, "body");
        atom->u.contents.option = C_BODY_DEP;
        return 0;
 }
 
-static int subject_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int subject_atom_parser(struct ref_format *format UNUSED,
+                              struct used_atom *atom,
                               const char *arg, struct strbuf *err)
 {
        if (!arg)
@@ -374,11 +400,12 @@ static int subject_atom_parser(struct ref_format *format, struct used_atom *atom
        else if (!strcmp(arg, "sanitize"))
                atom->u.contents.option = C_SUB_SANITIZE;
        else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "subject", arg);
+               return err_bad_arg(err, "subject", arg);
        return 0;
 }
 
-static int trailers_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int trailers_atom_parser(struct ref_format *format UNUSED,
+                               struct used_atom *atom,
                                const char *arg, struct strbuf *err)
 {
        atom->u.contents.trailer_opts.no_divider = 1;
@@ -428,23 +455,25 @@ static int contents_atom_parser(struct ref_format *format, struct used_atom *ato
                if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
                        return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
        } else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "contents", arg);
+               return err_bad_arg(err, "contents", arg);
        return 0;
 }
 
-static int raw_atom_parser(struct ref_format *format, struct used_atom *atom,
-                               const char *arg, struct strbuf *err)
+static int raw_atom_parser(struct ref_format *format UNUSED,
+                          struct used_atom *atom,
+                          const char *arg, struct strbuf *err)
 {
        if (!arg)
                atom->u.raw_data.option = RAW_BARE;
        else if (!strcmp(arg, "size"))
                atom->u.raw_data.option = RAW_LENGTH;
        else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "raw", arg);
+               return err_bad_arg(err, "raw", arg);
        return 0;
 }
 
-static int oid_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int oid_atom_parser(struct ref_format *format UNUSED,
+                          struct used_atom *atom,
                           const char *arg, struct strbuf *err)
 {
        if (!arg)
@@ -459,11 +488,12 @@ static int oid_atom_parser(struct ref_format *format, struct used_atom *atom,
                if (atom->u.oid.length < MINIMUM_ABBREV)
                        atom->u.oid.length = MINIMUM_ABBREV;
        } else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), atom->name, arg);
+               return err_bad_arg(err, atom->name, arg);
        return 0;
 }
 
-static int person_email_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int person_email_atom_parser(struct ref_format *format UNUSED,
+                                   struct used_atom *atom,
                                    const char *arg, struct strbuf *err)
 {
        if (!arg)
@@ -473,11 +503,12 @@ static int person_email_atom_parser(struct ref_format *format, struct used_atom
        else if (!strcmp(arg, "localpart"))
                atom->u.email_option.option = EO_LOCALPART;
        else
-               return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
+               return err_bad_arg(err, atom->name, arg);
        return 0;
 }
 
-static int refname_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int refname_atom_parser(struct ref_format *format UNUSED,
+                              struct used_atom *atom,
                               const char *arg, struct strbuf *err)
 {
        return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
@@ -494,7 +525,8 @@ static align_type parse_align_position(const char *s)
        return -1;
 }
 
-static int align_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int align_atom_parser(struct ref_format *format UNUSED,
+                            struct used_atom *atom,
                             const char *arg, struct strbuf *err)
 {
        struct align *align = &atom->u.align;
@@ -546,7 +578,8 @@ static int align_atom_parser(struct ref_format *format, struct used_atom *atom,
        return 0;
 }
 
-static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int if_atom_parser(struct ref_format *format UNUSED,
+                         struct used_atom *atom,
                          const char *arg, struct strbuf *err)
 {
        if (!arg) {
@@ -557,22 +590,26 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
        } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
                atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
        } else
-               return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), "if", arg);
+               return err_bad_arg(err, "if", arg);
        return 0;
 }
 
-static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
+static int rest_atom_parser(struct ref_format *format,
+                           struct used_atom *atom UNUSED,
                            const char *arg, struct strbuf *err)
 {
        if (arg)
-               return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
+               return err_no_arg(err, "rest");
        format->use_rest = 1;
        return 0;
 }
 
-static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
-                           const char *arg, struct strbuf *unused_err)
+static int head_atom_parser(struct ref_format *format UNUSED,
+                           struct used_atom *atom,
+                           const char *arg, struct strbuf *err)
 {
+       if (arg)
+               return err_no_arg(err, "HEAD");
        atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
        return 0;
 }
@@ -773,7 +810,7 @@ static void quote_formatting(struct strbuf *s, const char *str, ssize_t len, int
 }
 
 static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
-                      struct strbuf *unused_err)
+                      struct strbuf *err UNUSED)
 {
        /*
         * Quote formatting is only done when the stack has a single
@@ -823,7 +860,7 @@ static void end_align_handler(struct ref_formatting_stack **stack)
 }
 
 static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
-                             struct strbuf *unused_err)
+                             struct strbuf *err UNUSED)
 {
        struct ref_formatting_stack *new_stack;
 
@@ -870,7 +907,7 @@ static void if_then_else_handler(struct ref_formatting_stack **stack)
 }
 
 static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
-                          struct strbuf *unused_err)
+                          struct strbuf *err UNUSED)
 {
        struct ref_formatting_stack *new_stack;
        struct if_then_else *if_then_else = xcalloc(1,
@@ -897,7 +934,8 @@ static int is_empty(struct strbuf *buf)
        return cur == end;
  }
 
-static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
+static int then_atom_handler(struct atom_value *atomv UNUSED,
+                            struct ref_formatting_state *state,
                             struct strbuf *err)
 {
        struct ref_formatting_stack *cur = state->stack;
@@ -934,7 +972,8 @@ static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
        return 0;
 }
 
-static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
+static int else_atom_handler(struct atom_value *atomv UNUSED,
+                            struct ref_formatting_state *state,
                             struct strbuf *err)
 {
        struct ref_formatting_stack *prev = state->stack;
@@ -955,7 +994,8 @@ static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_sta
        return 0;
 }
 
-static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
+static int end_atom_handler(struct atom_value *atomv UNUSED,
+                           struct ref_formatting_state *state,
                            struct strbuf *err)
 {
        struct ref_formatting_stack *current = state->stack;
@@ -1057,9 +1097,11 @@ static const char *do_grab_oid(const char *field, const struct object_id *oid,
        case O_FULL:
                return oid_to_hex(oid);
        case O_LENGTH:
-               return find_unique_abbrev(oid, atom->u.oid.length);
+               return repo_find_unique_abbrev(the_repository, oid,
+                                              atom->u.oid.length);
        case O_SHORT:
-               return find_unique_abbrev(oid, DEFAULT_ABBREV);
+               return repo_find_unique_abbrev(the_repository, oid,
+                                              DEFAULT_ABBREV);
        default:
                BUG("unknown %%(%s) option", field);
        }
@@ -1191,7 +1233,7 @@ static const char *copy_name(const char *buf)
 {
        const char *cp;
        for (cp = buf; *cp && *cp != '\n'; cp++) {
-               if (!strncmp(cp, " <", 2))
+               if (starts_with(cp, " <"))
                        return xmemdupz(buf, cp - buf);
        }
        return xstrdup("");
@@ -1261,7 +1303,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
         * ":" means no format is specified, and use the default.
         */
        formatp = strchr(atomname, ':');
-       if (formatp != NULL) {
+       if (formatp) {
                formatp++;
                parse_date_format(formatp, &date_mode);
        }
@@ -1358,6 +1400,7 @@ static void find_subpos(const char *buf,
 
        /* parse signature first; we might not even have a subject line */
        parse_signature(buf, end - buf, &payload, &signature);
+       strbuf_release(&payload);
 
        /* skip past header until we hit empty line */
        while (*buf && *buf != '\n') {
@@ -1375,12 +1418,12 @@ static void find_subpos(const char *buf,
        /* subject is first non-empty line */
        *sub = buf;
        /* subject goes to first empty line before signature begins */
-       if ((eol = strstr(*sub, "\n\n"))) {
+       if ((eol = strstr(*sub, "\n\n")) ||
+           (eol = strstr(*sub, "\r\n\r\n"))) {
                eol = eol < sigstart ? eol : sigstart;
-       /* check if message uses CRLF */
-       } else if (! (eol = strstr(*sub, "\r\n\r\n"))) {
+       } else {
                /* treat whole message as subject */
-               eol = strrchr(*sub, '\0');
+               eol = sigstart;
        }
        buf = eol;
        *sublen = buf - *sub;
@@ -1509,7 +1552,7 @@ static void fill_missing_values(struct atom_value *val)
        int i;
        for (i = 0; i < used_atom_cnt; i++) {
                struct atom_value *v = &val[i];
-               if (v->s == NULL)
+               if (!v->s)
                        v->s = xstrdup("");
        }
 }
@@ -1619,7 +1662,7 @@ static const char *rstrip_ref_components(const char *refname, int len)
 
        while (remaining-- > 0) {
                char *p = strrchr(start, '/');
-               if (p == NULL) {
+               if (!p) {
                        free((char *)to_free);
                        return xstrdup("");
                } else
@@ -1722,6 +1765,8 @@ char *get_head_description(void)
        } else
                strbuf_addstr(&desc, _("(no branch)"));
 
+       wt_status_state_free_buffers(&state);
+
        return strbuf_detach(&desc, NULL);
 }
 
@@ -1801,7 +1846,7 @@ static void lazy_init_worktree_map(void)
        populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
 }
 
-static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
+static char *get_worktree_path(const struct ref_array_item *ref)
 {
        struct hashmap_entry entry, *e;
        struct ref_to_worktree_entry *lookup_result;
@@ -1860,7 +1905,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
                        refname = get_refname(atom, ref);
                else if (atom_type == ATOM_WORKTREEPATH) {
                        if (ref->kind == FILTER_REFS_BRANCHES)
-                               v->s = get_worktree_path(atom, ref);
+                               v->s = get_worktree_path(ref);
                        else
                                v->s = xstrdup("");
                        continue;
@@ -2126,8 +2171,9 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
                return for_each_fullref_in("", cb, cb_data);
        }
 
-       return for_each_fullref_in_prefixes(NULL, filter->name_patterns,
-                                           cb, cb_data);
+       return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository),
+                                                NULL, filter->name_patterns,
+                                                cb, cb_data);
 }
 
 /*
@@ -2392,6 +2438,7 @@ static void reach_filter(struct ref_array *array,
                clear_commit_marks(merge_commit, ALL_REV_FLAGS);
        }
 
+       release_revisions(&revs);
        free(to_clear);
 }
 
@@ -2404,6 +2451,7 @@ static void reach_filter(struct ref_array *array,
 int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
 {
        struct ref_filter_cbdata ref_cbdata;
+       int save_commit_buffer_orig;
        int ret = 0;
 
        ref_cbdata.array = array;
@@ -2411,6 +2459,9 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 
        filter->kind = type & FILTER_REFS_KIND_MASK;
 
+       save_commit_buffer_orig = save_commit_buffer;
+       save_commit_buffer = 0;
+
        init_contains_cache(&ref_cbdata.contains_cache);
        init_contains_cache(&ref_cbdata.no_contains_cache);
 
@@ -2443,6 +2494,7 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
        reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
        reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
 
+       save_commit_buffer = save_commit_buffer_orig;
        return ret;
 }
 
@@ -2737,7 +2789,7 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
 
        BUG_ON_OPT_NEG(unset);
 
-       if (get_oid(arg, &oid))
+       if (repo_get_oid(the_repository, arg, &oid))
                die(_("malformed object name %s"), arg);
 
        merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);