| 1 | #include "builtin.h" |
| 2 | #include "commit.h" |
| 3 | #include "config.h" |
| 4 | #include "environment.h" |
| 5 | #include "for-each-ref.h" |
| 6 | #include "gettext.h" |
| 7 | #include "object.h" |
| 8 | #include "parse-options.h" |
| 9 | #include "ref-filter.h" |
| 10 | #include "strbuf.h" |
| 11 | #include "strvec.h" |
| 12 | |
| 13 | int for_each_ref_core(int argc, const char **argv, const char *prefix, struct repository *repo, const char *const *usage) |
| 14 | { |
| 15 | struct ref_sorting *sorting; |
| 16 | struct string_list sorting_options = STRING_LIST_INIT_DUP; |
| 17 | int icase = 0, include_root_refs = 0, from_stdin = 0; |
| 18 | struct ref_filter filter = REF_FILTER_INIT; |
| 19 | struct ref_format format = REF_FORMAT_INIT; |
| 20 | unsigned int flags = FILTER_REFS_REGULAR; |
| 21 | struct strvec vec = STRVEC_INIT; |
| 22 | |
| 23 | struct option opts[] = { |
| 24 | OPT_BIT('s', "shell", &format.quote_style, |
| 25 | N_("quote placeholders suitably for shells"), QUOTE_SHELL), |
| 26 | OPT_BIT('p', "perl", &format.quote_style, |
| 27 | N_("quote placeholders suitably for perl"), QUOTE_PERL), |
| 28 | OPT_BIT(0 , "python", &format.quote_style, |
| 29 | N_("quote placeholders suitably for python"), QUOTE_PYTHON), |
| 30 | OPT_BIT(0 , "tcl", &format.quote_style, |
| 31 | N_("quote placeholders suitably for Tcl"), QUOTE_TCL), |
| 32 | OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty, |
| 33 | N_("do not output a newline after empty formatted refs")), |
| 34 | |
| 35 | OPT_GROUP(""), |
| 36 | OPT_INTEGER( 0 , "count", &format.array_opts.max_count, N_("show only <n> matched refs")), |
| 37 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), |
| 38 | OPT_STRING( 0 , "start-after", &filter.start_after, N_("marker"), N_("start iteration after the provided marker")), |
| 39 | OPT__COLOR(&format.use_color, N_("respect format colors")), |
| 40 | OPT_REF_FILTER_EXCLUDE(&filter), |
| 41 | OPT_REF_SORT(&sorting_options), |
| 42 | OPT_CALLBACK(0, "points-at", &filter.points_at, |
| 43 | N_("object"), N_("print only refs which points at the given object"), |
| 44 | parse_opt_object_name), |
| 45 | OPT_MERGED(&filter, N_("print only refs that are merged")), |
| 46 | OPT_NO_MERGED(&filter, N_("print only refs that are not merged")), |
| 47 | OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")), |
| 48 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), |
| 49 | OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
| 50 | OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")), |
| 51 | OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")), |
| 52 | OPT_END(), |
| 53 | }; |
| 54 | |
| 55 | format.format = "%(objectname) %(objecttype)\t%(refname)"; |
| 56 | |
| 57 | repo_config(repo, git_default_config, NULL); |
| 58 | |
| 59 | /* Set default (refname) sorting */ |
| 60 | string_list_append(&sorting_options, "refname"); |
| 61 | |
| 62 | parse_options(argc, argv, prefix, opts, usage, 0); |
| 63 | if (format.array_opts.max_count < 0) { |
| 64 | error("invalid --count argument: `%d'", format.array_opts.max_count); |
| 65 | usage_with_options(usage, opts); |
| 66 | } |
| 67 | if (HAS_MULTI_BITS(format.quote_style)) { |
| 68 | error("more than one quoting style?"); |
| 69 | usage_with_options(usage, opts); |
| 70 | } |
| 71 | if (verify_ref_format(&format)) |
| 72 | usage_with_options(usage, opts); |
| 73 | |
| 74 | if (filter.start_after && sorting_options.nr > 1) |
| 75 | die(_("cannot use --start-after with custom sort options")); |
| 76 | |
| 77 | sorting = ref_sorting_options(&sorting_options); |
| 78 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
| 79 | filter.ignore_case = icase; |
| 80 | |
| 81 | if (from_stdin) { |
| 82 | struct strbuf line = STRBUF_INIT; |
| 83 | |
| 84 | if (argv[0]) |
| 85 | die(_("unknown arguments supplied with --stdin")); |
| 86 | |
| 87 | while (strbuf_getline(&line, stdin) != EOF) |
| 88 | strvec_push(&vec, line.buf); |
| 89 | |
| 90 | strbuf_release(&line); |
| 91 | |
| 92 | /* vec.v is NULL-terminated, just like 'argv'. */ |
| 93 | filter.name_patterns = vec.v; |
| 94 | } else { |
| 95 | filter.name_patterns = argv; |
| 96 | } |
| 97 | |
| 98 | if (filter.start_after && filter.name_patterns && filter.name_patterns[0]) |
| 99 | die(_("cannot use --start-after with patterns")); |
| 100 | |
| 101 | if (include_root_refs) |
| 102 | flags |= FILTER_REFS_ROOT_REFS | FILTER_REFS_DETACHED_HEAD; |
| 103 | |
| 104 | filter.match_as_path = 1; |
| 105 | filter_and_format_refs(&filter, flags, sorting, &format); |
| 106 | |
| 107 | ref_filter_clear(&filter); |
| 108 | ref_sorting_release(sorting); |
| 109 | strvec_clear(&vec); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int cmd_for_each_ref(int argc, |
| 114 | const char **argv, |
| 115 | const char *prefix, |
| 116 | struct repository *repo) |
| 117 | { |
| 118 | static char const * const for_each_ref_usage[] = { |
| 119 | N_("git for-each-ref " COMMON_USAGE_FOR_EACH_REF), |
| 120 | NULL |
| 121 | }; |
| 122 | |
| 123 | return for_each_ref_core(argc, argv, prefix, repo, for_each_ref_usage); |
| 124 | } |