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