6 static const char ls_remote_usage
[] =
7 "git-ls-remote [--upload-pack=<git-upload-pack>] [<host>:]<directory>";
10 * Is there one among the list of patterns that match the tail part
13 static int tail_match(const char **pattern
, const char *path
)
16 char pathbuf
[PATH_MAX
];
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))
30 int cmd_ls_remote(int argc
, const char **argv
, const char *prefix
)
33 const char *dest
= NULL
;
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
];
49 if (!prefixcmp(arg
, "--upload-pack=")) {
50 uploadpack
= arg
+ 14;
53 if (!prefixcmp(arg
, "--exec=")) {
57 if (!strcmp("--tags", arg
) || !strcmp("-t", arg
)) {
61 if (!strcmp("--heads", arg
) || !strcmp("-h", arg
)) {
65 if (!strcmp("--refs", arg
)) {
69 usage(ls_remote_usage
);
77 usage(ls_remote_usage
);
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
]);
89 remote
= nongit
? NULL
: remote_get(dest
);
90 if (remote
&& !remote
->url_nr
)
91 die("remote %s has no configured URL", dest
);
92 transport
= transport_get(remote
, remote
? remote
->url
[0] : dest
);
93 if (uploadpack
!= NULL
)
94 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, uploadpack
);
96 ref
= transport_get_remote_refs(transport
);
97 transport_disconnect(transport
);
102 for ( ; ref
; ref
= ref
->next
) {
103 if (!check_ref_type(ref
, flags
))
105 if (!tail_match(pattern
, ref
->name
))
107 printf("%s %s\n", sha1_to_hex(ref
->old_sha1
), ref
->name
);