add macro REALLOC_ARRAY
[git] / builtin / ls-remote.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
5
6 static const char ls_remote_usage[] =
7 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
8 "                     [-q|--quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
9
10 /*
11  * Is there one among the list of patterns that match the tail part
12  * of the path?
13  */
14 static int tail_match(const char **pattern, const char *path)
15 {
16         const char *p;
17         char pathbuf[PATH_MAX];
18
19         if (!pattern)
20                 return 1; /* no restriction */
21
22         if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
23                 return error("insanely long ref %.*s...", 20, path);
24         while ((p = *(pattern++)) != NULL) {
25                 if (!wildmatch(p, pathbuf, 0, NULL))
26                         return 1;
27         }
28         return 0;
29 }
30
31 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
32 {
33         int i;
34         const char *dest = NULL;
35         unsigned flags = 0;
36         int get_url = 0;
37         int quiet = 0;
38         int status = 0;
39         const char *uploadpack = NULL;
40         const char **pattern = NULL;
41
42         struct remote *remote;
43         struct transport *transport;
44         const struct ref *ref;
45
46         if (argc == 2 && !strcmp("-h", argv[1]))
47                 usage(ls_remote_usage);
48
49         for (i = 1; i < argc; i++) {
50                 const char *arg = argv[i];
51
52                 if (*arg == '-') {
53                         if (starts_with(arg, "--upload-pack=")) {
54                                 uploadpack = arg + 14;
55                                 continue;
56                         }
57                         if (starts_with(arg, "--exec=")) {
58                                 uploadpack = arg + 7;
59                                 continue;
60                         }
61                         if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
62                                 flags |= REF_TAGS;
63                                 continue;
64                         }
65                         if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
66                                 flags |= REF_HEADS;
67                                 continue;
68                         }
69                         if (!strcmp("--refs", arg)) {
70                                 flags |= REF_NORMAL;
71                                 continue;
72                         }
73                         if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
74                                 quiet = 1;
75                                 continue;
76                         }
77                         if (!strcmp("--get-url", arg)) {
78                                 get_url = 1;
79                                 continue;
80                         }
81                         if (!strcmp("--exit-code", arg)) {
82                                 /* return this code if no refs are reported */
83                                 status = 2;
84                                 continue;
85                         }
86                         usage(ls_remote_usage);
87                 }
88                 dest = arg;
89                 i++;
90                 break;
91         }
92
93         if (argv[i]) {
94                 int j;
95                 pattern = xcalloc(argc - i + 1, sizeof(const char *));
96                 for (j = i; j < argc; j++) {
97                         int len = strlen(argv[j]);
98                         char *p = xmalloc(len + 3);
99                         sprintf(p, "*/%s", argv[j]);
100                         pattern[j - i] = p;
101                 }
102         }
103         remote = remote_get(dest);
104         if (!remote) {
105                 if (dest)
106                         die("bad repository '%s'", dest);
107                 die("No remote configured to list refs from.");
108         }
109         if (!remote->url_nr)
110                 die("remote %s has no configured URL", dest);
111
112         if (get_url) {
113                 printf("%s\n", *remote->url);
114                 return 0;
115         }
116
117         transport = transport_get(remote, NULL);
118         if (uploadpack != NULL)
119                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
120
121         ref = transport_get_remote_refs(transport);
122         if (transport_disconnect(transport))
123                 return 1;
124
125         if (!dest && !quiet)
126                 fprintf(stderr, "From %s\n", *remote->url);
127         for ( ; ref; ref = ref->next) {
128                 if (!check_ref_type(ref, flags))
129                         continue;
130                 if (!tail_match(pattern, ref->name))
131                         continue;
132                 printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
133                 status = 0; /* we found something */
134         }
135         return status;
136 }