4 #include "ref-filter.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]\n"
11 " [--symref] [<repository> [<refs>...]]"),
16 * Is there one among the list of patterns that match the tail part
19 static int tail_match(const char **pattern, const char *path)
25 return 1; /* no restriction */
27 pathbuf = xstrfmt("/%s", path);
28 while ((p = *(pattern++)) != NULL) {
29 if (!wildmatch(p, pathbuf, 0)) {
38 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
40 const char *dest = NULL;
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;
50 struct string_list server_options = STRING_LIST_INIT_DUP;
52 struct remote *remote;
53 struct transport *transport;
54 const struct ref *ref;
55 struct ref_array ref_array;
56 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
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"),
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")),
70 OPT_REF_SORT(sorting_tail),
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_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
80 memset(&ref_array, 0, sizeof(ref_array));
82 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
83 PARSE_OPT_STOP_AT_NON_OPTION);
88 pattern = xcalloc(argc, sizeof(const char *));
89 for (i = 1; i < argc; i++) {
90 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
95 argv_array_push(&ref_prefixes, "refs/tags/");
96 if (flags & REF_HEADS)
97 argv_array_push(&ref_prefixes, "refs/heads/");
99 remote = remote_get(dest);
102 die("bad repository '%s'", dest);
103 die("No remote configured to list refs from.");
106 die("remote %s has no configured URL", dest);
109 printf("%s\n", *remote->url);
114 transport = transport_get(remote, NULL);
115 if (uploadpack != NULL)
116 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
117 if (server_options.nr)
118 transport->server_options = &server_options;
120 ref = transport_get_remote_refs(transport, &ref_prefixes);
122 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
123 repo_set_hash_algo(the_repository, hash_algo);
125 if (transport_disconnect(transport)) {
131 fprintf(stderr, "From %s\n", *remote->url);
132 for ( ; ref; ref = ref->next) {
133 struct ref_array_item *item;
134 if (!check_ref_type(ref, flags))
136 if (!tail_match(pattern, ref->name))
138 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
139 item->symref = xstrdup_or_null(ref->symref);
143 ref_array_sort(sorting, &ref_array);
145 for (i = 0; i < ref_array.nr; i++) {
146 const struct ref_array_item *ref = ref_array.items[i];
147 if (show_symref_target && ref->symref)
148 printf("ref: %s\t%s\n", ref->symref, ref->refname);
149 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
150 status = 0; /* we found something */
154 ref_array_clear(&ref_array);