@matheusbsbs / matheus-bagliano / commits / af13cdf298
Fork 0

Be more careful about reference parsing

This does two things: - we don't allow "." and ".." as components of a refname. Thus get_sha1() will not accept "./refname" as being the same as "refname" any more. - git-rev-parse stops doing revision translation after seeing a pathname, to match the brhaviour of all the tools (once we see a pathname, everything else will also be parsed as a pathname). Basically, if you did git log * and "gitk" was somewhere in the "*", we don't want to replace the filename "gitk" with the SHA1 of the branch with the same name. Of course, if there is any change of ambiguity, you should always use "--" to make it explicit what are filenames and what are revisions, but this makes the normal cases sane. The refname rule also means that instead of the "--", you can do the same thing we're used to doing with filenames that start with a slash: use "./filename" instead, and now it's a filename, not an option (and not a revision). So "git log ./*.c" is now actually a perfectly valid thing to do, even if the first C-file might have the same name as a branch. Trivial test: git-rev-parse gitk ./gitk gitk should output something like 9843c3074dfbf57117565f6b7c93e3e6812857ee ./gitk gitk where the "./gitk" isn't seen as a revision, and the second "gitk" is a filename simply because we've seen filenames already, and thus stopped doing revision parsing. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Oct 28, 2005 at 12:41 UTC af13cdf298b927d171a58ec30c885d2a9c9bf727
2 files changed +28
rev-parse.c
+1
@@ -292,6 +292,7 @@ int main(int argc, char **argv)
292 }
293 if (verify)
294 die("Needed a single revision");
295 + as_is = 1;
296 show_file(arg);
297 }
298 show_default();
sha1_name.c
+27
@@ -203,6 +203,29 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
203 return NULL;
204 }
205
206 +static int ambiguous_path(const char *path)
207 +{
208 + int slash = 1;
209 +
210 + for (;;) {
211 + switch (*path++) {
212 + case '\0':
213 + break;
214 + case '/':
215 + if (slash)
216 + break;
217 + slash = 1;
218 + continue;
219 + case '.':
220 + continue;
221 + default:
222 + slash = 0;
223 + continue;
224 + }
225 + return slash;
226 + }
227 +}
228 +
229 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
230 {
231 static const char *prefix[] = {
@@ -217,6 +240,10 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
240 if (len == 40 && !get_sha1_hex(str, sha1))
241 return 0;
242
243 + /* Accept only unambiguous ref paths. */
244 + if (ambiguous_path(str))
245 + return -1;
246 +
247 for (p = prefix; *p; p++) {
248 char *pathname = git_path("%s/%.*s", *p, len, str);
249 if (!read_ref(pathname, sha1))