Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "cache.h" |
| 3 | #include "transport.h" |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 4 | #include "ref-filter.h" |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 5 | #include "remote.h" |
Brandon Williams | b4be741 | 2018-03-15 10:31:24 -0700 | [diff] [blame] | 6 | #include "refs.h" |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 7 | |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 8 | static const char * const ls_remote_usage[] = { |
| 9 | N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n" |
Ævar Arnfjörð Bjarmason | 5d70198 | 2021-09-13 02:13:19 +0200 | [diff] [blame] | 10 | " [-q | --quiet] [--exit-code] [--get-url]\n" |
| 11 | " [--symref] [<repository> [<refs>...]]"), |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 12 | NULL |
| 13 | }; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 14 | |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 15 | /* |
Junio C Hamano | 3d3c4f5 | 2007-12-09 12:16:55 -0800 | [diff] [blame] | 16 | * Is there one among the list of patterns that match the tail part |
| 17 | * of the path? |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 18 | */ |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 19 | static int tail_match(const char **pattern, const char *path) |
| 20 | { |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 21 | const char *p; |
Jeff King | 7f897b6 | 2017-03-28 15:46:30 -0400 | [diff] [blame] | 22 | char *pathbuf; |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 23 | |
Junio C Hamano | 3d3c4f5 | 2007-12-09 12:16:55 -0800 | [diff] [blame] | 24 | if (!pattern) |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 25 | return 1; /* no restriction */ |
| 26 | |
Jeff King | 7f897b6 | 2017-03-28 15:46:30 -0400 | [diff] [blame] | 27 | pathbuf = xstrfmt("/%s", path); |
Junio C Hamano | 3d3c4f5 | 2007-12-09 12:16:55 -0800 | [diff] [blame] | 28 | while ((p = *(pattern++)) != NULL) { |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 29 | if (!wildmatch(p, pathbuf, 0)) { |
Jeff King | 7f897b6 | 2017-03-28 15:46:30 -0400 | [diff] [blame] | 30 | free(pathbuf); |
Junio C Hamano | 3d3c4f5 | 2007-12-09 12:16:55 -0800 | [diff] [blame] | 31 | return 1; |
Jeff King | 7f897b6 | 2017-03-28 15:46:30 -0400 | [diff] [blame] | 32 | } |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 33 | } |
Jeff King | 7f897b6 | 2017-03-28 15:46:30 -0400 | [diff] [blame] | 34 | free(pathbuf); |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Daniel Barkalow | 8951d7c | 2007-11-04 15:51:17 -0500 | [diff] [blame] | 38 | int cmd_ls_remote(int argc, const char **argv, const char *prefix) |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 39 | { |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 40 | const char *dest = NULL; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 41 | unsigned flags = 0; |
Uwe Kleine-König | 45781ad | 2011-03-01 10:21:36 +0100 | [diff] [blame] | 42 | int get_url = 0; |
Tay Ray Chuan | cefb2a5 | 2010-05-12 01:20:23 +0800 | [diff] [blame] | 43 | int quiet = 0; |
Michael Schubert | a872477 | 2011-05-18 22:06:00 +0200 | [diff] [blame] | 44 | int status = 0; |
Thomas Gummerer | 99c08d4 | 2016-01-19 00:20:50 +0100 | [diff] [blame] | 45 | int show_symref_target = 0; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 46 | const char *uploadpack = NULL; |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 47 | const char **pattern = NULL; |
Jonathan Tan | 3983540 | 2021-02-05 12:48:48 -0800 | [diff] [blame] | 48 | struct transport_ls_refs_options transport_options = |
| 49 | TRANSPORT_LS_REFS_OPTIONS_INIT; |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 50 | int i; |
Brandon Williams | ff47322 | 2018-04-23 15:46:23 -0700 | [diff] [blame] | 51 | struct string_list server_options = STRING_LIST_INIT_DUP; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 52 | |
Shawn O. Pearce | 7c2c6ee | 2007-11-06 20:29:20 -0500 | [diff] [blame] | 53 | struct remote *remote; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 54 | struct transport *transport; |
| 55 | const struct ref *ref; |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 56 | struct ref_array ref_array; |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 57 | struct string_list sorting_options = STRING_LIST_INIT_DUP; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 58 | |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 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")), |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 71 | OPT_REF_SORT(&sorting_options), |
Nguyễn Thái Ngọc Duy | cdc71c1 | 2018-02-09 18:02:03 +0700 | [diff] [blame] | 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), |
Thomas Gummerer | 99c08d4 | 2016-01-19 00:20:50 +0100 | [diff] [blame] | 75 | OPT_BOOL(0, "symref", &show_symref_target, |
| 76 | N_("show underlying ref in addition to the object pointed by it")), |
Brandon Williams | ff47322 | 2018-04-23 15:46:23 -0700 | [diff] [blame] | 77 | OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")), |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 78 | OPT_END() |
| 79 | }; |
Junio C Hamano | 91a640f | 2011-09-16 11:14:27 -0700 | [diff] [blame] | 80 | |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 81 | memset(&ref_array, 0, sizeof(ref_array)); |
| 82 | |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 83 | argc = parse_options(argc, argv, prefix, options, ls_remote_usage, |
| 84 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 85 | dest = argv[0]; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 86 | |
Ævar Arnfjörð Bjarmason | f58c746 | 2021-08-23 15:17:49 +0200 | [diff] [blame] | 87 | packet_trace_identity("ls-remote"); |
| 88 | |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 89 | if (argc > 1) { |
| 90 | int i; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 91 | CALLOC_ARRAY(pattern, argc); |
Brandon Williams | b4be741 | 2018-03-15 10:31:24 -0700 | [diff] [blame] | 92 | for (i = 1; i < argc; i++) { |
Thomas Gummerer | ba5f28b | 2016-01-19 00:20:49 +0100 | [diff] [blame] | 93 | pattern[i - 1] = xstrfmt("*/%s", argv[i]); |
Brandon Williams | b4be741 | 2018-03-15 10:31:24 -0700 | [diff] [blame] | 94 | } |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Jeff King | 6a139cd | 2018-10-31 00:24:42 -0400 | [diff] [blame] | 97 | if (flags & REF_TAGS) |
Jonathan Tan | 3983540 | 2021-02-05 12:48:48 -0800 | [diff] [blame] | 98 | strvec_push(&transport_options.ref_prefixes, "refs/tags/"); |
Jeff King | 6a139cd | 2018-10-31 00:24:42 -0400 | [diff] [blame] | 99 | if (flags & REF_HEADS) |
Jonathan Tan | 3983540 | 2021-02-05 12:48:48 -0800 | [diff] [blame] | 100 | strvec_push(&transport_options.ref_prefixes, "refs/heads/"); |
Jeff King | 6a139cd | 2018-10-31 00:24:42 -0400 | [diff] [blame] | 101 | |
Daniel Barkalow | c1d45cf | 2009-11-03 21:38:51 -0500 | [diff] [blame] | 102 | remote = remote_get(dest); |
Tay Ray Chuan | 9c00de5 | 2010-04-09 01:21:13 +0800 | [diff] [blame] | 103 | if (!remote) { |
| 104 | if (dest) |
| 105 | die("bad repository '%s'", dest); |
| 106 | die("No remote configured to list refs from."); |
| 107 | } |
Daniel Barkalow | c1d45cf | 2009-11-03 21:38:51 -0500 | [diff] [blame] | 108 | if (!remote->url_nr) |
Shawn O. Pearce | 7c2c6ee | 2007-11-06 20:29:20 -0500 | [diff] [blame] | 109 | die("remote %s has no configured URL", dest); |
Uwe Kleine-König | 45781ad | 2011-03-01 10:21:36 +0100 | [diff] [blame] | 110 | |
| 111 | if (get_url) { |
| 112 | printf("%s\n", *remote->url); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 116 | transport = transport_get(remote, NULL); |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 117 | if (uploadpack != NULL) |
| 118 | transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); |
Brandon Williams | ff47322 | 2018-04-23 15:46:23 -0700 | [diff] [blame] | 119 | if (server_options.nr) |
| 120 | transport->server_options = &server_options; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 121 | |
Jonathan Tan | 3983540 | 2021-02-05 12:48:48 -0800 | [diff] [blame] | 122 | ref = transport_get_remote_refs(transport, &transport_options); |
brian m. carlson | d96dab8 | 2020-05-25 19:59:19 +0000 | [diff] [blame] | 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); |
| 126 | } |
Tay Ray Chuan | cefb2a5 | 2010-05-12 01:20:23 +0800 | [diff] [blame] | 127 | |
| 128 | if (!dest && !quiet) |
| 129 | fprintf(stderr, "From %s\n", *remote->url); |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 130 | for ( ; ref; ref = ref->next) { |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 131 | struct ref_array_item *item; |
Junio C Hamano | 2ea7fe0 | 2007-12-08 22:52:59 -0800 | [diff] [blame] | 132 | if (!check_ref_type(ref, flags)) |
| 133 | continue; |
| 134 | if (!tail_match(pattern, ref->name)) |
| 135 | continue; |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 136 | item = ref_array_push(&ref_array, ref->name, &ref->old_oid); |
| 137 | item->symref = xstrdup_or_null(ref->symref); |
| 138 | } |
| 139 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 140 | if (sorting_options.nr) { |
| 141 | struct ref_sorting *sorting; |
| 142 | |
| 143 | sorting = ref_sorting_options(&sorting_options); |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 144 | ref_array_sort(sorting, &ref_array); |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 145 | ref_sorting_release(sorting); |
| 146 | } |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 147 | |
| 148 | for (i = 0; i < ref_array.nr; i++) { |
| 149 | const struct ref_array_item *ref = ref_array.items[i]; |
Thomas Gummerer | 99c08d4 | 2016-01-19 00:20:50 +0100 | [diff] [blame] | 150 | if (show_symref_target && ref->symref) |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 151 | printf("ref: %s\t%s\n", ref->symref, ref->refname); |
| 152 | printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname); |
Michael Schubert | a872477 | 2011-05-18 22:06:00 +0200 | [diff] [blame] | 153 | status = 0; /* we found something */ |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 154 | } |
Harald Nordgren | 1fb20df | 2018-04-09 03:42:26 +0200 | [diff] [blame] | 155 | |
Olga Telezhnaya | deec6b8 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 156 | ref_array_clear(&ref_array); |
Andrzej Hunt | 68ffe09 | 2021-03-21 16:58:37 +0000 | [diff] [blame] | 157 | if (transport_disconnect(transport)) |
| 158 | return 1; |
Michael Schubert | a872477 | 2011-05-18 22:06:00 +0200 | [diff] [blame] | 159 | return status; |
Daniel Barkalow | 18f7c51 | 2007-10-29 21:05:43 -0400 | [diff] [blame] | 160 | } |