Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
[git/dkf.git] / builtin-ls-remote.c
blob6dd31d1dd6c14677f91e8a0a941fb0f873b4c1fc
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 /*
10 * Is there one among the list of patterns that match the tail part
11 * of the path?
13 static int tail_match(const char **pattern, const char *path)
15 const char *p;
16 char pathbuf[PATH_MAX];
18 if (!pattern)
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))
25 return 1;
27 return 0;
30 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
32 int i;
33 const char *dest = NULL;
34 int nongit = 0;
35 unsigned flags = 0;
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];
48 if (*arg == '-') {
49 if (!prefixcmp(arg, "--upload-pack=")) {
50 uploadpack = arg + 14;
51 continue;
53 if (!prefixcmp(arg, "--exec=")) {
54 uploadpack = arg + 7;
55 continue;
57 if (!strcmp("--tags", arg) || !strcmp("-t", arg)) {
58 flags |= REF_TAGS;
59 continue;
61 if (!strcmp("--heads", arg) || !strcmp("-h", arg)) {
62 flags |= REF_HEADS;
63 continue;
65 if (!strcmp("--refs", arg)) {
66 flags |= REF_NORMAL;
67 continue;
69 usage(ls_remote_usage);
71 dest = arg;
72 i++;
73 break;
76 if (!dest)
77 usage(ls_remote_usage);
79 if (argv[i]) {
80 int j;
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]);
86 pattern[j - i] = p;
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);
98 if (!ref)
99 return 1;
101 for ( ; ref; ref = ref->next) {
102 if (!check_ref_type(ref, flags))
103 continue;
104 if (!tail_match(pattern, ref->name))
105 continue;
106 printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
108 return 0;