prefix_filename: drop length parameter
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>
Jeff King committed
Mar 20, 2017 at 21:22 UTC
116fb64e439d3744d0f244a51d7a6d714b7703ae
15 files changed
+23
-35
abspath.c
+3
-1
@@ -246,9 +246,11 @@ char *absolute_pathdup(const char *path)
246
return strbuf_detach(&sb, NULL);
247
}
248
249
-const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
249
+const char *prefix_filename(const char *pfx, const char *arg)
250
{
251
static struct strbuf path = STRBUF_INIT;
252
+ size_t pfx_len = pfx ? strlen(pfx) : 0;
253
+
254
#ifndef GIT_WINDOWS_NATIVE
255
if (!pfx_len || is_absolute_path(arg))
256
return arg;
apply.c
+2
-4
@@ -2046,7 +2046,7 @@ static void prefix_one(struct apply_state *state, char **name)
2046
char *old_name = *name;
2047
if (!old_name)
2048
return;
2049
- *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
2049
+ *name = xstrdup(prefix_filename(state->prefix, *name));
2050
free(old_name);
2051
}
2052
@@ -4815,9 +4815,7 @@ int apply_all_patches(struct apply_state *state,
4815
read_stdin = 0;
4816
continue;
4817
} else if (0 < state->prefix_length)
4818
- arg = prefix_filename(state->prefix,
4819
- state->prefix_length,
4820
- arg);
4818
+ arg = prefix_filename(state->prefix, arg);
4819
4820
fd = open(arg, O_RDONLY);
4821
if (fd < 0) {
builtin/config.c
-1
@@ -528,7 +528,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
528
if (!is_absolute_path(given_config_source.file) && prefix)
529
given_config_source.file =
530
xstrdup(prefix_filename(prefix,
531
- strlen(prefix),
531
given_config_source.file));
532
}
533
builtin/hash-object.c
+3
-6
@@ -102,7 +102,6 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
102
OPT_END()
103
};
104
int i;
105
- int prefix_length = -1;
105
const char *errstr = NULL;
106
107
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)
112
else
113
prefix = setup_git_directory_gently(&nongit);
114
116
- prefix_length = prefix ? strlen(prefix) : 0;
115
if (vpath && prefix)
118
- vpath = xstrdup(prefix_filename(prefix, prefix_length, vpath));
116
+ vpath = xstrdup(prefix_filename(prefix, vpath));
117
118
git_config(git_default_config, NULL);
119
@@ -146,9 +144,8 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
144
const char *arg = argv[i];
145
char *to_free = NULL;
146
149
- if (0 <= prefix_length)
150
- arg = to_free =
151
- xstrdup(prefix_filename(prefix, prefix_length, arg));
147
+ if (prefix)
148
+ arg = to_free = xstrdup(prefix_filename(prefix, arg));
149
hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
150
flags, literally);
151
free(to_free);
builtin/log.c
+1
-2
@@ -1084,8 +1084,7 @@ static const char *set_outdir(const char *prefix, const char *output_directory)
1084
if (!output_directory)
1085
return prefix;
1086
1087
- return xstrdup(prefix_filename(prefix, outdir_offset,
1088
- output_directory));
1087
+ return xstrdup(prefix_filename(prefix, output_directory));
1088
}
1089
1090
static const char * const builtin_format_patch_usage[] = {
builtin/mailinfo.c
+1
-1
@@ -15,7 +15,7 @@ static char *prefix_copy(const char *prefix, const char *filename)
15
{
16
if (!prefix || is_absolute_path(filename))
17
return xstrdup(filename);
18
- return xstrdup(prefix_filename(prefix, strlen(prefix), filename));
18
+ return xstrdup(prefix_filename(prefix, filename));
19
}
20
21
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
builtin/merge-file.c
+2
-6
@@ -28,7 +28,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
28
xmparam_t xmp = {{0}};
29
int ret = 0, i = 0, to_stdout = 0;
30
int quiet = 0;
31
- int prefixlen = 0;
31
struct option options[] = {
32
OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
33
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)
64
return error_errno("failed to redirect stderr to /dev/null");
65
}
66
68
- if (prefix)
69
- prefixlen = strlen(prefix);
70
-
67
for (i = 0; i < 3; i++) {
72
- const char *fname = prefix_filename(prefix, prefixlen, argv[i]);
68
+ const char *fname = prefix_filename(prefix, argv[i]);
69
if (!names[i])
70
names[i] = argv[i];
71
if (read_mmfile(mmfs + i, fname))
@@ -90,7 +86,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
86
87
if (ret >= 0) {
88
const char *filename = argv[0];
93
- const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
89
+ const char *fpath = prefix_filename(prefix, argv[0]);
90
FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
91
92
if (!f)
builtin/rev-parse.c
+1
-3
@@ -228,9 +228,7 @@ static int show_file(const char *arg, int output_prefix)
228
if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
229
if (output_prefix) {
230
const char *prefix = startup_info->prefix;
231
- show(prefix_filename(prefix,
232
- prefix ? strlen(prefix) : 0,
233
- arg));
231
+ show(prefix_filename(prefix, arg));
232
} else
233
show(arg);
234
return 1;
builtin/worktree.c
+1
-1
@@ -338,7 +338,7 @@ static int add(int ac, const char **av, const char *prefix)
338
if (ac < 1 || ac > 2)
339
usage_with_options(worktree_usage, options);
340
341
- path = prefix_filename(prefix, strlen(prefix), av[0]);
341
+ path = prefix_filename(prefix, av[0]);
342
branch = ac < 2 ? "HEAD" : av[1];
343
344
if (!strcmp(branch, "-"))
cache.h
+1
-1
@@ -540,7 +540,7 @@ extern char *prefix_path_gently(const char *prefix, int len, int *remaining, con
540
* The return value may point to static storage which will be overwritten by
541
* further calls.
542
*/
543
-extern const char *prefix_filename(const char *prefix, int len, const char *path);
543
+extern const char *prefix_filename(const char *prefix, const char *path);
544
545
extern int check_filename(const char *prefix, const char *name);
546
extern void verify_filename(const char *prefix,
diff-no-index.c
+3
-4
@@ -236,7 +236,7 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
236
void diff_no_index(struct rev_info *revs,
237
int argc, const char **argv)
238
{
239
- int i, prefixlen;
239
+ int i;
240
const char *paths[2];
241
struct strbuf replacement = STRBUF_INIT;
242
const char *prefix = revs->prefix;
@@ -257,7 +257,6 @@ void diff_no_index(struct rev_info *revs,
257
}
258
}
259
260
- prefixlen = prefix ? strlen(prefix) : 0;
260
for (i = 0; i < 2; i++) {
261
const char *p = argv[argc - 2 + i];
262
if (!strcmp(p, "-"))
@@ -266,8 +265,8 @@ void diff_no_index(struct rev_info *revs,
265
* path that is "-", spell it as "./-".
266
*/
267
p = file_from_standard_input;
269
- else if (prefixlen)
270
- p = xstrdup(prefix_filename(prefix, prefixlen, p));
268
+ else if (prefix)
269
+ p = xstrdup(prefix_filename(prefix, p));
270
paths[i] = p;
271
}
272
diff.c
+2
-2
@@ -4023,7 +4023,7 @@ int diff_opt_parse(struct diff_options *options,
4023
else if (!strcmp(arg, "--pickaxe-regex"))
4024
options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
4025
else if ((argcount = short_opt('O', av, &optarg))) {
4026
- const char *path = prefix_filename(prefix, strlen(prefix), optarg);
4026
+ const char *path = prefix_filename(prefix, optarg);
4027
options->orderfile = xstrdup(path);
4028
return argcount;
4029
}
@@ -4071,7 +4071,7 @@ int diff_opt_parse(struct diff_options *options,
4071
else if (!strcmp(arg, "--no-function-context"))
4072
DIFF_OPT_CLR(options, FUNCCONTEXT);
4073
else if ((argcount = parse_long_opt("output", av, &optarg))) {
4074
- const char *path = prefix_filename(prefix, strlen(prefix), optarg);
4074
+ const char *path = prefix_filename(prefix, optarg);
4075
options->file = fopen(path, "w");
4076
if (!options->file)
4077
die_errno("Could not open '%s'", path);
parse-options.c
+1
-1
@@ -40,7 +40,7 @@ static void fix_filename(const char *prefix, const char **file)
40
if (!file || !*file || !prefix || is_absolute_path(*file)
41
|| !strcmp("-", *file))
42
return;
43
- *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
43
+ *file = xstrdup(prefix_filename(prefix, *file));
44
}
45
46
static int opt_command_mode_error(const struct option *opt,
setup.c
+1
-1
@@ -142,7 +142,7 @@ int check_filename(const char *prefix, const char *arg)
142
return 1;
143
name = arg + 2;
144
} else if (prefix)
145
- name = prefix_filename(prefix, strlen(prefix), arg);
145
+ name = prefix_filename(prefix, arg);
146
else
147
name = arg;
148
if (!lstat(name, &st))
worktree.c
+1
-1
@@ -254,7 +254,7 @@ struct worktree *find_worktree(struct worktree **list,
254
if ((wt = find_worktree_by_suffix(list, arg)))
255
return wt;
256
257
- arg = prefix_filename(prefix, strlen(prefix), arg);
257
+ arg = prefix_filename(prefix, arg);
258
path = real_pathdup(arg, 1);
259
for (; *list; list++)
260
if (!fspathcmp(path, real_path((*list)->path)))