blob: 57489e4eab1c16d94806e82471371b9be574d9ef [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Junio C Hamano9f613dd2006-09-15 13:30:02 -07002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamano9f613dd2006-09-15 13:30:02 -07004#include "refs.h"
5#include "object.h"
Pierre Habouzitc3428da2007-10-13 20:40:46 +02006#include "parse-options.h"
Karthik Nayak69b1cf92015-06-14 01:07:26 +05307#include "ref-filter.h"
Junio C Hamano9f613dd2006-09-15 13:30:02 -07008
Pierre Habouzitc3428da2007-10-13 20:40:46 +02009static char const * const for_each_ref_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070010 N_("git for-each-ref [<options>] [<pattern>]"),
Karthik Nayakd3254062015-07-07 21:36:10 +053011 N_("git for-each-ref [--points-at <object>]"),
Ævar Arnfjörð Bjarmason7ac04f12017-03-23 13:05:21 +000012 N_("git for-each-ref [(--merged | --no-merged) [<commit>]]"),
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +000013 N_("git for-each-ref [--contains [<commit>]] [--no-contains [<commit>]]"),
Pierre Habouzitc3428da2007-10-13 20:40:46 +020014 NULL
15};
16
17int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
Junio C Hamano9f613dd2006-09-15 13:30:02 -070018{
Karthik Nayak215b5652015-06-14 01:07:22 +053019 int i;
Karthik Nayak73079d22015-06-14 01:07:25 +053020 struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
Jeff King4a68e362017-07-13 11:01:18 -040021 int maxcount = 0, icase = 0;
Karthik Nayak14de7fb2015-06-14 01:07:28 +053022 struct ref_array array;
23 struct ref_filter filter;
Jeff King4a68e362017-07-13 11:01:18 -040024 struct ref_format format = REF_FORMAT_INIT;
Junio C Hamano9f613dd2006-09-15 13:30:02 -070025
Pierre Habouzitc3428da2007-10-13 20:40:46 +020026 struct option opts[] = {
Jeff King4a68e362017-07-13 11:01:18 -040027 OPT_BIT('s', "shell", &format.quote_style,
Nguyễn Thái Ngọc Duy18913522012-08-20 19:32:11 +070028 N_("quote placeholders suitably for shells"), QUOTE_SHELL),
Jeff King4a68e362017-07-13 11:01:18 -040029 OPT_BIT('p', "perl", &format.quote_style,
Nguyễn Thái Ngọc Duy18913522012-08-20 19:32:11 +070030 N_("quote placeholders suitably for perl"), QUOTE_PERL),
Jeff King4a68e362017-07-13 11:01:18 -040031 OPT_BIT(0 , "python", &format.quote_style,
Nguyễn Thái Ngọc Duy18913522012-08-20 19:32:11 +070032 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
Jeff King4a68e362017-07-13 11:01:18 -040033 OPT_BIT(0 , "tcl", &format.quote_style,
Ralf Thielowb799a692014-11-28 19:00:11 +010034 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
Junio C Hamano9f613dd2006-09-15 13:30:02 -070035
Pierre Habouzitc3428da2007-10-13 20:40:46 +020036 OPT_GROUP(""),
Nguyễn Thái Ngọc Duy18913522012-08-20 19:32:11 +070037 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
Jeff King4a68e362017-07-13 11:01:18 -040038 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
Jeff King0c88bf52017-10-03 09:45:47 -040039 OPT__COLOR(&format.use_color, N_("respect format colors")),
Jeff King95be7172019-03-20 16:22:15 -040040 OPT_REF_SORT(sorting_tail),
Karthik Nayakd3254062015-07-07 21:36:10 +053041 OPT_CALLBACK(0, "points-at", &filter.points_at,
42 N_("object"), N_("print only refs which points at the given object"),
43 parse_opt_object_name),
Karthik Nayak7c328342015-07-07 21:36:13 +053044 OPT_MERGED(&filter, N_("print only refs that are merged")),
45 OPT_NO_MERGED(&filter, N_("print only refs that are not merged")),
Karthik Nayak4a711092015-07-07 21:36:17 +053046 OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +000047 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070048 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
Pierre Habouzitc3428da2007-10-13 20:40:46 +020049 OPT_END(),
50 };
Junio C Hamano9f613dd2006-09-15 13:30:02 -070051
Karthik Nayakd3254062015-07-07 21:36:10 +053052 memset(&array, 0, sizeof(array));
53 memset(&filter, 0, sizeof(filter));
54
Jeff King4a68e362017-07-13 11:01:18 -040055 format.format = "%(objectname) %(objecttype)\t%(refname)";
56
Jeff Kingd8b68682017-07-13 11:07:14 -040057 git_config(git_default_config, NULL);
58
Stephen Boyd37782922009-05-23 11:53:12 -070059 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
Pierre Habouzitc3428da2007-10-13 20:40:46 +020060 if (maxcount < 0) {
61 error("invalid --count argument: `%d'", maxcount);
62 usage_with_options(for_each_ref_usage, opts);
Junio C Hamano9f613dd2006-09-15 13:30:02 -070063 }
Jeff King4a68e362017-07-13 11:01:18 -040064 if (HAS_MULTI_BITS(format.quote_style)) {
Johannes Sixtc9ecf4f2007-12-06 13:24:39 +010065 error("more than one quoting style?");
Pierre Habouzitc3428da2007-10-13 20:40:46 +020066 usage_with_options(for_each_ref_usage, opts);
67 }
Jeff King4a68e362017-07-13 11:01:18 -040068 if (verify_ref_format(&format))
Pierre Habouzitc3428da2007-10-13 20:40:46 +020069 usage_with_options(for_each_ref_usage, opts);
Junio C Hamano9f613dd2006-09-15 13:30:02 -070070
Karthik Nayak73079d22015-06-14 01:07:25 +053071 if (!sorting)
72 sorting = ref_default_sorting();
Jeff King76f9e562020-05-03 05:11:57 -040073 ref_sorting_icase_all(sorting, icase);
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +070074 filter.ignore_case = icase;
Junio C Hamano9f613dd2006-09-15 13:30:02 -070075
Karthik Nayak14de7fb2015-06-14 01:07:28 +053076 filter.name_patterns = argv;
Karthik Nayakbef0e122015-09-10 21:18:26 +053077 filter.match_as_path = 1;
Karthik Nayak14de7fb2015-06-14 01:07:28 +053078 filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
79 ref_array_sort(sorting, &array);
Junio C Hamano9f613dd2006-09-15 13:30:02 -070080
Karthik Nayak14de7fb2015-06-14 01:07:28 +053081 if (!maxcount || array.nr < maxcount)
82 maxcount = array.nr;
Junio C Hamano9f613dd2006-09-15 13:30:02 -070083 for (i = 0; i < maxcount; i++)
Jeff King4a68e362017-07-13 11:01:18 -040084 show_ref_array_item(array.items[i], &format);
Karthik Nayak14de7fb2015-06-14 01:07:28 +053085 ref_array_clear(&array);
Junio C Hamano9f613dd2006-09-15 13:30:02 -070086 return 0;
87}