Re-fix ls-remote
An earlier attempt in 2ea7fe0 (ls-remote: resurrect pattern limit support) forgot that the user string can also be a glob. This should finally fix it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Dec 9, 2007 at 12:16 UTC
3d3c4f5d767b33cdaa7557ab009ab1194ff4a8a3
1 file changed
+21
-18
builtin-ls-remote.c
+21
-18
@@ -7,30 +7,22 @@ static const char ls_remote_usage[] =
7
"git-ls-remote [--upload-pack=<git-upload-pack>] [<host>:]<directory>";
8
9
/*
10
- * pattern is a list of tail-part of accepted refnames. Is there one
11
- * among them that is a suffix of the path? Directory boundary must
12
- * be honored when checking this match. IOW, patterns "master" and
13
- * "sa/master" both match path "refs/hold/sa/master". On the other
14
- * hand, path "refs/hold/foosa/master" is matched by "master" but not
15
- * by "sa/master".
10
+ * Is there one among the list of patterns that match the tail part
11
+ * of the path?
12
*/
17
-
13
static int tail_match(const char **pattern, const char *path)
14
{
20
- int pathlen;
15
const char *p;
16
+ char pathbuf[PATH_MAX];
17
23
- if (!*pattern)
18
+ if (!pattern)
19
return 1; /* no restriction */
20
26
- for (pathlen = strlen(path); (p = *pattern); pattern++) {
27
- int pfxlen = pathlen - strlen(p);
28
- if (pfxlen < 0)
29
- continue; /* pattern is longer, will never match */
30
- if (strcmp(path + pfxlen, p))
31
- continue; /* no tail match */
32
- if (!pfxlen || path[pfxlen - 1] == '/')
33
- return 1; /* fully match at directory boundary */
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;
26
}
27
return 0;
28
}
@@ -77,12 +69,23 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
69
usage(ls_remote_usage);
70
}
71
dest = arg;
72
+ i++;
73
break;
74
}
75
76
if (!dest)
77
usage(ls_remote_usage);
85
- pattern = argv + i + 1;
78
+
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;
87
+ }
88
+ }
89
remote = nongit ? NULL : remote_get(dest);
90
if (remote && !remote->url_nr)
91
die("remote %s has no configured URL", dest);