gitweb: Prepare for splitting gitweb
[git/jnareb-git/bp-gitweb.git] / builtin / ls-remote.c
blob97eed4012ba23cfe7f13cf2b6c160ade54f6b2d1
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
6 static const char ls_remote_usage[] =
7 "git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
8 " [-q|--quiet] [<repository> [<refs>...]]";
11 * Is there one among the list of patterns that match the tail part
12 * of the path?
14 static int tail_match(const char **pattern, const char *path)
16 const char *p;
17 char pathbuf[PATH_MAX];
19 if (!pattern)
20 return 1; /* no restriction */
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;
28 return 0;
31 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
33 int i;
34 const char *dest = NULL;
35 unsigned flags = 0;
36 int quiet = 0;
37 const char *uploadpack = NULL;
38 const char **pattern = NULL;
40 struct remote *remote;
41 struct transport *transport;
42 const struct ref *ref;
44 for (i = 1; i < argc; i++) {
45 const char *arg = argv[i];
47 if (*arg == '-') {
48 if (!prefixcmp(arg, "--upload-pack=")) {
49 uploadpack = arg + 14;
50 continue;
52 if (!prefixcmp(arg, "--exec=")) {
53 uploadpack = arg + 7;
54 continue;
56 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
57 flags |= REF_TAGS;
58 continue;
60 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
61 flags |= REF_HEADS;
62 continue;
64 if (!strcmp("--refs", arg)) {
65 flags |= REF_NORMAL;
66 continue;
68 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
69 quiet = 1;
70 continue;
72 usage(ls_remote_usage);
74 dest = arg;
75 i++;
76 break;
79 if (argv[i]) {
80 int j;
81 pattern = xcalloc(sizeof(const char *), argc - i + 1);
82 for (j = i; j < argc; j++) {
83 int len = strlen(argv[j]);
84 char *p = xmalloc(len + 3);
85 sprintf(p, "*/%s", argv[j]);
86 pattern[j - i] = p;
89 remote = remote_get(dest);
90 if (!remote) {
91 if (dest)
92 die("bad repository '%s'", dest);
93 die("No remote configured to list refs from.");
95 if (!remote->url_nr)
96 die("remote %s has no configured URL", dest);
97 transport = transport_get(remote, NULL);
98 if (uploadpack != NULL)
99 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
101 ref = transport_get_remote_refs(transport);
102 if (transport_disconnect(transport))
103 return 1;
105 if (!dest && !quiet)
106 fprintf(stderr, "From %s\n", *remote->url);
107 for ( ; ref; ref = ref->next) {
108 if (!check_ref_type(ref, flags))
109 continue;
110 if (!tail_match(pattern, ref->name))
111 continue;
112 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
114 return 0;