]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/ls-remote.c
t/README: A new section about test coverage
[thirdparty/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;
af05d679 35 int nongit;
2718ff09 36 unsigned flags = 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
JH
44
45 setup_git_directory_gently(&nongit);
18705953
JH
46
47 for (i = 1; i < argc; i++) {
18f7c51c 48 const char *arg = argv[i];
18705953
JH
49
50 if (*arg == '-') {
599065a3 51 if (!prefixcmp(arg, "--upload-pack=")) {
27dca07f
UKK
52 uploadpack = arg + 14;
53 continue;
54 }
599065a3 55 if (!prefixcmp(arg, "--exec=")) {
27dca07f 56 uploadpack = arg + 7;
2718ff09
LT
57 continue;
58 }
3e345269 59 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
2718ff09
LT
60 flags |= REF_TAGS;
61 continue;
62 }
3e345269 63 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
2718ff09
LT
64 flags |= REF_HEADS;
65 continue;
66 }
67 if (!strcmp("--refs", arg)) {
68 flags |= REF_NORMAL;
69 continue;
70 }
cefb2a5e
TRC
71 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
72 quiet = 1;
73 continue;
74 }
8951d7c1 75 usage(ls_remote_usage);
18705953
JH
76 }
77 dest = arg;
3d3c4f5d 78 i++;
18705953
JH
79 break;
80 }
2718ff09 81
3d3c4f5d
JH
82 if (argv[i]) {
83 int j;
84 pattern = xcalloc(sizeof(const char *), argc - i + 1);
85 for (j = i; j < argc; j++) {
86 int len = strlen(argv[j]);
87 char *p = xmalloc(len + 3);
88 sprintf(p, "*/%s", argv[j]);
89 pattern[j - i] = p;
90 }
91 }
c1d45cf7 92 remote = remote_get(dest);
9c00de5a
TRC
93 if (!remote) {
94 if (dest)
95 die("bad repository '%s'", dest);
96 die("No remote configured to list refs from.");
97 }
c1d45cf7 98 if (!remote->url_nr)
7c2c6ee7 99 die("remote %s has no configured URL", dest);
fb0cc87e 100 transport = transport_get(remote, NULL);
18f7c51c
DB
101 if (uploadpack != NULL)
102 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
103
104 ref = transport_get_remote_refs(transport);
27b4070e 105 if (transport_disconnect(transport))
18f7c51c 106 return 1;
cefb2a5e
TRC
107
108 if (!dest && !quiet)
109 fprintf(stderr, "From %s\n", *remote->url);
2ea7fe0d
JH
110 for ( ; ref; ref = ref->next) {
111 if (!check_ref_type(ref, flags))
112 continue;
113 if (!tail_match(pattern, ref->name))
114 continue;
115 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
18f7c51c
DB
116 }
117 return 0;
18705953 118}