]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/ls-remote.c
ls-remote: fix synopsis
[thirdparty/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
8951d7c1 6static const char ls_remote_usage[] =
40a88529 7"git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
9c9b4f2f 8" [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
18705953 9
2ea7fe0d 10/*
3d3c4f5d
JH
11 * Is there one among the list of patterns that match the tail part
12 * of the path?
2ea7fe0d 13 */
2ea7fe0d
JH
14static int tail_match(const char **pattern, const char *path)
15{
2ea7fe0d 16 const char *p;
3d3c4f5d 17 char pathbuf[PATH_MAX];
2ea7fe0d 18
3d3c4f5d 19 if (!pattern)
2ea7fe0d
JH
20 return 1; /* no restriction */
21
3d3c4f5d
JH
22 if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
23 return error("insanely long ref %.*s...", 20, path);
24 while ((p = *(pattern++)) != NULL) {
eb07894f 25 if (!wildmatch(p, pathbuf, 0, NULL))
3d3c4f5d 26 return 1;
2ea7fe0d
JH
27 }
28 return 0;
29}
30
8951d7c1 31int cmd_ls_remote(int argc, const char **argv, const char *prefix)
18705953 32{
18f7c51c
DB
33 int i;
34 const char *dest = NULL;
2718ff09 35 unsigned flags = 0;
45781adb 36 int get_url = 0;
cefb2a5e 37 int quiet = 0;
a8724773 38 int status = 0;
18f7c51c 39 const char *uploadpack = NULL;
2ea7fe0d 40 const char **pattern = NULL;
18f7c51c 41
7c2c6ee7 42 struct remote *remote;
18f7c51c
DB
43 struct transport *transport;
44 const struct ref *ref;
e44eb3e4 45
91a640ff
JH
46 if (argc == 2 && !strcmp("-h", argv[1]))
47 usage(ls_remote_usage);
48
18705953 49 for (i = 1; i < argc; i++) {
18f7c51c 50 const char *arg = argv[i];
18705953
JH
51
52 if (*arg == '-') {
59556548 53 if (starts_with(arg, "--upload-pack=")) {
27dca07f
UKK
54 uploadpack = arg + 14;
55 continue;
56 }
59556548 57 if (starts_with(arg, "--exec=")) {
27dca07f 58 uploadpack = arg + 7;
2718ff09
LT
59 continue;
60 }
3e345269 61 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
2718ff09
LT
62 flags |= REF_TAGS;
63 continue;
64 }
3e345269 65 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
2718ff09
LT
66 flags |= REF_HEADS;
67 continue;
68 }
69 if (!strcmp("--refs", arg)) {
70 flags |= REF_NORMAL;
71 continue;
72 }
cefb2a5e
TRC
73 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
74 quiet = 1;
75 continue;
76 }
45781adb
UKK
77 if (!strcmp("--get-url", arg)) {
78 get_url = 1;
79 continue;
80 }
a8724773
MS
81 if (!strcmp("--exit-code", arg)) {
82 /* return this code if no refs are reported */
83 status = 2;
84 continue;
85 }
8951d7c1 86 usage(ls_remote_usage);
18705953
JH
87 }
88 dest = arg;
3d3c4f5d 89 i++;
18705953
JH
90 break;
91 }
2718ff09 92
3d3c4f5d
JH
93 if (argv[i]) {
94 int j;
edd2d846 95 pattern = xcalloc(argc - i + 1, sizeof(const char *));
75faa45a
JK
96 for (j = i; j < argc; j++)
97 pattern[j - i] = xstrfmt("*/%s", argv[j]);
3d3c4f5d 98 }
c1d45cf7 99 remote = remote_get(dest);
9c00de5a
TRC
100 if (!remote) {
101 if (dest)
102 die("bad repository '%s'", dest);
103 die("No remote configured to list refs from.");
104 }
c1d45cf7 105 if (!remote->url_nr)
7c2c6ee7 106 die("remote %s has no configured URL", dest);
45781adb
UKK
107
108 if (get_url) {
109 printf("%s\n", *remote->url);
110 return 0;
111 }
112
fb0cc87e 113 transport = transport_get(remote, NULL);
18f7c51c
DB
114 if (uploadpack != NULL)
115 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
116
117 ref = transport_get_remote_refs(transport);
27b4070e 118 if (transport_disconnect(transport))
18f7c51c 119 return 1;
cefb2a5e
TRC
120
121 if (!dest && !quiet)
122 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
123 for ( ; ref; ref = ref->next) {
124 if (!check_ref_type(ref, flags))
125 continue;
126 if (!tail_match(pattern, ref->name))
127 continue;
f4e54d02 128 printf("%s %s\n", oid_to_hex(&ref->old_oid), ref->name);
a8724773 129 status = 0; /* we found something */
18f7c51c 130 }
a8724773 131 return status;
18705953 132}