Build in ls-remote
[git/dscho.git] / builtin-ls-remote.c
blob003580c26a32d00c818c7f1e26b209645ff61ab4
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 [--upload-pack=<git-upload-pack>] [<host>:]<directory>";
9 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
11 int i;
12 const char *dest = NULL;
13 int nongit = 0;
14 unsigned flags = 0;
15 const char *uploadpack = NULL;
17 struct transport *transport;
18 const struct ref *ref;
20 setup_git_directory_gently(&nongit);
22 for (i = 1; i < argc; i++) {
23 const char *arg = argv[i];
25 if (*arg == '-') {
26 if (!prefixcmp(arg, "--upload-pack=")) {
27 uploadpack = arg + 14;
28 continue;
30 if (!prefixcmp(arg, "--exec=")) {
31 uploadpack = arg + 7;
32 continue;
34 if (!strcmp("--tags", arg)) {
35 flags |= REF_TAGS;
36 continue;
38 if (!strcmp("--heads", arg)) {
39 flags |= REF_HEADS;
40 continue;
42 if (!strcmp("--refs", arg)) {
43 flags |= REF_NORMAL;
44 continue;
46 usage(ls_remote_usage);
48 dest = arg;
49 break;
52 if (!dest || i != argc - 1)
53 usage(ls_remote_usage);
55 transport = transport_get(NULL, dest);
56 if (uploadpack != NULL)
57 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
59 ref = transport_get_remote_refs(transport);
61 if (!ref)
62 return 1;
64 while (ref) {
65 if (check_ref_type(ref, flags))
66 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
67 ref = ref->next;
69 return 0;