]>
Commit | Line | Data |
---|---|---|
18f7c51c | 1 | #include "builtin.h" |
18705953 | 2 | #include "cache.h" |
18f7c51c DB |
3 | #include "transport.h" |
4 | #include "remote.h" | |
b4be7410 | 5 | #include "refs.h" |
18705953 | 6 | |
ba5f28bf TG |
7 | static const char * const ls_remote_usage[] = { |
8 | N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n" | |
99c08d4e TG |
9 | " [-q | --quiet] [--exit-code] [--get-url]\n" |
10 | " [--symref] [<repository> [<refs>...]]"), | |
ba5f28bf TG |
11 | NULL |
12 | }; | |
18705953 | 13 | |
2ea7fe0d | 14 | /* |
3d3c4f5d JH |
15 | * Is there one among the list of patterns that match the tail part |
16 | * of the path? | |
2ea7fe0d | 17 | */ |
2ea7fe0d JH |
18 | static int tail_match(const char **pattern, const char *path) |
19 | { | |
2ea7fe0d | 20 | const char *p; |
7f897b6f | 21 | char *pathbuf; |
2ea7fe0d | 22 | |
3d3c4f5d | 23 | if (!pattern) |
2ea7fe0d JH |
24 | return 1; /* no restriction */ |
25 | ||
7f897b6f | 26 | pathbuf = xstrfmt("/%s", path); |
3d3c4f5d | 27 | while ((p = *(pattern++)) != NULL) { |
55d34269 | 28 | if (!wildmatch(p, pathbuf, 0)) { |
7f897b6f | 29 | free(pathbuf); |
3d3c4f5d | 30 | return 1; |
7f897b6f | 31 | } |
2ea7fe0d | 32 | } |
7f897b6f | 33 | free(pathbuf); |
2ea7fe0d JH |
34 | return 0; |
35 | } | |
36 | ||
8951d7c1 | 37 | int cmd_ls_remote(int argc, const char **argv, const char *prefix) |
18705953 | 38 | { |
18f7c51c | 39 | const char *dest = NULL; |
2718ff09 | 40 | unsigned flags = 0; |
45781adb | 41 | int get_url = 0; |
cefb2a5e | 42 | int quiet = 0; |
a8724773 | 43 | int status = 0; |
99c08d4e | 44 | int show_symref_target = 0; |
18f7c51c | 45 | const char *uploadpack = NULL; |
2ea7fe0d | 46 | const char **pattern = NULL; |
b4be7410 | 47 | struct argv_array ref_prefixes = ARGV_ARRAY_INIT; |
18f7c51c | 48 | |
7c2c6ee7 | 49 | struct remote *remote; |
18f7c51c DB |
50 | struct transport *transport; |
51 | const struct ref *ref; | |
e44eb3e4 | 52 | |
ba5f28bf TG |
53 | struct option options[] = { |
54 | OPT__QUIET(&quiet, N_("do not print remote URL")), | |
55 | OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"), | |
56 | N_("path of git-upload-pack on the remote host")), | |
57 | { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"), | |
58 | N_("path of git-upload-pack on the remote host"), | |
59 | PARSE_OPT_HIDDEN }, | |
60 | OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS), | |
61 | OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS), | |
62 | OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL), | |
63 | OPT_BOOL(0, "get-url", &get_url, | |
64 | N_("take url.<base>.insteadOf into account")), | |
cdc71c1c NTND |
65 | OPT_SET_INT_F(0, "exit-code", &status, |
66 | N_("exit with exit code 2 if no matching refs are found"), | |
67 | 2, PARSE_OPT_NOCOMPLETE), | |
99c08d4e TG |
68 | OPT_BOOL(0, "symref", &show_symref_target, |
69 | N_("show underlying ref in addition to the object pointed by it")), | |
ba5f28bf TG |
70 | OPT_END() |
71 | }; | |
91a640ff | 72 | |
ba5f28bf TG |
73 | argc = parse_options(argc, argv, prefix, options, ls_remote_usage, |
74 | PARSE_OPT_STOP_AT_NON_OPTION); | |
75 | dest = argv[0]; | |
18705953 | 76 | |
ba5f28bf TG |
77 | if (argc > 1) { |
78 | int i; | |
79 | pattern = xcalloc(argc, sizeof(const char *)); | |
b4be7410 BW |
80 | for (i = 1; i < argc; i++) { |
81 | const char *glob; | |
ba5f28bf | 82 | pattern[i - 1] = xstrfmt("*/%s", argv[i]); |
b4be7410 BW |
83 | |
84 | glob = strchr(argv[i], '*'); | |
85 | if (glob) | |
86 | argv_array_pushf(&ref_prefixes, "%.*s", | |
87 | (int)(glob - argv[i]), argv[i]); | |
88 | else | |
89 | expand_ref_prefix(&ref_prefixes, argv[i]); | |
90 | } | |
18705953 | 91 | } |
2718ff09 | 92 | |
c1d45cf7 | 93 | remote = remote_get(dest); |
9c00de5a TRC |
94 | if (!remote) { |
95 | if (dest) | |
96 | die("bad repository '%s'", dest); | |
97 | die("No remote configured to list refs from."); | |
98 | } | |
c1d45cf7 | 99 | if (!remote->url_nr) |
7c2c6ee7 | 100 | die("remote %s has no configured URL", dest); |
45781adb UKK |
101 | |
102 | if (get_url) { | |
103 | printf("%s\n", *remote->url); | |
104 | return 0; | |
105 | } | |
106 | ||
fb0cc87e | 107 | transport = transport_get(remote, NULL); |
18f7c51c DB |
108 | if (uploadpack != NULL) |
109 | transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); | |
110 | ||
b4be7410 | 111 | ref = transport_get_remote_refs(transport, &ref_prefixes); |
27b4070e | 112 | if (transport_disconnect(transport)) |
18f7c51c | 113 | return 1; |
cefb2a5e TRC |
114 | |
115 | if (!dest && !quiet) | |
116 | fprintf(stderr, "From %s\n", *remote->url); | |
2ea7fe0d JH |
117 | for ( ; ref; ref = ref->next) { |
118 | if (!check_ref_type(ref, flags)) | |
119 | continue; | |
120 | if (!tail_match(pattern, ref->name)) | |
121 | continue; | |
99c08d4e TG |
122 | if (show_symref_target && ref->symref) |
123 | printf("ref: %s\t%s\n", ref->symref, ref->name); | |
124 | printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name); | |
a8724773 | 125 | status = 0; /* we found something */ |
18f7c51c | 126 | } |
a8724773 | 127 | return status; |
18705953 | 128 | } |