ae5419e0de74f9cfe38dc281dca79a06c3ed041b
   1#include "builtin.h"
   2#include "cache.h"
   3#include "refs.h"
   4#include "object.h"
   5#include "parse-options.h"
   6#include "ref-filter.h"
   7
   8static char const * const for_each_ref_usage[] = {
   9        N_("git for-each-ref [<options>] [<pattern>]"),
  10        N_("git for-each-ref [--points-at <object>]"),
  11        NULL
  12};
  13
  14int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
  15{
  16        int i;
  17        const char *format = "%(objectname) %(objecttype)\t%(refname)";
  18        struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
  19        int maxcount = 0, quote_style = 0;
  20        struct ref_array array;
  21        struct ref_filter filter;
  22
  23        struct option opts[] = {
  24                OPT_BIT('s', "shell", &quote_style,
  25                        N_("quote placeholders suitably for shells"), QUOTE_SHELL),
  26                OPT_BIT('p', "perl",  &quote_style,
  27                        N_("quote placeholders suitably for perl"), QUOTE_PERL),
  28                OPT_BIT(0 , "python", &quote_style,
  29                        N_("quote placeholders suitably for python"), QUOTE_PYTHON),
  30                OPT_BIT(0 , "tcl",  &quote_style,
  31                        N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
  32
  33                OPT_GROUP(""),
  34                OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
  35                OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
  36                OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
  37                            N_("field name to sort on"), &parse_opt_ref_sorting),
  38                OPT_CALLBACK(0, "points-at", &filter.points_at,
  39                             N_("object"), N_("print only refs which points at the given object"),
  40                             parse_opt_object_name),
  41                OPT_END(),
  42        };
  43
  44        memset(&array, 0, sizeof(array));
  45        memset(&filter, 0, sizeof(filter));
  46
  47        parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
  48        if (maxcount < 0) {
  49                error("invalid --count argument: `%d'", maxcount);
  50                usage_with_options(for_each_ref_usage, opts);
  51        }
  52        if (HAS_MULTI_BITS(quote_style)) {
  53                error("more than one quoting style?");
  54                usage_with_options(for_each_ref_usage, opts);
  55        }
  56        if (verify_ref_format(format))
  57                usage_with_options(for_each_ref_usage, opts);
  58
  59        if (!sorting)
  60                sorting = ref_default_sorting();
  61
  62        /* for warn_ambiguous_refs */
  63        git_config(git_default_config, NULL);
  64
  65        filter.name_patterns = argv;
  66        filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
  67        ref_array_sort(sorting, &array);
  68
  69        if (!maxcount || array.nr < maxcount)
  70                maxcount = array.nr;
  71        for (i = 0; i < maxcount; i++)
  72                show_ref_array_item(array.items[i], format, quote_style);
  73        ref_array_clear(&array);
  74        return 0;
  75}