sparse: Fix errors and silence warnings
[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] [<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 (!fnmatch(p, pathbuf, 0))
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         const char *uploadpack = NULL;
39         const char **pattern = NULL;
40
41         struct remote *remote;
42         struct transport *transport;
43         const struct ref *ref;
44
45         for (i = 1; i < argc; i++) {
46                 const char *arg = argv[i];
47
48                 if (*arg == '-') {
49                         if (!prefixcmp(arg, "--upload-pack=")) {
50                                 uploadpack = arg + 14;
51                                 continue;
52                         }
53                         if (!prefixcmp(arg, "--exec=")) {
54                                 uploadpack = arg + 7;
55                                 continue;
56                         }
57                         if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
58                                 flags |= REF_TAGS;
59                                 continue;
60                         }
61                         if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
62                                 flags |= REF_HEADS;
63                                 continue;
64                         }
65                         if (!strcmp("--refs", arg)) {
66                                 flags |= REF_NORMAL;
67                                 continue;
68                         }
69                         if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
70                                 quiet = 1;
71                                 continue;
72                         }
73                         if (!strcmp("--get-url", arg)) {
74                                 get_url = 1;
75                                 continue;
76                         }
77                         usage(ls_remote_usage);
78                 }
79                 dest = arg;
80                 i++;
81                 break;
82         }
83
84         if (argv[i]) {
85                 int j;
86                 pattern = xcalloc(sizeof(const char *), argc - i + 1);
87                 for (j = i; j < argc; j++) {
88                         int len = strlen(argv[j]);
89                         char *p = xmalloc(len + 3);
90                         sprintf(p, "*/%s", argv[j]);
91                         pattern[j - i] = p;
92                 }
93         }
94         remote = remote_get(dest);
95         if (!remote) {
96                 if (dest)
97                         die("bad repository '%s'", dest);
98                 die("No remote configured to list refs from.");
99         }
100         if (!remote->url_nr)
101                 die("remote %s has no configured URL", dest);
102
103         if (get_url) {
104                 printf("%s\n", *remote->url);
105                 return 0;
106         }
107
108         transport = transport_get(remote, NULL);
109         if (uploadpack != NULL)
110                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
111
112         ref = transport_get_remote_refs(transport);
113         if (transport_disconnect(transport))
114                 return 1;
115
116         if (!dest && !quiet)
117                 fprintf(stderr, "From %s\n", *remote->url);
118         for ( ; ref; ref = ref->next) {
119                 if (!check_ref_type(ref, flags))
120                         continue;
121                 if (!tail_match(pattern, ref->name))
122                         continue;
123                 printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
124         }
125         return 0;
126 }