Skip to content

Commit ba5f28b

Browse files
tgummerergitster
authored andcommitted
ls-remote: use parse-options api
Currently ls-remote uses a hand rolled parser for its command line arguments. Use the parse-options api instead of the hand rolled parser to simplify the code and make it easier to add new arguments. In addition this improves the help message. Helped-by: Jeff King <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 80b17e5 commit ba5f28b

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

builtin/ls-remote.c

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include "transport.h"
44
#include "remote.h"
55

6-
static const char ls_remote_usage[] =
7-
"git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8-
" [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
6+
static const char * const ls_remote_usage[] = {
7+
N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8+
" [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]"),
9+
NULL
10+
};
911

1012
/*
1113
* Is there one among the list of patterns that match the tail part
@@ -30,7 +32,6 @@ static int tail_match(const char **pattern, const char *path)
3032

3133
int cmd_ls_remote(int argc, const char **argv, const char *prefix)
3234
{
33-
int i;
3435
const char *dest = NULL;
3536
unsigned flags = 0;
3637
int get_url = 0;
@@ -43,59 +44,34 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
4344
struct transport *transport;
4445
const struct ref *ref;
4546

46-
if (argc == 2 && !strcmp("-h", argv[1]))
47-
usage(ls_remote_usage);
47+
struct option options[] = {
48+
OPT__QUIET(&quiet, N_("do not print remote URL")),
49+
OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
50+
N_("path of git-upload-pack on the remote host")),
51+
{ OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
52+
N_("path of git-upload-pack on the remote host"),
53+
PARSE_OPT_HIDDEN },
54+
OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
55+
OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
56+
OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
57+
OPT_BOOL(0, "get-url", &get_url,
58+
N_("take url.<base>.insteadOf into account")),
59+
OPT_SET_INT(0, "exit-code", &status,
60+
N_("exit with exit code 2 if no matching refs are found"), 2),
61+
OPT_END()
62+
};
4863

49-
for (i = 1; i < argc; i++) {
50-
const char *arg = argv[i];
64+
argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
65+
PARSE_OPT_STOP_AT_NON_OPTION);
66+
dest = argv[0];
5167

52-
if (*arg == '-') {
53-
if (starts_with(arg, "--upload-pack=")) {
54-
uploadpack = arg + 14;
55-
continue;
56-
}
57-
if (starts_with(arg, "--exec=")) {
58-
uploadpack = arg + 7;
59-
continue;
60-
}
61-
if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
62-
flags |= REF_TAGS;
63-
continue;
64-
}
65-
if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
66-
flags |= REF_HEADS;
67-
continue;
68-
}
69-
if (!strcmp("--refs", arg)) {
70-
flags |= REF_NORMAL;
71-
continue;
72-
}
73-
if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
74-
quiet = 1;
75-
continue;
76-
}
77-
if (!strcmp("--get-url", arg)) {
78-
get_url = 1;
79-
continue;
80-
}
81-
if (!strcmp("--exit-code", arg)) {
82-
/* return this code if no refs are reported */
83-
status = 2;
84-
continue;
85-
}
86-
usage(ls_remote_usage);
87-
}
88-
dest = arg;
89-
i++;
90-
break;
68+
if (argc > 1) {
69+
int i;
70+
pattern = xcalloc(argc, sizeof(const char *));
71+
for (i = 1; i < argc; i++)
72+
pattern[i - 1] = xstrfmt("*/%s", argv[i]);
9173
}
9274

93-
if (argv[i]) {
94-
int j;
95-
pattern = xcalloc(argc - i + 1, sizeof(const char *));
96-
for (j = i; j < argc; j++)
97-
pattern[j - i] = xstrfmt("*/%s", argv[j]);
98-
}
9975
remote = remote_get(dest);
10076
if (!remote) {
10177
if (dest)

0 commit comments

Comments
 (0)