ls-remote: use parse-options api
[git/git.git] / builtin / ls-remote.c
CommitLineData
18f7c51c 1#include "builtin.h"
18705953 2#include "cache.h"
18f7c51c
DB
3#include "transport.h"
4#include "remote.h"
18705953 5
ba5f28bf
TG
6static 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};
18705953 11
2ea7fe0d 12/*
3d3c4f5d
JH
13 * Is there one among the list of patterns that match the tail part
14 * of the path?
2ea7fe0d 15 */
2ea7fe0d
JH
16static int tail_match(const char **pattern, const char *path)
17{
2ea7fe0d 18 const char *p;
3d3c4f5d 19 char pathbuf[PATH_MAX];
2ea7fe0d 20
3d3c4f5d 21 if (!pattern)
2ea7fe0d
JH
22 return 1; /* no restriction */
23
3d3c4f5d
JH
24 if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
25 return error("insanely long ref %.*s...", 20, path);
26 while ((p = *(pattern++)) != NULL) {
eb07894f 27 if (!wildmatch(p, pathbuf, 0, NULL))
3d3c4f5d 28 return 1;
2ea7fe0d
JH
29 }
30 return 0;
31}
32
8951d7c1 33int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 34{
18f7c51c 35 const char *dest = NULL;
2718ff09 36 unsigned flags = 0;
45781adb 37 int get_url = 0;
cefb2a5e 38 int quiet = 0;
a8724773 39 int status = 0;
18f7c51c 40 const char *uploadpack = NULL;
2ea7fe0d 41 const char **pattern = NULL;
18f7c51c 42
7c2c6ee7 43 struct remote *remote;
18f7c51c
DB
44 struct transport *transport;
45 const struct ref *ref;
e44eb3e4 46
ba5f28bf
TG
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 };
91a640ff 63
ba5f28bf
TG
64 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
65 PARSE_OPT_STOP_AT_NON_OPTION);
66 dest = argv[0];
18705953 67
ba5f28bf
TG
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]);
18705953 73 }
2718ff09 74
c1d45cf7 75 remote = remote_get(dest);
9c00de5a
TRC
76 if (!remote) {
77 if (dest)
78 die("bad repository '%s'", dest);
79 die("No remote configured to list refs from.");
80 }
c1d45cf7 81 if (!remote->url_nr)
7c2c6ee7 82 die("remote %s has no configured URL", dest);
45781adb
UKK
83
84 if (get_url) {
85 printf("%s\n", *remote->url);
86 return 0;
87 }
88
fb0cc87e 89 transport = transport_get(remote, NULL);
18f7c51c
DB
90 if (uploadpack != NULL)
91 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
92
93 ref = transport_get_remote_refs(transport);
27b4070e 94 if (transport_disconnect(transport))
18f7c51c 95 return 1;
cefb2a5e
TRC
96
97 if (!dest && !quiet)
98 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
99 for ( ; ref; ref = ref->next) {
100 if (!check_ref_type(ref, flags))
101 continue;
102 if (!tail_match(pattern, ref->name))
103 continue;
f4e54d02 104 printf("%s %s\n", oid_to_hex(&ref->old_oid), ref->name);
a8724773 105 status = 0; /* we found something */
18f7c51c 106 }
a8724773 107 return status;
18705953 108}