prefix_filename: drop length parameter
authorJeff King <peff@peff.net>
Tue, 21 Mar 2017 01:22:28 +0000 (21:22 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Mar 2017 18:12:53 +0000 (11:12 -0700)
This function takes the prefix as a ptr/len pair, but in
every caller the length is exactly strlen(ptr). Let's
simplify the interface and just take the string. This saves
callers specifying it (and in some cases handling a NULL
prefix).

In a handful of cases we had the length already without
calling strlen, so this is technically slower. But it's not
likely to matter (after all, if the prefix is non-empty
we'll allocate and copy it into a buffer anyway).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
15 files changed:
abspath.c
apply.c
builtin/config.c
builtin/hash-object.c
builtin/log.c
builtin/mailinfo.c
builtin/merge-file.c
builtin/rev-parse.c
builtin/worktree.c
cache.h
diff-no-index.c
diff.c
parse-options.c
setup.c
worktree.c
index fd30aff08fdae93ecb7283b4e145cab41fd2f9c9..c6f480993d7cb404ac5d275db86be6f64a325a6b 100644 (file)
--- a/abspath.c
+++ b/abspath.c
@@ -246,9 +246,11 @@ char *absolute_pathdup(const char *path)
        return strbuf_detach(&sb, NULL);
 }
 
-const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
+const char *prefix_filename(const char *pfx, const char *arg)
 {
        static struct strbuf path = STRBUF_INIT;
+       size_t pfx_len = pfx ? strlen(pfx) : 0;
+
 #ifndef GIT_WINDOWS_NATIVE
        if (!pfx_len || is_absolute_path(arg))
                return arg;
diff --git a/apply.c b/apply.c
index 0e2caeab9cc523364e0cfbb8cbbb5aba1a0a58ee..b8bd5a4befa410ffa96ea638c2eb59f51c2b2c96 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -2046,7 +2046,7 @@ static void prefix_one(struct apply_state *state, char **name)
        char *old_name = *name;
        if (!old_name)
                return;
-       *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
+       *name = xstrdup(prefix_filename(state->prefix, *name));
        free(old_name);
 }
 
@@ -4815,9 +4815,7 @@ int apply_all_patches(struct apply_state *state,
                        read_stdin = 0;
                        continue;
                } else if (0 < state->prefix_length)
-                       arg = prefix_filename(state->prefix,
-                                             state->prefix_length,
-                                             arg);
+                       arg = prefix_filename(state->prefix, arg);
 
                fd = open(arg, O_RDONLY);
                if (fd < 0) {
index 05843a0f96e4dc0dbf9fbc7310039794b57947e7..74f6c34d11ec720b7faf8212b9b61ec0648cf4aa 100644 (file)
@@ -528,7 +528,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                if (!is_absolute_path(given_config_source.file) && prefix)
                        given_config_source.file =
                                xstrdup(prefix_filename(prefix,
-                                                       strlen(prefix),
                                                        given_config_source.file));
        }
 
index 56df77b0c2a299fcba56a2542f7adb34869ced9f..2ea36909d2443dc704c035d57840f804fb6f6401 100644 (file)
@@ -102,7 +102,6 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
        int i;
-       int prefix_length = -1;
        const char *errstr = NULL;
 
        argc = parse_options(argc, argv, NULL, hash_object_options,
@@ -113,9 +112,8 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
        else
                prefix = setup_git_directory_gently(&nongit);
 
-       prefix_length = prefix ? strlen(prefix) : 0;
        if (vpath && prefix)
-               vpath = xstrdup(prefix_filename(prefix, prefix_length, vpath));
+               vpath = xstrdup(prefix_filename(prefix, vpath));
 
        git_config(git_default_config, NULL);
 
@@ -146,9 +144,8 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
                const char *arg = argv[i];
                char *to_free = NULL;
 
-               if (0 <= prefix_length)
-                       arg = to_free =
-                               xstrdup(prefix_filename(prefix, prefix_length, arg));
+               if (prefix)
+                       arg = to_free = xstrdup(prefix_filename(prefix, arg));
                hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
                            flags, literally);
                free(to_free);
index 281af8c1ecb04c7be05d9a17a1d5f3bd43420c85..bfdc7a23d36473a271a1296b67ed0c850407c6bc 100644 (file)
@@ -1084,8 +1084,7 @@ static const char *set_outdir(const char *prefix, const char *output_directory)
        if (!output_directory)
                return prefix;
 
-       return xstrdup(prefix_filename(prefix, outdir_offset,
-                                      output_directory));
+       return xstrdup(prefix_filename(prefix, output_directory));
 }
 
 static const char * const builtin_format_patch_usage[] = {
index e3b62f2fc744d340de5e3d9efad12b5de93cd55c..681f07f54d792af1be9fe9ed4636d8ce947cd75b 100644 (file)
@@ -15,7 +15,7 @@ static char *prefix_copy(const char *prefix, const char *filename)
 {
        if (!prefix || is_absolute_path(filename))
                return xstrdup(filename);
-       return xstrdup(prefix_filename(prefix, strlen(prefix), filename));
+       return xstrdup(prefix_filename(prefix, filename));
 }
 
 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
index 13e22a2f0be73ec7e521a02d1f36c414e77a8700..63cd94358770326e109df8f2b628aa6b8c97bc40 100644 (file)
@@ -28,7 +28,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
        xmparam_t xmp = {{0}};
        int ret = 0, i = 0, to_stdout = 0;
        int quiet = 0;
-       int prefixlen = 0;
        struct option options[] = {
                OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
                OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
@@ -65,11 +64,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
                        return error_errno("failed to redirect stderr to /dev/null");
        }
 
-       if (prefix)
-               prefixlen = strlen(prefix);
-
        for (i = 0; i < 3; i++) {
-               const char *fname = prefix_filename(prefix, prefixlen, argv[i]);
+               const char *fname = prefix_filename(prefix, argv[i]);
                if (!names[i])
                        names[i] = argv[i];
                if (read_mmfile(mmfs + i, fname))
@@ -90,7 +86,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 
        if (ret >= 0) {
                const char *filename = argv[0];
-               const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
+               const char *fpath = prefix_filename(prefix, argv[0]);
                FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
 
                if (!f)
index 2549643267440c5742690e3b808943de6fad19af..c8035331e21579df39c6a76aca8a8299c6b9b5df 100644 (file)
@@ -228,9 +228,7 @@ static int show_file(const char *arg, int output_prefix)
        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
                if (output_prefix) {
                        const char *prefix = startup_info->prefix;
-                       show(prefix_filename(prefix,
-                                            prefix ? strlen(prefix) : 0,
-                                            arg));
+                       show(prefix_filename(prefix, arg));
                } else
                        show(arg);
                return 1;
index 831fe058a53da95643b1a93038f576d5bffea7ef..e38325e44bb567a63b622418a573f2296c91eea3 100644 (file)
@@ -338,7 +338,7 @@ static int add(int ac, const char **av, const char *prefix)
        if (ac < 1 || ac > 2)
                usage_with_options(worktree_usage, options);
 
-       path = prefix_filename(prefix, strlen(prefix), av[0]);
+       path = prefix_filename(prefix, av[0]);
        branch = ac < 2 ? "HEAD" : av[1];
 
        if (!strcmp(branch, "-"))
diff --git a/cache.h b/cache.h
index a01668fc42e72815489b552f8960118353f2cfd1..0b53aef0ed4b761aed47e8e930e1237666bac287 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -540,7 +540,7 @@ extern char *prefix_path_gently(const char *prefix, int len, int *remaining, con
  * The return value may point to static storage which will be overwritten by
  * further calls.
  */
-extern const char *prefix_filename(const char *prefix, int len, const char *path);
+extern const char *prefix_filename(const char *prefix, const char *path);
 
 extern int check_filename(const char *prefix, const char *name);
 extern void verify_filename(const char *prefix,
index df762fd0f7d1ec74175a42de07b064107d3a601a..5f7317ced9ec1ab06d61d04fdb2c3526d794249a 100644 (file)
@@ -236,7 +236,7 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
 void diff_no_index(struct rev_info *revs,
                   int argc, const char **argv)
 {
-       int i, prefixlen;
+       int i;
        const char *paths[2];
        struct strbuf replacement = STRBUF_INIT;
        const char *prefix = revs->prefix;
@@ -257,7 +257,6 @@ void diff_no_index(struct rev_info *revs,
                }
        }
 
-       prefixlen = prefix ? strlen(prefix) : 0;
        for (i = 0; i < 2; i++) {
                const char *p = argv[argc - 2 + i];
                if (!strcmp(p, "-"))
@@ -266,8 +265,8 @@ void diff_no_index(struct rev_info *revs,
                         * path that is "-", spell it as "./-".
                         */
                        p = file_from_standard_input;
-               else if (prefixlen)
-                       p = xstrdup(prefix_filename(prefix, prefixlen, p));
+               else if (prefix)
+                       p = xstrdup(prefix_filename(prefix, p));
                paths[i] = p;
        }
 
diff --git a/diff.c b/diff.c
index a628ac3a95108ca79aef0b41b7051ab2fcbe693a..70870b4b6963d55472b7f9c6232f9a194bddd83a 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -4023,7 +4023,7 @@ int diff_opt_parse(struct diff_options *options,
        else if (!strcmp(arg, "--pickaxe-regex"))
                options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
        else if ((argcount = short_opt('O', av, &optarg))) {
-               const char *path = prefix_filename(prefix, strlen(prefix), optarg);
+               const char *path = prefix_filename(prefix, optarg);
                options->orderfile = xstrdup(path);
                return argcount;
        }
@@ -4071,7 +4071,7 @@ int diff_opt_parse(struct diff_options *options,
        else if (!strcmp(arg, "--no-function-context"))
                DIFF_OPT_CLR(options, FUNCCONTEXT);
        else if ((argcount = parse_long_opt("output", av, &optarg))) {
-               const char *path = prefix_filename(prefix, strlen(prefix), optarg);
+               const char *path = prefix_filename(prefix, optarg);
                options->file = fopen(path, "w");
                if (!options->file)
                        die_errno("Could not open '%s'", path);
index 4fbe924a5de27d04b271a11a2ae040e817483951..ba6cc30b26aebdcd1a9a14b6e7a2ad4a481ab128 100644 (file)
@@ -40,7 +40,7 @@ static void fix_filename(const char *prefix, const char **file)
        if (!file || !*file || !prefix || is_absolute_path(*file)
            || !strcmp("-", *file))
                return;
-       *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
+       *file = xstrdup(prefix_filename(prefix, *file));
 }
 
 static int opt_command_mode_error(const struct option *opt,
diff --git a/setup.c b/setup.c
index 64f922a9378cc48c72b9173e7c45296bdb885678..a76379e0ceaa688322da8f07c02350cfbcba7ccf 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -142,7 +142,7 @@ int check_filename(const char *prefix, const char *arg)
                        return 1;
                name = arg + 2;
        } else if (prefix)
-               name = prefix_filename(prefix, strlen(prefix), arg);
+               name = prefix_filename(prefix, arg);
        else
                name = arg;
        if (!lstat(name, &st))
index fa7bc67a50a6d52116b0ca994b473668998f1c3f..42dd3d52b0963445a304062feaba79a64ecca0ef 100644 (file)
@@ -254,7 +254,7 @@ struct worktree *find_worktree(struct worktree **list,
        if ((wt = find_worktree_by_suffix(list, arg)))
                return wt;
 
-       arg = prefix_filename(prefix, strlen(prefix), arg);
+       arg = prefix_filename(prefix, arg);
        path = real_pathdup(arg, 1);
        for (; *list; list++)
                if (!fspathcmp(path, real_path((*list)->path)))