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

Fix filename verification when in a subdirectory

When we are in a subdirectory of a git archive, we need to take the prefix of that subdirectory into accoung when we verify filename arguments. Noted by Matthias Lederhofer This also uses the improved error reporting for all the other git commands that use the revision parsing interfaces, not just git-rev-parse. Also, it makes the error reporting for mixed filenames and argument flags clearer (you cannot put flags after the start of the pathname list). [jc: with fix to a trivial typo noticed by Timo Hirvonen] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Apr 26, 2006 at 10:15 UTC e23d0b4a4a55cc07e133905f0e9526b3550dd61b
4 files changed +29 -18
cache.h
+1
@@ -134,6 +134,7 @@ extern const char *setup_git_directory_gently(int *);
134 extern const char *setup_git_directory(void);
135 extern const char *prefix_path(const char *prefix, int len, const char *path);
136 extern const char *prefix_filename(const char *prefix, int len, const char *path);
137 +extern void verify_filename(const char *prefix, const char *name);
138
139 #define alloc_nr(x) (((x)+16)*3/2)
140
rev-parse.c
+2 -13
@@ -160,14 +160,6 @@ static int show_file(const char *arg)
160 return 0;
161 }
162
163 -static void die_badfile(const char *arg)
164 -{
165 - if (errno != ENOENT)
166 - die("'%s': %s", arg, strerror(errno));
167 - die("'%s' is ambiguous - revision name or file/directory name?\n"
168 - "Please put '--' before the list of filenames.", arg);
169 -}
170 -
163 int main(int argc, char **argv)
164 {
165 int i, as_is = 0, verify = 0;
@@ -177,14 +169,12 @@ int main(int argc, char **argv)
169 git_config(git_default_config);
170
171 for (i = 1; i < argc; i++) {
180 - struct stat st;
172 char *arg = argv[i];
173 char *dotdot;
174
175 if (as_is) {
176 if (show_file(arg) && as_is < 2)
186 - if (lstat(arg, &st) < 0)
187 - die_badfile(arg);
177 + verify_filename(prefix, arg);
178 continue;
179 }
180 if (!strcmp(arg,"-n")) {
@@ -350,8 +340,7 @@ int main(int argc, char **argv)
340 continue;
341 if (verify)
342 die("Needed a single revision");
353 - if (lstat(arg, &st) < 0)
354 - die_badfile(arg);
343 + verify_filename(prefix, arg);
344 }
345 show_default();
346 if (verify && revs_count != 1)
revision.c
+3 -5
@@ -675,17 +675,15 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
675 arg++;
676 }
677 if (get_sha1(arg, sha1) < 0) {
678 - struct stat st;
678 int j;
679
680 if (seen_dashdash || local_flags)
681 die("bad revision '%s'", arg);
682
683 /* If we didn't have a "--", all filenames must exist */
685 - for (j = i; j < argc; j++) {
686 - if (lstat(argv[j], &st) < 0)
687 - die("'%s': %s", argv[j], strerror(errno));
688 - }
684 + for (j = i; j < argc; j++)
685 + verify_filename(revs->prefix, argv[j]);
686 +
687 revs->prune_data = get_pathspec(revs->prefix, argv + i);
688 break;
689 }
setup.c
+23
@@ -62,6 +62,29 @@ const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
62 return path;
63 }
64
65 +/*
66 + * Verify a filename that we got as an argument for a pathspec
67 + * entry. Note that a filename that begins with "-" never verifies
68 + * as true, because even if such a filename were to exist, we want
69 + * it to be preceded by the "--" marker (or we want the user to
70 + * use a format like "./-filename")
71 + */
72 +void verify_filename(const char *prefix, const char *arg)
73 +{
74 + const char *name;
75 + struct stat st;
76 +
77 + if (*arg == '-')
78 + die("bad flag '%s' used after filename", arg);
79 + name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
80 + if (!lstat(name, &st))
81 + return;
82 + if (errno == ENOENT)
83 + die("ambiguous argument '%s': unknown revision or filename\n"
84 + "Use '--' to separate filenames from revisions", arg);
85 + die("'%s': %s", arg, strerror(errno));
86 +}
87 +
88 const char **get_pathspec(const char *prefix, const char **pathspec)
89 {
90 const char *entry = *pathspec;