Skip to content

Commit 116fb64

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5980197 commit 116fb64

File tree

15 files changed

+23
-35
lines changed

15 files changed

+23
-35
lines changed

abspath.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ char *absolute_pathdup(const char *path)
246246
return strbuf_detach(&sb, NULL);
247247
}
248248

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)
250250
{
251251
static struct strbuf path = STRBUF_INIT;
252+
size_t pfx_len = pfx ? strlen(pfx) : 0;
253+
252254
#ifndef GIT_WINDOWS_NATIVE
253255
if (!pfx_len || is_absolute_path(arg))
254256
return arg;

apply.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ static void prefix_one(struct apply_state *state, char **name)
20462046
char *old_name = *name;
20472047
if (!old_name)
20482048
return;
2049-
*name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
2049+
*name = xstrdup(prefix_filename(state->prefix, *name));
20502050
free(old_name);
20512051
}
20522052

@@ -4815,9 +4815,7 @@ int apply_all_patches(struct apply_state *state,
48154815
read_stdin = 0;
48164816
continue;
48174817
} 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);
48214819

48224820
fd = open(arg, O_RDONLY);
48234821
if (fd < 0) {

builtin/config.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
528528
if (!is_absolute_path(given_config_source.file) && prefix)
529529
given_config_source.file =
530530
xstrdup(prefix_filename(prefix,
531-
strlen(prefix),
532531
given_config_source.file));
533532
}
534533

builtin/hash-object.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
102102
OPT_END()
103103
};
104104
int i;
105-
int prefix_length = -1;
106105
const char *errstr = NULL;
107106

108107
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)
113112
else
114113
prefix = setup_git_directory_gently(&nongit);
115114

116-
prefix_length = prefix ? strlen(prefix) : 0;
117115
if (vpath && prefix)
118-
vpath = xstrdup(prefix_filename(prefix, prefix_length, vpath));
116+
vpath = xstrdup(prefix_filename(prefix, vpath));
119117

120118
git_config(git_default_config, NULL);
121119

@@ -146,9 +144,8 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
146144
const char *arg = argv[i];
147145
char *to_free = NULL;
148146

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));
152149
hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
153150
flags, literally);
154151
free(to_free);

builtin/log.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,7 @@ static const char *set_outdir(const char *prefix, const char *output_directory)
10841084
if (!output_directory)
10851085
return prefix;
10861086

1087-
return xstrdup(prefix_filename(prefix, outdir_offset,
1088-
output_directory));
1087+
return xstrdup(prefix_filename(prefix, output_directory));
10891088
}
10901089

10911090
static const char * const builtin_format_patch_usage[] = {

builtin/mailinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static char *prefix_copy(const char *prefix, const char *filename)
1515
{
1616
if (!prefix || is_absolute_path(filename))
1717
return xstrdup(filename);
18-
return xstrdup(prefix_filename(prefix, strlen(prefix), filename));
18+
return xstrdup(prefix_filename(prefix, filename));
1919
}
2020

2121
int cmd_mailinfo(int argc, const char **argv, const char *prefix)

builtin/merge-file.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
2828
xmparam_t xmp = {{0}};
2929
int ret = 0, i = 0, to_stdout = 0;
3030
int quiet = 0;
31-
int prefixlen = 0;
3231
struct option options[] = {
3332
OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
3433
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)
6564
return error_errno("failed to redirect stderr to /dev/null");
6665
}
6766

68-
if (prefix)
69-
prefixlen = strlen(prefix);
70-
7167
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]);
7369
if (!names[i])
7470
names[i] = argv[i];
7571
if (read_mmfile(mmfs + i, fname))
@@ -90,7 +86,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
9086

9187
if (ret >= 0) {
9288
const char *filename = argv[0];
93-
const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
89+
const char *fpath = prefix_filename(prefix, argv[0]);
9490
FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
9591

9692
if (!f)

builtin/rev-parse.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ static int show_file(const char *arg, int output_prefix)
228228
if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
229229
if (output_prefix) {
230230
const char *prefix = startup_info->prefix;
231-
show(prefix_filename(prefix,
232-
prefix ? strlen(prefix) : 0,
233-
arg));
231+
show(prefix_filename(prefix, arg));
234232
} else
235233
show(arg);
236234
return 1;

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int add(int ac, const char **av, const char *prefix)
338338
if (ac < 1 || ac > 2)
339339
usage_with_options(worktree_usage, options);
340340

341-
path = prefix_filename(prefix, strlen(prefix), av[0]);
341+
path = prefix_filename(prefix, av[0]);
342342
branch = ac < 2 ? "HEAD" : av[1];
343343

344344
if (!strcmp(branch, "-"))

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ extern char *prefix_path_gently(const char *prefix, int len, int *remaining, con
540540
* The return value may point to static storage which will be overwritten by
541541
* further calls.
542542
*/
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);
544544

545545
extern int check_filename(const char *prefix, const char *name);
546546
extern void verify_filename(const char *prefix,

0 commit comments

Comments
 (0)