Git fork
at reftables-rust 184 lines 5.6 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#include "builtin.h" 3#include "gettext.h" 4#include "hex.h" 5#include "transport.h" 6#include "pkt-line.h" 7#include "ref-filter.h" 8#include "remote.h" 9#include "parse-options.h" 10#include "wildmatch.h" 11 12static const char * const ls_remote_usage[] = { 13 N_("git ls-remote [--branches] [--tags] [--refs] [--upload-pack=<exec>]\n" 14 " [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n" 15 " [--symref] [<repository> [<patterns>...]]"), 16 NULL 17}; 18 19/* 20 * Is there one among the list of patterns that match the tail part 21 * of the path? 22 */ 23static int tail_match(const struct strvec *pattern, const char *path) 24{ 25 char *pathbuf; 26 27 if (!pattern->nr) 28 return 1; /* no restriction */ 29 30 pathbuf = xstrfmt("/%s", path); 31 for (size_t i = 0; i < pattern->nr; i++) { 32 if (!wildmatch(pattern->v[i], pathbuf, 0)) { 33 free(pathbuf); 34 return 1; 35 } 36 } 37 free(pathbuf); 38 return 0; 39} 40 41int cmd_ls_remote(int argc, 42 const char **argv, 43 const char *prefix, 44 struct repository *repo UNUSED) 45{ 46 const char *dest = NULL; 47 unsigned flags = 0; 48 int get_url = 0; 49 int quiet = 0; 50 int status = 0; 51 int show_symref_target = 0; 52 const char *uploadpack = NULL; 53 struct strvec pattern = STRVEC_INIT; 54 struct transport_ls_refs_options transport_options = 55 TRANSPORT_LS_REFS_OPTIONS_INIT; 56 int i; 57 struct string_list server_options = STRING_LIST_INIT_DUP; 58 59 struct remote *remote; 60 struct transport *transport; 61 const struct ref *ref; 62 struct ref_array ref_array; 63 struct ref_sorting *sorting; 64 struct string_list sorting_options = STRING_LIST_INIT_DUP; 65 66 struct option options[] = { 67 OPT__QUIET(&quiet, N_("do not print remote URL")), 68 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"), 69 N_("path of git-upload-pack on the remote host")), 70 { 71 .type = OPTION_STRING, 72 .long_name = "exec", 73 .value = &uploadpack, 74 .argh = N_("exec"), 75 .help = N_("path of git-upload-pack on the remote host"), 76 .flags = PARSE_OPT_HIDDEN, 77 }, 78 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS), 79 OPT_BIT('b', "branches", &flags, N_("limit to branches"), REF_BRANCHES), 80 OPT_BIT_F('h', "heads", &flags, 81 N_("deprecated synonym for --branches"), REF_BRANCHES, 82 PARSE_OPT_HIDDEN), 83 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL), 84 OPT_BOOL(0, "get-url", &get_url, 85 N_("take url.<base>.insteadOf into account")), 86 OPT_REF_SORT(&sorting_options), 87 OPT_SET_INT_F(0, "exit-code", &status, 88 N_("exit with exit code 2 if no matching refs are found"), 89 2, PARSE_OPT_NOCOMPLETE), 90 OPT_BOOL(0, "symref", &show_symref_target, 91 N_("show underlying ref in addition to the object pointed by it")), 92 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")), 93 OPT_END() 94 }; 95 96 memset(&ref_array, 0, sizeof(ref_array)); 97 98 argc = parse_options(argc, argv, prefix, options, ls_remote_usage, 99 PARSE_OPT_STOP_AT_NON_OPTION); 100 dest = argv[0]; 101 102 /* 103 * TODO: This is buggy, but required for transport helpers. When a 104 * transport helper advertises a "refspec", then we'd add that to a 105 * list of refspecs via `refspec_append()`, which transitively depends 106 * on `the_hash_algo`. Thus, when the hash algorithm isn't properly set 107 * up, this would lead to a segfault. 108 * 109 * We really should fix this in the transport helper logic such that we 110 * lazily parse refspec capabilities _after_ we have learned about the 111 * remote's object format. Otherwise, we may end up misparsing refspecs 112 * depending on what object hash the remote uses. 113 */ 114 if (!the_repository->hash_algo) 115 repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT); 116 117 packet_trace_identity("ls-remote"); 118 119 for (int i = 1; i < argc; i++) 120 strvec_pushf(&pattern, "*/%s", argv[i]); 121 122 if (flags & REF_TAGS) 123 strvec_push(&transport_options.ref_prefixes, "refs/tags/"); 124 if (flags & REF_BRANCHES) 125 strvec_push(&transport_options.ref_prefixes, "refs/heads/"); 126 127 remote = remote_get(dest); 128 if (!remote) { 129 if (dest) 130 die("bad repository '%s'", dest); 131 die("No remote configured to list refs from."); 132 } 133 134 if (get_url) { 135 printf("%s\n", remote->url.v[0]); 136 return 0; 137 } 138 139 transport = transport_get(remote, NULL); 140 if (uploadpack) 141 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); 142 if (server_options.nr) 143 transport->server_options = &server_options; 144 145 ref = transport_get_remote_refs(transport, &transport_options); 146 if (ref) { 147 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport)); 148 repo_set_hash_algo(the_repository, hash_algo); 149 } 150 151 if (!dest && !quiet) 152 fprintf(stderr, "From %s\n", remote->url.v[0]); 153 for ( ; ref; ref = ref->next) { 154 struct ref_array_item *item; 155 if (!check_ref_type(ref, flags)) 156 continue; 157 if (!tail_match(&pattern, ref->name)) 158 continue; 159 item = ref_array_push(&ref_array, ref->name, &ref->old_oid); 160 item->symref = xstrdup_or_null(ref->symref); 161 } 162 163 sorting = ref_sorting_options(&sorting_options); 164 ref_array_sort(sorting, &ref_array); 165 166 for (i = 0; i < ref_array.nr; i++) { 167 const struct ref_array_item *ref = ref_array.items[i]; 168 if (show_symref_target && ref->symref) 169 printf("ref: %s\t%s\n", ref->symref, ref->refname); 170 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname); 171 status = 0; /* we found something */ 172 } 173 174 string_list_clear(&server_options, 0); 175 ref_sorting_release(sorting); 176 ref_array_clear(&ref_array); 177 if (transport_disconnect(transport)) 178 status = 1; 179 transport_ls_refs_options_release(&transport_options); 180 181 strvec_clear(&pattern); 182 string_list_clear(&server_options, 0); 183 return status; 184}