Merge branch 'tt/help'
[git] / builtin-ls-remote.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
5
6 static const char ls_remote_usage[] =
7 "git-ls-remote [--upload-pack=<git-upload-pack>] [<host>:]<directory>";
8
9 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
10 {
11         int i;
12         const char *dest = NULL;
13         int nongit = 0;
14         unsigned flags = 0;
15         const char *uploadpack = NULL;
16
17         struct remote *remote;
18         struct transport *transport;
19         const struct ref *ref;
20
21         setup_git_directory_gently(&nongit);
22
23         for (i = 1; i < argc; i++) {
24                 const char *arg = argv[i];
25
26                 if (*arg == '-') {
27                         if (!prefixcmp(arg, "--upload-pack=")) {
28                                 uploadpack = arg + 14;
29                                 continue;
30                         }
31                         if (!prefixcmp(arg, "--exec=")) {
32                                 uploadpack = arg + 7;
33                                 continue;
34                         }
35                         if (!strcmp("--tags", arg)) {
36                                 flags |= REF_TAGS;
37                                 continue;
38                         }
39                         if (!strcmp("--heads", arg)) {
40                                 flags |= REF_HEADS;
41                                 continue;
42                         }
43                         if (!strcmp("--refs", arg)) {
44                                 flags |= REF_NORMAL;
45                                 continue;
46                         }
47                         usage(ls_remote_usage);
48                 }
49                 dest = arg;
50                 break;
51         }
52
53         if (!dest || i != argc - 1)
54                 usage(ls_remote_usage);
55
56         remote = nongit ? NULL : remote_get(dest);
57         if (remote && !remote->url_nr)
58                 die("remote %s has no configured URL", dest);
59         transport = transport_get(remote, remote ? remote->url[0] : dest);
60         if (uploadpack != NULL)
61                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
62
63         ref = transport_get_remote_refs(transport);
64
65         if (!ref)
66                 return 1;
67
68         while (ref) {
69                 if (check_ref_type(ref, flags))
70                         printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
71                 ref = ref->next;
72         }
73         return 0;
74 }