ls-remote: send server options when using protocol v2
[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"
b4be7410 5#include "refs.h"
18705953 6
ba5f28bf
TG
7static const char * const ls_remote_usage[] = {
8 N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
99c08d4e
TG
9 " [-q | --quiet] [--exit-code] [--get-url]\n"
10 " [--symref] [<repository> [<refs>...]]"),
ba5f28bf
TG
11 NULL
12};
18705953 13
2ea7fe0d 14/*
3d3c4f5d
JH
15 * Is there one among the list of patterns that match the tail part
16 * of the path?
2ea7fe0d 17 */
2ea7fe0d
JH
18static int tail_match(const char **pattern, const char *path)
19{
2ea7fe0d 20 const char *p;
7f897b6f 21 char *pathbuf;
2ea7fe0d 22
3d3c4f5d 23 if (!pattern)
2ea7fe0d
JH
24 return 1; /* no restriction */
25
7f897b6f 26 pathbuf = xstrfmt("/%s", path);
3d3c4f5d 27 while ((p = *(pattern++)) != NULL) {
55d34269 28 if (!wildmatch(p, pathbuf, 0)) {
7f897b6f 29 free(pathbuf);
3d3c4f5d 30 return 1;
7f897b6f 31 }
2ea7fe0d 32 }
7f897b6f 33 free(pathbuf);
2ea7fe0d
JH
34 return 0;
35}
36
8951d7c1 37int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 38{
18f7c51c 39 const char *dest = NULL;
2718ff09 40 unsigned flags = 0;
45781adb 41 int get_url = 0;
cefb2a5e 42 int quiet = 0;
a8724773 43 int status = 0;
99c08d4e 44 int show_symref_target = 0;
18f7c51c 45 const char *uploadpack = NULL;
2ea7fe0d 46 const char **pattern = NULL;
b4be7410 47 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
ff473221 48 struct string_list server_options = STRING_LIST_INIT_DUP;
18f7c51c 49
7c2c6ee7 50 struct remote *remote;
18f7c51c
DB
51 struct transport *transport;
52 const struct ref *ref;
e44eb3e4 53
ba5f28bf
TG
54 struct option options[] = {
55 OPT__QUIET(&quiet, N_("do not print remote URL")),
56 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
57 N_("path of git-upload-pack on the remote host")),
58 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
59 N_("path of git-upload-pack on the remote host"),
60 PARSE_OPT_HIDDEN },
61 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
62 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
63 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
64 OPT_BOOL(0, "get-url", &get_url,
65 N_("take url.<base>.insteadOf into account")),
cdc71c1c
NTND
66 OPT_SET_INT_F(0, "exit-code", &status,
67 N_("exit with exit code 2 if no matching refs are found"),
68 2, PARSE_OPT_NOCOMPLETE),
99c08d4e
TG
69 OPT_BOOL(0, "symref", &show_symref_target,
70 N_("show underlying ref in addition to the object pointed by it")),
ff473221 71 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
ba5f28bf
TG
72 OPT_END()
73 };
91a640ff 74
ba5f28bf
TG
75 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
76 PARSE_OPT_STOP_AT_NON_OPTION);
77 dest = argv[0];
18705953 78
ba5f28bf
TG
79 if (argc > 1) {
80 int i;
81 pattern = xcalloc(argc, sizeof(const char *));
b4be7410
BW
82 for (i = 1; i < argc; i++) {
83 const char *glob;
ba5f28bf 84 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
b4be7410
BW
85
86 glob = strchr(argv[i], '*');
87 if (glob)
88 argv_array_pushf(&ref_prefixes, "%.*s",
89 (int)(glob - argv[i]), argv[i]);
90 else
91 expand_ref_prefix(&ref_prefixes, argv[i]);
92 }
18705953 93 }
2718ff09 94
c1d45cf7 95 remote = remote_get(dest);
9c00de5a
TRC
96 if (!remote) {
97 if (dest)
98 die("bad repository '%s'", dest);
99 die("No remote configured to list refs from.");
100 }
c1d45cf7 101 if (!remote->url_nr)
7c2c6ee7 102 die("remote %s has no configured URL", dest);
45781adb
UKK
103
104 if (get_url) {
105 printf("%s\n", *remote->url);
106 return 0;
107 }
108
fb0cc87e 109 transport = transport_get(remote, NULL);
18f7c51c
DB
110 if (uploadpack != NULL)
111 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
ff473221
BW
112 if (server_options.nr)
113 transport->server_options = &server_options;
18f7c51c 114
b4be7410 115 ref = transport_get_remote_refs(transport, &ref_prefixes);
27b4070e 116 if (transport_disconnect(transport))
18f7c51c 117 return 1;
cefb2a5e
TRC
118
119 if (!dest && !quiet)
120 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
121 for ( ; ref; ref = ref->next) {
122 if (!check_ref_type(ref, flags))
123 continue;
124 if (!tail_match(pattern, ref->name))
125 continue;
99c08d4e
TG
126 if (show_symref_target && ref->symref)
127 printf("ref: %s\t%s\n", ref->symref, ref->name);
128 printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
a8724773 129 status = 0; /* we found something */
18f7c51c 130 }
a8724773 131 return status;
18705953 132}