builtin / for-each-ref.con commit t7610: run 'git reset --hard' after each test to clean up (c3ad312)
   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        N_("git for-each-ref [(--merged | --no-merged) [<object>]]"),
  12        N_("git for-each-ref [--contains [<object>]]"),
  13        NULL
  14};
  15
  16int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
  17{
  18        int i;
  19        const char *format = "%(objectname) %(objecttype)\t%(refname)";
  20        struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
  21        int maxcount = 0, quote_style = 0, icase = 0;
  22        struct ref_array array;
  23        struct ref_filter filter;
  24
  25        struct option opts[] = {
  26                OPT_BIT('s', "shell", &quote_style,
  27                        N_("quote placeholders suitably for shells"), QUOTE_SHELL),
  28                OPT_BIT('p', "perl",  &quote_style,
  29                        N_("quote placeholders suitably for perl"), QUOTE_PERL),
  30                OPT_BIT(0 , "python", &quote_style,
  31                        N_("quote placeholders suitably for python"), QUOTE_PYTHON),
  32                OPT_BIT(0 , "tcl",  &quote_style,
  33                        N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
  34
  35                OPT_GROUP(""),
  36                OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
  37                OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
  38                OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
  39                            N_("field name to sort on"), &parse_opt_ref_sorting),
  40                OPT_CALLBACK(0, "points-at", &filter.points_at,
  41                             N_("object"), N_("print only refs which points at the given object"),
  42                             parse_opt_object_name),
  43                OPT_MERGED(&filter, N_("print only refs that are merged")),
  44                OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
  45                OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
  46                OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
  47                OPT_END(),
  48        };
  49
  50        memset(&array, 0, sizeof(array));
  51        memset(&filter, 0, sizeof(filter));
  52
  53        parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
  54        if (maxcount < 0) {
  55                error("invalid --count argument: `%d'", maxcount);
  56                usage_with_options(for_each_ref_usage, opts);
  57        }
  58        if (HAS_MULTI_BITS(quote_style)) {
  59                error("more than one quoting style?");
  60                usage_with_options(for_each_ref_usage, opts);
  61        }
  62        if (verify_ref_format(format))
  63                usage_with_options(for_each_ref_usage, opts);
  64
  65        if (!sorting)
  66                sorting = ref_default_sorting();
  67        sorting->ignore_case = icase;
  68        filter.ignore_case = icase;
  69
  70        /* for warn_ambiguous_refs */
  71        git_config(git_default_config, NULL);
  72
  73        filter.name_patterns = argv;
  74        filter.match_as_path = 1;
  75        filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
  76        ref_array_sort(sorting, &array);
  77
  78        if (!maxcount || array.nr < maxcount)
  79                maxcount = array.nr;
  80        for (i = 0; i < maxcount; i++)
  81                show_ref_array_item(array.items[i], format, quote_style);
  82        ref_array_clear(&array);
  83        return 0;
  84}