Git 1.7.4
[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
8951d7c1 6static const char ls_remote_usage[] =
9c00de5a 7"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
cefb2a5e 8" [-q|--quiet] [<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) {
25 if (!fnmatch(p, pathbuf, 0))
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;
cefb2a5e 36 int quiet = 0;
18f7c51c 37 const char *uploadpack = NULL;
2ea7fe0d 38 const char **pattern = NULL;
18f7c51c 39
7c2c6ee7 40 struct remote *remote;
18f7c51c
DB
41 struct transport *transport;
42 const struct ref *ref;
e44eb3e4 43
18705953 44 for (i = 1; i < argc; i++) {
18f7c51c 45 const char *arg = argv[i];
18705953
JH
46
47 if (*arg == '-') {
599065a3 48 if (!prefixcmp(arg, "--upload-pack=")) {
27dca07f
UKK
49 uploadpack = arg + 14;
50 continue;
51 }
599065a3 52 if (!prefixcmp(arg, "--exec=")) {
27dca07f 53 uploadpack = arg + 7;
2718ff09
LT
54 continue;
55 }
3e345269 56 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
2718ff09
LT
57 flags |= REF_TAGS;
58 continue;
59 }
3e345269 60 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
2718ff09
LT
61 flags |= REF_HEADS;
62 continue;
63 }
64 if (!strcmp("--refs", arg)) {
65 flags |= REF_NORMAL;
66 continue;
67 }
cefb2a5e
TRC
68 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
69 quiet = 1;
70 continue;
71 }
8951d7c1 72 usage(ls_remote_usage);
18705953
JH
73 }
74 dest = arg;
3d3c4f5d 75 i++;
18705953
JH
76 break;
77 }
2718ff09 78
3d3c4f5d
JH
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;
87 }
88 }
c1d45cf7 89 remote = remote_get(dest);
9c00de5a
TRC
90 if (!remote) {
91 if (dest)
92 die("bad repository '%s'", dest);
93 die("No remote configured to list refs from.");
94 }
c1d45cf7 95 if (!remote->url_nr)
7c2c6ee7 96 die("remote %s has no configured URL", dest);
fb0cc87e 97 transport = transport_get(remote, NULL);
18f7c51c
DB
98 if (uploadpack != NULL)
99 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
100
101 ref = transport_get_remote_refs(transport);
27b4070e 102 if (transport_disconnect(transport))
18f7c51c 103 return 1;
cefb2a5e
TRC
104
105 if (!dest && !quiet)
106 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
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);
18f7c51c
DB
113 }
114 return 0;
18705953 115}