Do not skip prefix_filename() when there is no prefix
PATCH 1/1 Do not skip prefix_filename() when there is no prefix
On UNIX, when there is no prefix, prefix_filename() returns the input string as-is. Avoiding it only makes sense before 9e5f5d4 (prefix_filename():
safely handle the case where pfx_len=0 - 2010-10-17) because the function back then could try to dereference a NULL pointer.
On Windows, even when there is no prefix, prefix_filename() can still do some slash conversion. Arguably it's not the best place for that (too subtle and probably can't cover all paths) but it's how it is.
Let's always call prefix_filename() to make Windows users a tiny bit happier.
---
I left out worktree.c on purpose because that call site is handled separately [1]. In fact this patch is the result of Junio's comment on [1] (its earlier iteration).
[1] http://mid.gmane.org/[email protected]
builtin/apply.c | 2 +- builtin/hash-object.c | 2 +- diff-no-index.c | 2 +- parse-options.c | 9 ++++++--- setup.c | 8 ++++---- 5 files changed, 13 insertions(+), 10 deletions(-)
builtin/apply.c +1 -1
@@ -4615,7 +4615,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_) errs |=apply_patch (0 ,"<stdin>" , options); read_stdin =0 ;continue ; - }else if (0 < prefix_length) + }else arg =prefix_filename (prefix, prefix_length, arg); fd =open (arg,O_RDONLY );
builtin/hash-object.c +1 -1
@@ -110,7 +110,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)if (flags &HASH_WRITE_OBJECT ) { prefix =setup_git_directory (); prefix_length = prefix ?strlen (prefix) :0 ; -if (vpath && prefix) +if (vpath) vpath =prefix_filename (prefix, prefix_length, vpath); }
diff-no-index.c +1 -1
@@ -268,7 +268,7 @@ void diff_no_index(struct rev_info *revs,* path that is "-", spell it as "./-". */ p = file_from_standard_input; - else if (prefixlen) + else p =xstrdup (prefix_filename (prefix, prefixlen, p)); paths[i] = p; }
parse-options.c +6 -3
@@ -37,10 +37,13 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,static void fix_filename (const char *prefix,const char **file) { -if (!file || !*file || !prefix ||is_absolute_path (*file) - || !strcmp ("-" , *file)) +int prefix_length; + +if (!file || !*file ||is_absolute_path (*file) || !strcmp ("-" , *file))return ; - *file =xstrdup (prefix_filename (prefix,strlen (prefix), *file)); + + prefix_length = prefix ?strlen (prefix) :0 ; + *file =xstrdup (prefix_filename (prefix, prefix_length, *file)); } static int opt_command_mode_error(const struct option *opt,
setup.c +4 -4
@@ -141,10 +141,10 @@ int check_filename(const char *prefix, const char *arg)if (arg[2 ] =='\0' )/* ":/" is root dir, always exists */ return 1 ; name = arg +2 ; - }else if (prefix) - name =prefix_filename (prefix,strlen (prefix), arg); -else - name = arg; + }else { +int prefix_length = prefix ?strlen (prefix) :0 ; + name =prefix_filename (prefix, prefix_length, arg); + }if (!lstat (name, &st))return 1 ;/* file exists */ if (errno ==ENOENT || errno ==ENOTDIR )
Signature · 1 line
2.8.2.524.g6ff3d78