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