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