git_connect: use use_shell instead of explicit "sh", "-c"
[git/dscho.git] / builtin-ls-remote.c
blob70f5622d9d49aae4080b38ee4487cc6e403a9d2c
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>] <repository> <refs>...";
9 /*
10 * Is there one among the list of patterns that match the tail part
11 * of the path?
13 static int tail_match(const char **pattern, const char *path)
15 const char *p;
16 char pathbuf[PATH_MAX];
18 if (!pattern)
19 return 1; /* no restriction */
21 if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
22 return error("insanely long ref %.*s...", 20, path);
23 while ((p = *(pattern++)) != NULL) {
24 if (!fnmatch(p, pathbuf, 0))
25 return 1;
27 return 0;
30 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
32 int i;
33 const char *dest = NULL;
34 int nongit;
35 unsigned flags = 0;
36 const char *uploadpack = NULL;
37 const char **pattern = NULL;
39 struct remote *remote;
40 struct transport *transport;
41 const struct ref *ref;
43 setup_git_directory_gently(&nongit);
45 for (i = 1; i < argc; i++) {
46 const char *arg = argv[i];
48 if (*arg == '-') {
49 if (!prefixcmp(arg, "--upload-pack=")) {
50 uploadpack = arg + 14;
51 continue;
53 if (!prefixcmp(arg, "--exec=")) {
54 uploadpack = arg + 7;
55 continue;
57 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
58 flags |= REF_TAGS;
59 continue;
61 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
62 flags |= REF_HEADS;
63 continue;
65 if (!strcmp("--refs", arg)) {
66 flags |= REF_NORMAL;
67 continue;
69 usage(ls_remote_usage);
71 dest = arg;
72 i++;
73 break;
76 if (!dest)
77 usage(ls_remote_usage);
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->url_nr)
91 die("remote %s has no configured URL", dest);
92 transport = transport_get(remote, NULL);
93 if (uploadpack != NULL)
94 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
96 ref = transport_get_remote_refs(transport);
97 if (transport_disconnect(transport))
98 return 1;
99 for ( ; ref; ref = ref->next) {
100 if (!check_ref_type(ref, flags))
101 continue;
102 if (!tail_match(pattern, ref->name))
103 continue;
104 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
106 return 0;