check_filename(): refactor ":/" handling

We handle arguments with the ":/" pathspec magic specially, making sure the name exists at the top-level. We'll want to handle more pathspec magic in future patches, so let's do a little rearranging to make that easier. Instead of relying on an if/else cascade to avoid the prefix_filename() call, we'll just set prefix to NULL. Likewise, we'll get rid of the "name" variable entirely, and just push the "arg" pointer forward to skip past the magic. That means by the time we get to the prefix-handling, we're set up appropriately whether we saw ":/" or not. Note that this does impact the final error message we produce when stat() fails, as it shows "arg" (which we'll have modified to skip magic and include the prefix). This is a good thing; the original message would say something like "failed to stat ':/foo'", which is confusing (we tried to stat "foo"). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 26, 2017 at 15:07 UTC a08cbcda1782993d83cf8763a394dab24e2c52b3
1 file changed +8 -7
setup.c
+8 -7
@@ -134,19 +134,20 @@ int path_inside_repo(const char *prefix, const char *path)
134
135 int check_filename(const char *prefix, const char *arg)
136 {
137 - const char *name;
137 char *to_free = NULL;
138 struct stat st;
139
140 if (starts_with(arg, ":/")) {
141 if (arg[2] == '\0') /* ":/" is root dir, always exists */
142 return 1;
144 - name = arg + 2;
145 - } else if (prefix)
146 - name = to_free = prefix_filename(prefix, arg);
147 - else
148 - name = arg;
149 - if (!lstat(name, &st)) {
143 + arg += 2;
144 + prefix = NULL;
145 + }
146 +
147 + if (prefix)
148 + arg = to_free = prefix_filename(prefix, arg);
149 +
150 + if (!lstat(arg, &st)) {
151 free(to_free);
152 return 1; /* file exists */
153 }