6 static const char ls_remote_usage
[] =
7 "git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
8 " [-q | --quiet] [--exit-code] [--get-url] [<repository> [<refs>...]]";
11 * Is there one among the list of patterns that match the tail part
14 static int tail_match(const char **pattern
, const char *path
)
17 char pathbuf
[PATH_MAX
];
20 return 1; /* no restriction */
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 (!wildmatch(p
, pathbuf
, 0, NULL
))
31 int cmd_ls_remote(int argc
, const char **argv
, const char *prefix
)
34 const char *dest
= NULL
;
39 const char *uploadpack
= NULL
;
40 const char **pattern
= NULL
;
42 struct remote
*remote
;
43 struct transport
*transport
;
44 const struct ref
*ref
;
46 if (argc
== 2 && !strcmp("-h", argv
[1]))
47 usage(ls_remote_usage
);
49 for (i
= 1; i
< argc
; i
++) {
50 const char *arg
= argv
[i
];
53 if (starts_with(arg
, "--upload-pack=")) {
54 uploadpack
= arg
+ 14;
57 if (starts_with(arg
, "--exec=")) {
61 if (!strcmp("--tags", arg
) || !strcmp("-t", arg
)) {
65 if (!strcmp("--heads", arg
) || !strcmp("-h", arg
)) {
69 if (!strcmp("--refs", arg
)) {
73 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
77 if (!strcmp("--get-url", arg
)) {
81 if (!strcmp("--exit-code", arg
)) {
82 /* return this code if no refs are reported */
86 usage(ls_remote_usage
);
95 pattern
= xcalloc(argc
- i
+ 1, sizeof(const char *));
96 for (j
= i
; j
< argc
; j
++)
97 pattern
[j
- i
] = xstrfmt("*/%s", argv
[j
]);
99 remote
= remote_get(dest
);
102 die("bad repository '%s'", dest
);
103 die("No remote configured to list refs from.");
106 die("remote %s has no configured URL", dest
);
109 printf("%s\n", *remote
->url
);
113 transport
= transport_get(remote
, NULL
);
114 if (uploadpack
!= NULL
)
115 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, uploadpack
);
117 ref
= transport_get_remote_refs(transport
);
118 if (transport_disconnect(transport
))
122 fprintf(stderr
, "From %s\n", *remote
->url
);
123 for ( ; ref
; ref
= ref
->next
) {
124 if (!check_ref_type(ref
, flags
))
126 if (!tail_match(pattern
, ref
->name
))
128 printf("%s %s\n", sha1_to_hex(ref
->old_sha1
), ref
->name
);
129 status
= 0; /* we found something */