summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Jeff King <peff@peff.net>2017-05-26 15:07:31 -0400
committerLibravatar Junio C Hamano <gitster@pobox.com>2017-05-29 11:36:54 +0900
commita08cbcda1782993d83cf8763a394dab24e2c52b3 (patch)
tree47d1648ed9d0150353644bd46c549be69bcf4915
parentt4208: add check for ":/" without matching file (diff)
downloadtgif-a08cbcda1782993d83cf8763a394dab24e2c52b3.tar.xz
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>
-rw-r--r--setup.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/setup.c b/setup.c
index 0309c27821..000ffa810e 100644
--- a/setup.c
+++ b/setup.c
@@ -134,19 +134,20 @@ int path_inside_repo(const char *prefix, const char *path)
int check_filename(const char *prefix, const char *arg)
{
- const char *name;
char *to_free = NULL;
struct stat st;
if (starts_with(arg, ":/")) {
if (arg[2] == '\0') /* ":/" is root dir, always exists */
return 1;
- name = arg + 2;
- } else if (prefix)
- name = to_free = prefix_filename(prefix, arg);
- else
- name = arg;
- if (!lstat(name, &st)) {
+ arg += 2;
+ prefix = NULL;
+ }
+
+ if (prefix)
+ arg = to_free = prefix_filename(prefix, arg);
+
+ if (!lstat(arg, &st)) {
free(to_free);
return 1; /* file exists */
}