completion: use __gitcomp_builtin in _git_ls_files
[git/git.git] / builtin / ls-remote.c
CommitLineData
18f7c51c 1#include "builtin.h"
18705953 2#include "cache.h"
18f7c51c
DB
3#include "transport.h"
4#include "remote.h"
18705953 5
ba5f28bf
TG
6static const char * const ls_remote_usage[] = {
7 N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
99c08d4e
TG
8 " [-q | --quiet] [--exit-code] [--get-url]\n"
9 " [--symref] [<repository> [<refs>...]]"),
ba5f28bf
TG
10 NULL
11};
18705953 12
2ea7fe0d 13/*
3d3c4f5d
JH
14 * Is there one among the list of patterns that match the tail part
15 * of the path?
2ea7fe0d 16 */
2ea7fe0d
JH
17static int tail_match(const char **pattern, const char *path)
18{
2ea7fe0d 19 const char *p;
7f897b6f 20 char *pathbuf;
2ea7fe0d 21
3d3c4f5d 22 if (!pattern)
2ea7fe0d
JH
23 return 1; /* no restriction */
24
7f897b6f 25 pathbuf = xstrfmt("/%s", path);
3d3c4f5d 26 while ((p = *(pattern++)) != NULL) {
55d34269 27 if (!wildmatch(p, pathbuf, 0)) {
7f897b6f 28 free(pathbuf);
3d3c4f5d 29 return 1;
7f897b6f 30 }
2ea7fe0d 31 }
7f897b6f 32 free(pathbuf);
2ea7fe0d
JH
33 return 0;
34}
35
8951d7c1 36int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 37{
18f7c51c 38 const char *dest = NULL;
2718ff09 39 unsigned flags = 0;
45781adb 40 int get_url = 0;
cefb2a5e 41 int quiet = 0;
a8724773 42 int status = 0;
99c08d4e 43 int show_symref_target = 0;
18f7c51c 44 const char *uploadpack = NULL;
2ea7fe0d 45 const char **pattern = NULL;
18f7c51c 46
7c2c6ee7 47 struct remote *remote;
18f7c51c
DB
48 struct transport *transport;
49 const struct ref *ref;
e44eb3e4 50
ba5f28bf
TG
51 struct option options[] = {
52 OPT__QUIET(&quiet, N_("do not print remote URL")),
53 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
54 N_("path of git-upload-pack on the remote host")),
55 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
56 N_("path of git-upload-pack on the remote host"),
57 PARSE_OPT_HIDDEN },
58 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
59 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
60 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
61 OPT_BOOL(0, "get-url", &get_url,
62 N_("take url.<base>.insteadOf into account")),
63 OPT_SET_INT(0, "exit-code", &status,
64 N_("exit with exit code 2 if no matching refs are found"), 2),
99c08d4e
TG
65 OPT_BOOL(0, "symref", &show_symref_target,
66 N_("show underlying ref in addition to the object pointed by it")),
ba5f28bf
TG
67 OPT_END()
68 };
91a640ff 69
ba5f28bf
TG
70 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
71 PARSE_OPT_STOP_AT_NON_OPTION);
72 dest = argv[0];
18705953 73
ba5f28bf
TG
74 if (argc > 1) {
75 int i;
76 pattern = xcalloc(argc, sizeof(const char *));
77 for (i = 1; i < argc; i++)
78 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
18705953 79 }
2718ff09 80
c1d45cf7 81 remote = remote_get(dest);
9c00de5a
TRC
82 if (!remote) {
83 if (dest)
84 die("bad repository '%s'", dest);
85 die("No remote configured to list refs from.");
86 }
c1d45cf7 87 if (!remote->url_nr)
7c2c6ee7 88 die("remote %s has no configured URL", dest);
45781adb
UKK
89
90 if (get_url) {
91 printf("%s\n", *remote->url);
92 return 0;
93 }
94
fb0cc87e 95 transport = transport_get(remote, NULL);
18f7c51c
DB
96 if (uploadpack != NULL)
97 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
98
99 ref = transport_get_remote_refs(transport);
27b4070e 100 if (transport_disconnect(transport))
18f7c51c 101 return 1;
cefb2a5e
TRC
102
103 if (!dest && !quiet)
104 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
105 for ( ; ref; ref = ref->next) {
106 if (!check_ref_type(ref, flags))
107 continue;
108 if (!tail_match(pattern, ref->name))
109 continue;
99c08d4e
TG
110 if (show_symref_target && ref->symref)
111 printf("ref: %s\t%s\n", ref->symref, ref->name);
112 printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
a8724773 113 status = 0; /* we found something */
18f7c51c 114 }
a8724773 115 return status;
18705953 116}