]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/ls-remote.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / builtin / ls-remote.c
CommitLineData
18f7c51c 1#include "builtin.h"
18705953 2#include "cache.h"
f394e093 3#include "gettext.h"
41771fa4 4#include "hex.h"
18f7c51c 5#include "transport.h"
1fb20dfd 6#include "ref-filter.h"
18f7c51c 7#include "remote.h"
b4be7410 8#include "refs.h"
18705953 9
ba5f28bf
TG
10static const char * const ls_remote_usage[] = {
11 N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8c9e292d 12 " [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n"
d9ec3b0d 13 " [--symref] [<repository> [<patterns>...]]"),
ba5f28bf
TG
14 NULL
15};
18705953 16
2ea7fe0d 17/*
3d3c4f5d
JH
18 * Is there one among the list of patterns that match the tail part
19 * of the path?
2ea7fe0d 20 */
2ea7fe0d
JH
21static int tail_match(const char **pattern, const char *path)
22{
2ea7fe0d 23 const char *p;
7f897b6f 24 char *pathbuf;
2ea7fe0d 25
3d3c4f5d 26 if (!pattern)
2ea7fe0d
JH
27 return 1; /* no restriction */
28
7f897b6f 29 pathbuf = xstrfmt("/%s", path);
3d3c4f5d 30 while ((p = *(pattern++)) != NULL) {
55d34269 31 if (!wildmatch(p, pathbuf, 0)) {
7f897b6f 32 free(pathbuf);
3d3c4f5d 33 return 1;
7f897b6f 34 }
2ea7fe0d 35 }
7f897b6f 36 free(pathbuf);
2ea7fe0d
JH
37 return 0;
38}
39
8951d7c1 40int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 41{
18f7c51c 42 const char *dest = NULL;
2718ff09 43 unsigned flags = 0;
45781adb 44 int get_url = 0;
cefb2a5e 45 int quiet = 0;
a8724773 46 int status = 0;
99c08d4e 47 int show_symref_target = 0;
18f7c51c 48 const char *uploadpack = NULL;
2ea7fe0d 49 const char **pattern = NULL;
39835409
JT
50 struct transport_ls_refs_options transport_options =
51 TRANSPORT_LS_REFS_OPTIONS_INIT;
1fb20dfd 52 int i;
ff473221 53 struct string_list server_options = STRING_LIST_INIT_DUP;
18f7c51c 54
7c2c6ee7 55 struct remote *remote;
18f7c51c
DB
56 struct transport *transport;
57 const struct ref *ref;
1fb20dfd 58 struct ref_array ref_array;
98e7ab6d 59 struct string_list sorting_options = STRING_LIST_INIT_DUP;
e44eb3e4 60
ba5f28bf
TG
61 struct option options[] = {
62 OPT__QUIET(&quiet, N_("do not print remote URL")),
63 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
64 N_("path of git-upload-pack on the remote host")),
65 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
66 N_("path of git-upload-pack on the remote host"),
67 PARSE_OPT_HIDDEN },
68 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
69 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
70 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
71 OPT_BOOL(0, "get-url", &get_url,
72 N_("take url.<base>.insteadOf into account")),
98e7ab6d 73 OPT_REF_SORT(&sorting_options),
cdc71c1c
NTND
74 OPT_SET_INT_F(0, "exit-code", &status,
75 N_("exit with exit code 2 if no matching refs are found"),
76 2, PARSE_OPT_NOCOMPLETE),
99c08d4e
TG
77 OPT_BOOL(0, "symref", &show_symref_target,
78 N_("show underlying ref in addition to the object pointed by it")),
ff473221 79 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
ba5f28bf
TG
80 OPT_END()
81 };
91a640ff 82
1fb20dfd
HN
83 memset(&ref_array, 0, sizeof(ref_array));
84
ba5f28bf
TG
85 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
86 PARSE_OPT_STOP_AT_NON_OPTION);
87 dest = argv[0];
18705953 88
f58c7468
ÆAB
89 packet_trace_identity("ls-remote");
90
ba5f28bf
TG
91 if (argc > 1) {
92 int i;
ca56dadb 93 CALLOC_ARRAY(pattern, argc);
b4be7410 94 for (i = 1; i < argc; i++) {
ba5f28bf 95 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
b4be7410 96 }
18705953 97 }
2718ff09 98
6a139cdd 99 if (flags & REF_TAGS)
39835409 100 strvec_push(&transport_options.ref_prefixes, "refs/tags/");
6a139cdd 101 if (flags & REF_HEADS)
39835409 102 strvec_push(&transport_options.ref_prefixes, "refs/heads/");
6a139cdd 103
c1d45cf7 104 remote = remote_get(dest);
9c00de5a
TRC
105 if (!remote) {
106 if (dest)
107 die("bad repository '%s'", dest);
108 die("No remote configured to list refs from.");
109 }
c1d45cf7 110 if (!remote->url_nr)
7c2c6ee7 111 die("remote %s has no configured URL", dest);
45781adb
UKK
112
113 if (get_url) {
114 printf("%s\n", *remote->url);
115 return 0;
116 }
117
fb0cc87e 118 transport = transport_get(remote, NULL);
afe8a907 119 if (uploadpack)
18f7c51c 120 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
ff473221
BW
121 if (server_options.nr)
122 transport->server_options = &server_options;
18f7c51c 123
39835409 124 ref = transport_get_remote_refs(transport, &transport_options);
d96dab86 125 if (ref) {
126 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
127 repo_set_hash_algo(the_repository, hash_algo);
128 }
cefb2a5e
TRC
129
130 if (!dest && !quiet)
131 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d 132 for ( ; ref; ref = ref->next) {
1fb20dfd 133 struct ref_array_item *item;
2ea7fe0d
JH
134 if (!check_ref_type(ref, flags))
135 continue;
136 if (!tail_match(pattern, ref->name))
137 continue;
1fb20dfd
HN
138 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
139 item->symref = xstrdup_or_null(ref->symref);
140 }
141
98e7ab6d
JH
142 if (sorting_options.nr) {
143 struct ref_sorting *sorting;
144
145 sorting = ref_sorting_options(&sorting_options);
1fb20dfd 146 ref_array_sort(sorting, &ref_array);
98e7ab6d
JH
147 ref_sorting_release(sorting);
148 }
1fb20dfd
HN
149
150 for (i = 0; i < ref_array.nr; i++) {
151 const struct ref_array_item *ref = ref_array.items[i];
99c08d4e 152 if (show_symref_target && ref->symref)
1fb20dfd
HN
153 printf("ref: %s\t%s\n", ref->symref, ref->refname);
154 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
a8724773 155 status = 0; /* we found something */
18f7c51c 156 }
1fb20dfd 157
deec6b8e 158 ref_array_clear(&ref_array);
68ffe095 159 if (transport_disconnect(transport))
f36d4f83
ÆAB
160 status = 1;
161 transport_ls_refs_options_release(&transport_options);
a8724773 162 return status;
18705953 163}