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