1#include "builtin.h"
2#include "cache.h"
3#include "transport.h"
4#include "ref-filter.h"
5#include "remote.h"
6#include "refs.h"
7
8static const char * const ls_remote_usage[] = {
9 N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
10 " [-q | --quiet] [--exit-code] [--get-url]\n"
11 " [--symref] [<repository> [<refs>...]]"),
12 NULL
13};
14
15/*
16 * Is there one among the list of patterns that match the tail part
17 * of the path?
18 */
19static int tail_match(const char **pattern, const char *path)
20{
21 const char *p;
22 char *pathbuf;
23
24 if (!pattern)
25 return 1; /* no restriction */
26
27 pathbuf = xstrfmt("/%s", path);
28 while ((p = *(pattern++)) != NULL) {
29 if (!wildmatch(p, pathbuf, 0)) {
30 free(pathbuf);
31 return 1;
32 }
33 }
34 free(pathbuf);
35 return 0;
36}
37
38int cmd_ls_remote(int argc, const char **argv, const char *prefix)
39{
40 const char *dest = NULL;
41 unsigned flags = 0;
42 int get_url = 0;
43 int quiet = 0;
44 int status = 0;
45 int show_symref_target = 0;
46 const char *uploadpack = NULL;
47 const char **pattern = NULL;
48 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
49 int i;
50
51 struct remote *remote;
52 struct transport *transport;
53 const struct ref *ref;
54 struct ref_array ref_array;
55 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
56
57 struct option options[] = {
58 OPT__QUIET(&quiet, N_("do not print remote URL")),
59 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
60 N_("path of git-upload-pack on the remote host")),
61 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
62 N_("path of git-upload-pack on the remote host"),
63 PARSE_OPT_HIDDEN },
64 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
65 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
66 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
67 OPT_BOOL(0, "get-url", &get_url,
68 N_("take url.<base>.insteadOf into account")),
69 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
70 N_("field name to sort on"), &parse_opt_ref_sorting),
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),
74 OPT_BOOL(0, "symref", &show_symref_target,
75 N_("show underlying ref in addition to the object pointed by it")),
76 OPT_END()
77 };
78
79 memset(&ref_array, 0, sizeof(ref_array));
80
81 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
82 PARSE_OPT_STOP_AT_NON_OPTION);
83 dest = argv[0];
84
85 if (argc > 1) {
86 int i;
87 pattern = xcalloc(argc, sizeof(const char *));
88 for (i = 1; i < argc; i++) {
89 const char *glob;
90 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
91
92 glob = strchr(argv[i], '*');
93 if (glob)
94 argv_array_pushf(&ref_prefixes, "%.*s",
95 (int)(glob - argv[i]), argv[i]);
96 else
97 expand_ref_prefix(&ref_prefixes, argv[i]);
98 }
99 }
100
101 remote = remote_get(dest);
102 if (!remote) {
103 if (dest)
104 die("bad repository '%s'", dest);
105 die("No remote configured to list refs from.");
106 }
107 if (!remote->url_nr)
108 die("remote %s has no configured URL", dest);
109
110 if (get_url) {
111 printf("%s\n", *remote->url);
112 UNLEAK(sorting);
113 return 0;
114 }
115
116 transport = transport_get(remote, NULL);
117 if (uploadpack != NULL)
118 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
119
120 ref = transport_get_remote_refs(transport, &ref_prefixes);
121 if (transport_disconnect(transport)) {
122 UNLEAK(sorting);
123 return 1;
124 }
125
126 if (!dest && !quiet)
127 fprintf(stderr, "From %s\n", *remote->url);
128 for ( ; ref; ref = ref->next) {
129 struct ref_array_item *item;
130 if (!check_ref_type(ref, flags))
131 continue;
132 if (!tail_match(pattern, ref->name))
133 continue;
134 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
135 item->symref = xstrdup_or_null(ref->symref);
136 }
137
138 if (sorting)
139 ref_array_sort(sorting, &ref_array);
140
141 for (i = 0; i < ref_array.nr; i++) {
142 const struct ref_array_item *ref = ref_array.items[i];
143 if (show_symref_target && ref->symref)
144 printf("ref: %s\t%s\n", ref->symref, ref->refname);
145 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
146 status = 0; /* we found something */
147 }
148
149 UNLEAK(sorting);
150 UNLEAK(ref_array);
151 return status;
152}