Merge branch 'mg/notes-newline'
[git/debian.git] / builtin / ls-remote.c
blob5d5ac0387165fa8f4fb5a4c22b0f0f002f0e1545
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"
8 static 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] [--sort=<key>]\n"
11 " [--symref] [<repository> [<refs>...]]"),
12 NULL
16 * Is there one among the list of patterns that match the tail part
17 * of the path?
19 static int tail_match(const char **pattern, const char *path)
21 const char *p;
22 char *pathbuf;
24 if (!pattern)
25 return 1; /* no restriction */
27 pathbuf = xstrfmt("/%s", path);
28 while ((p = *(pattern++)) != NULL) {
29 if (!wildmatch(p, pathbuf, 0)) {
30 free(pathbuf);
31 return 1;
34 free(pathbuf);
35 return 0;
38 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
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 transport_ls_refs_options transport_options =
49 TRANSPORT_LS_REFS_OPTIONS_INIT;
50 int i;
51 struct string_list server_options = STRING_LIST_INIT_DUP;
53 struct remote *remote;
54 struct transport *transport;
55 const struct ref *ref;
56 struct ref_array ref_array;
57 struct string_list sorting_options = STRING_LIST_INIT_DUP;
59 struct option options[] = {
60 OPT__QUIET(&quiet, N_("do not print remote URL")),
61 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
62 N_("path of git-upload-pack on the remote host")),
63 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
64 N_("path of git-upload-pack on the remote host"),
65 PARSE_OPT_HIDDEN },
66 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
67 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
68 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
69 OPT_BOOL(0, "get-url", &get_url,
70 N_("take url.<base>.insteadOf into account")),
71 OPT_REF_SORT(&sorting_options),
72 OPT_SET_INT_F(0, "exit-code", &status,
73 N_("exit with exit code 2 if no matching refs are found"),
74 2, PARSE_OPT_NOCOMPLETE),
75 OPT_BOOL(0, "symref", &show_symref_target,
76 N_("show underlying ref in addition to the object pointed by it")),
77 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
78 OPT_END()
81 memset(&ref_array, 0, sizeof(ref_array));
83 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
84 PARSE_OPT_STOP_AT_NON_OPTION);
85 dest = argv[0];
87 packet_trace_identity("ls-remote");
89 if (argc > 1) {
90 int i;
91 CALLOC_ARRAY(pattern, argc);
92 for (i = 1; i < argc; i++) {
93 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
97 if (flags & REF_TAGS)
98 strvec_push(&transport_options.ref_prefixes, "refs/tags/");
99 if (flags & REF_HEADS)
100 strvec_push(&transport_options.ref_prefixes, "refs/heads/");
102 remote = remote_get(dest);
103 if (!remote) {
104 if (dest)
105 die("bad repository '%s'", dest);
106 die("No remote configured to list refs from.");
108 if (!remote->url_nr)
109 die("remote %s has no configured URL", dest);
111 if (get_url) {
112 printf("%s\n", *remote->url);
113 return 0;
116 transport = transport_get(remote, NULL);
117 if (uploadpack)
118 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
119 if (server_options.nr)
120 transport->server_options = &server_options;
122 ref = transport_get_remote_refs(transport, &transport_options);
123 if (ref) {
124 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
125 repo_set_hash_algo(the_repository, hash_algo);
128 if (!dest && !quiet)
129 fprintf(stderr, "From %s\n", *remote->url);
130 for ( ; ref; ref = ref->next) {
131 struct ref_array_item *item;
132 if (!check_ref_type(ref, flags))
133 continue;
134 if (!tail_match(pattern, ref->name))
135 continue;
136 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
137 item->symref = xstrdup_or_null(ref->symref);
140 if (sorting_options.nr) {
141 struct ref_sorting *sorting;
143 sorting = ref_sorting_options(&sorting_options);
144 ref_array_sort(sorting, &ref_array);
145 ref_sorting_release(sorting);
148 for (i = 0; i < ref_array.nr; i++) {
149 const struct ref_array_item *ref = ref_array.items[i];
150 if (show_symref_target && ref->symref)
151 printf("ref: %s\t%s\n", ref->symref, ref->refname);
152 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
153 status = 0; /* we found something */
156 ref_array_clear(&ref_array);
157 if (transport_disconnect(transport))
158 status = 1;
159 transport_ls_refs_options_release(&transport_options);
160 return status;