6 #include "ref-filter.h"
8 #include "parse-options.h"
11 static const char * const ls_remote_usage
[] = {
12 N_("git ls-remote [--branches] [--tags] [--refs] [--upload-pack=<exec>]\n"
13 " [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n"
14 " [--symref] [<repository> [<patterns>...]]"),
19 * Is there one among the list of patterns that match the tail part
22 static int tail_match(const struct strvec
*pattern
, const char *path
)
27 return 1; /* no restriction */
29 pathbuf
= xstrfmt("/%s", path
);
30 for (size_t i
= 0; i
< pattern
->nr
; i
++) {
31 if (!wildmatch(pattern
->v
[i
], pathbuf
, 0)) {
40 int cmd_ls_remote(int argc
, const char **argv
, const char *prefix
)
42 const char *dest
= NULL
;
47 int show_symref_target
= 0;
48 const char *uploadpack
= NULL
;
49 struct strvec pattern
= STRVEC_INIT
;
50 struct transport_ls_refs_options transport_options
=
51 TRANSPORT_LS_REFS_OPTIONS_INIT
;
53 struct string_list server_options
= STRING_LIST_INIT_DUP
;
55 struct remote
*remote
;
56 struct transport
*transport
;
57 const struct ref
*ref
;
58 struct ref_array ref_array
;
59 struct ref_sorting
*sorting
;
60 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
62 struct option options
[] = {
63 OPT__QUIET(&quiet
, N_("do not print remote URL")),
64 OPT_STRING(0, "upload-pack", &uploadpack
, N_("exec"),
65 N_("path of git-upload-pack on the remote host")),
66 { OPTION_STRING
, 0, "exec", &uploadpack
, N_("exec"),
67 N_("path of git-upload-pack on the remote host"),
69 OPT_BIT('t', "tags", &flags
, N_("limit to tags"), REF_TAGS
),
70 OPT_BIT('b', "branches", &flags
, N_("limit to branches"), REF_BRANCHES
),
71 OPT_BIT_F('h', "heads", &flags
,
72 N_("deprecated synonym for --branches"), REF_BRANCHES
,
74 OPT_BIT(0, "refs", &flags
, N_("do not show peeled tags"), REF_NORMAL
),
75 OPT_BOOL(0, "get-url", &get_url
,
76 N_("take url.<base>.insteadOf into account")),
77 OPT_REF_SORT(&sorting_options
),
78 OPT_SET_INT_F(0, "exit-code", &status
,
79 N_("exit with exit code 2 if no matching refs are found"),
80 2, PARSE_OPT_NOCOMPLETE
),
81 OPT_BOOL(0, "symref", &show_symref_target
,
82 N_("show underlying ref in addition to the object pointed by it")),
83 OPT_STRING_LIST('o', "server-option", &server_options
, N_("server-specific"), N_("option to transmit")),
87 memset(&ref_array
, 0, sizeof(ref_array
));
89 argc
= parse_options(argc
, argv
, prefix
, options
, ls_remote_usage
,
90 PARSE_OPT_STOP_AT_NON_OPTION
);
94 * TODO: This is buggy, but required for transport helpers. When a
95 * transport helper advertises a "refspec", then we'd add that to a
96 * list of refspecs via `refspec_append()`, which transitively depends
97 * on `the_hash_algo`. Thus, when the hash algorithm isn't properly set
98 * up, this would lead to a segfault.
100 * We really should fix this in the transport helper logic such that we
101 * lazily parse refspec capabilities _after_ we have learned about the
102 * remote's object format. Otherwise, we may end up misparsing refspecs
103 * depending on what object hash the remote uses.
105 if (!the_repository
->hash_algo
)
106 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
108 packet_trace_identity("ls-remote");
110 for (int i
= 1; i
< argc
; i
++)
111 strvec_pushf(&pattern
, "*/%s", argv
[i
]);
113 if (flags
& REF_TAGS
)
114 strvec_push(&transport_options
.ref_prefixes
, "refs/tags/");
115 if (flags
& REF_BRANCHES
)
116 strvec_push(&transport_options
.ref_prefixes
, "refs/heads/");
118 remote
= remote_get(dest
);
121 die("bad repository '%s'", dest
);
122 die("No remote configured to list refs from.");
126 printf("%s\n", remote
->url
.v
[0]);
130 transport
= transport_get(remote
, NULL
);
132 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, uploadpack
);
133 if (server_options
.nr
)
134 transport
->server_options
= &server_options
;
136 ref
= transport_get_remote_refs(transport
, &transport_options
);
138 int hash_algo
= hash_algo_by_ptr(transport_get_hash_algo(transport
));
139 repo_set_hash_algo(the_repository
, hash_algo
);
143 fprintf(stderr
, "From %s\n", remote
->url
.v
[0]);
144 for ( ; ref
; ref
= ref
->next
) {
145 struct ref_array_item
*item
;
146 if (!check_ref_type(ref
, flags
))
148 if (!tail_match(&pattern
, ref
->name
))
150 item
= ref_array_push(&ref_array
, ref
->name
, &ref
->old_oid
);
151 item
->symref
= xstrdup_or_null(ref
->symref
);
154 sorting
= ref_sorting_options(&sorting_options
);
155 ref_array_sort(sorting
, &ref_array
);
157 for (i
= 0; i
< ref_array
.nr
; i
++) {
158 const struct ref_array_item
*ref
= ref_array
.items
[i
];
159 if (show_symref_target
&& ref
->symref
)
160 printf("ref: %s\t%s\n", ref
->symref
, ref
->refname
);
161 printf("%s\t%s\n", oid_to_hex(&ref
->objectname
), ref
->refname
);
162 status
= 0; /* we found something */
165 ref_sorting_release(sorting
);
166 ref_array_clear(&ref_array
);
167 if (transport_disconnect(transport
))
169 transport_ls_refs_options_release(&transport_options
);
171 strvec_clear(&pattern
);