Make builtin-for-each-ref.c use parse-opts.
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Pierre Habouzit committed
Oct 13, 2007 at 20:40 UTC
c3428da87f625d22b7f0ea1e1fb7028264943da3
2 files changed
+61
-77
builtin-for-each-ref.c
+61
-77
@@ -7,6 +7,7 @@
7
#include "tree.h"
8
#include "blob.h"
9
#include "quote.h"
10
+#include "parse-options.h"
11
12
/* Quoting styles */
13
#define QUOTE_NONE 0
@@ -158,17 +159,18 @@ static const char *find_next(const char *cp)
159
* Make sure the format string is well formed, and parse out
160
* the used atoms.
161
*/
161
-static void verify_format(const char *format)
162
+static int verify_format(const char *format)
163
{
164
const char *cp, *sp;
165
for (cp = format; *cp && (sp = find_next(cp)); ) {
166
const char *ep = strchr(sp, ')');
167
if (!ep)
167
- die("malformatted format string %s", sp);
168
+ return error("malformatted format string %s", sp);
169
/* sp points at "%(" and ep points at the closing ")" */
170
parse_atom(sp + 2, ep);
171
cp = ep + 1;
172
}
173
+ return 0;
174
}
175
176
/*
@@ -800,94 +802,76 @@ static struct ref_sort *default_sort(void)
802
return sort;
803
}
804
803
-int cmd_for_each_ref(int ac, const char **av, const char *prefix)
805
+int opt_parse_sort(const struct option *opt, const char *arg, int unset)
806
+{
807
+ struct ref_sort **sort_tail = opt->value;
808
+ struct ref_sort *s;
809
+ int len;
810
+
811
+ if (!arg) /* should --no-sort void the list ? */
812
+ return -1;
813
+
814
+ *sort_tail = s = xcalloc(1, sizeof(*s));
815
+ sort_tail = &s->next;
816
+
817
+ if (*arg == '-') {
818
+ s->reverse = 1;
819
+ arg++;
820
+ }
821
+ len = strlen(arg);
822
+ s->atom = parse_atom(arg, arg+len);
823
+ return 0;
824
+}
825
+
826
+static char const * const for_each_ref_usage[] = {
827
+ "git-for-each-ref [options] [<pattern>]",
828
+ NULL
829
+};
830
+
831
+int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
832
{
833
int i, num_refs;
806
- const char *format = NULL;
834
+ const char *format = "%(objectname) %(objecttype)\t%(refname)";
835
struct ref_sort *sort = NULL, **sort_tail = &sort;
808
- int maxcount = 0;
809
- int quote_style = -1; /* unspecified yet */
836
+ int maxcount = 0, quote_style;
837
+ int quote_shell = 0, quote_perl = 0, quote_python = 0, quote_tcl = 0;
838
struct refinfo **refs;
839
struct grab_ref_cbdata cbdata;
840
813
- for (i = 1; i < ac; i++) {
814
- const char *arg = av[i];
815
- if (arg[0] != '-')
816
- break;
817
- if (!strcmp(arg, "--")) {
818
- i++;
819
- break;
820
- }
821
- if (!prefixcmp(arg, "--format=")) {
822
- if (format)
823
- die("more than one --format?");
824
- format = arg + 9;
825
- continue;
826
- }
827
- if (!strcmp(arg, "-s") || !strcmp(arg, "--shell") ) {
828
- if (0 <= quote_style)
829
- die("more than one quoting style?");
830
- quote_style = QUOTE_SHELL;
831
- continue;
832
- }
833
- if (!strcmp(arg, "-p") || !strcmp(arg, "--perl") ) {
834
- if (0 <= quote_style)
835
- die("more than one quoting style?");
836
- quote_style = QUOTE_PERL;
837
- continue;
838
- }
839
- if (!strcmp(arg, "--python") ) {
840
- if (0 <= quote_style)
841
- die("more than one quoting style?");
842
- quote_style = QUOTE_PYTHON;
843
- continue;
844
- }
845
- if (!strcmp(arg, "--tcl") ) {
846
- if (0 <= quote_style)
847
- die("more than one quoting style?");
848
- quote_style = QUOTE_TCL;
849
- continue;
850
- }
851
- if (!prefixcmp(arg, "--count=")) {
852
- if (maxcount)
853
- die("more than one --count?");
854
- maxcount = atoi(arg + 8);
855
- if (maxcount <= 0)
856
- die("The number %s did not parse", arg);
857
- continue;
858
- }
859
- if (!prefixcmp(arg, "--sort=")) {
860
- struct ref_sort *s = xcalloc(1, sizeof(*s));
861
- int len;
862
-
863
- s->next = NULL;
864
- *sort_tail = s;
865
- sort_tail = &s->next;
866
-
867
- arg += 7;
868
- if (*arg == '-') {
869
- s->reverse = 1;
870
- arg++;
871
- }
872
- len = strlen(arg);
873
- sort->atom = parse_atom(arg, arg+len);
874
- continue;
875
- }
876
- break;
841
+ struct option opts[] = {
842
+ OPT_BOOLEAN('s', "shell", "e_shell, "quote placeholders suitably for shells"),
843
+ OPT_BOOLEAN('p', "perl", "e_perl, "quote placeholders suitably for perl"),
844
+ OPT_BOOLEAN( 0 , "python", "e_python, "quote placeholders suitably for python"),
845
+ OPT_BOOLEAN( 0 , "tcl", "e_tcl, "quote placeholders suitably for tcl"),
846
+
847
+ OPT_GROUP(""),
848
+ OPT_INTEGER( 0 , "count", &maxcount, "show only <n> matched refs"),
849
+ OPT_STRING( 0 , "format", &format, "format", "format to use for the output"),
850
+ OPT_CALLBACK(0 , "sort", &sort_tail, "key",
851
+ "field name to sort on", &opt_parse_sort),
852
+ OPT_END(),
853
+ };
854
+
855
+ parse_options(argc, argv, opts, for_each_ref_usage, 0);
856
+ if (maxcount < 0) {
857
+ error("invalid --count argument: `%d'", maxcount);
858
+ usage_with_options(for_each_ref_usage, opts);
859
}
878
- if (quote_style < 0)
879
- quote_style = QUOTE_NONE;
860
+ if (quote_shell + quote_perl + quote_python + quote_tcl > 1) {
861
+ error("more than one quoting style ?");
862
+ usage_with_options(for_each_ref_usage, opts);
863
+ }
864
+ if (verify_format(format))
865
+ usage_with_options(for_each_ref_usage, opts);
866
867
+ quote_style = QUOTE_SHELL * quote_shell + QUOTE_PERL * quote_perl +
868
+ QUOTE_PYTHON * quote_python + QUOTE_TCL * quote_tcl;
869
if (!sort)
870
sort = default_sort();
871
sort_atom_limit = used_atom_cnt;
884
- if (!format)
885
- format = "%(objectname) %(objecttype)\t%(refname)";
886
-
887
- verify_format(format);
872
873
memset(&cbdata, 0, sizeof(cbdata));
890
- cbdata.grab_pattern = av + i;
874
+ cbdata.grab_pattern = argv;
875
for_each_ref(grab_single_ref, &cbdata);
876
refs = cbdata.grab_array;
877
num_refs = cbdata.grab_cnt;