Skip to content

Commit a026bde

Browse files
committed
Merge branch 'jk/prefix-filename'
Code clean-up with minor bugfixes. * jk/prefix-filename: bundle: use prefix_filename with bundle path prefix_filename: simplify windows #ifdef prefix_filename: return newly allocated string prefix_filename: drop length parameter prefix_filename: move docstring to header file hash-object: fix buffer reuse with --path in a subdirectory
2 parents 09fb535 + 3b754ee commit a026bde

17 files changed

+86
-75
lines changed

abspath.c

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

249-
/*
250-
* Unlike prefix_path, this should be used if the named file does
251-
* not have to interact with index entry; i.e. name of a random file
252-
* on the filesystem.
253-
*/
254-
const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
249+
char *prefix_filename(const char *pfx, const char *arg)
255250
{
256-
static struct strbuf path = STRBUF_INIT;
257-
#ifndef GIT_WINDOWS_NATIVE
258-
if (!pfx_len || is_absolute_path(arg))
259-
return arg;
260-
strbuf_reset(&path);
261-
strbuf_add(&path, pfx, pfx_len);
262-
strbuf_addstr(&path, arg);
263-
#else
264-
/* don't add prefix to absolute paths, but still replace '\' by '/' */
265-
strbuf_reset(&path);
266-
if (is_absolute_path(arg))
251+
struct strbuf path = STRBUF_INIT;
252+
size_t pfx_len = pfx ? strlen(pfx) : 0;
253+
254+
if (!pfx_len)
255+
; /* nothing to prefix */
256+
else if (is_absolute_path(arg))
267257
pfx_len = 0;
268-
else if (pfx_len)
258+
else
269259
strbuf_add(&path, pfx, pfx_len);
260+
270261
strbuf_addstr(&path, arg);
262+
#ifdef GIT_WINDOWS_NATIVE
271263
convert_slashes(path.buf + pfx_len);
272264
#endif
273-
return path.buf;
265+
return strbuf_detach(&path, NULL);
274266
}

apply.c

Lines changed: 6 additions & 5 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 = prefix_filename(state->prefix, *name);
20502050
free(old_name);
20512051
}
20522052

@@ -4805,6 +4805,7 @@ int apply_all_patches(struct apply_state *state,
48054805

48064806
for (i = 0; i < argc; i++) {
48074807
const char *arg = argv[i];
4808+
char *to_free = NULL;
48084809
int fd;
48094810

48104811
if (!strcmp(arg, "-")) {
@@ -4814,21 +4815,21 @@ int apply_all_patches(struct apply_state *state,
48144815
errs |= res;
48154816
read_stdin = 0;
48164817
continue;
4817-
} else if (0 < state->prefix_length)
4818-
arg = prefix_filename(state->prefix,
4819-
state->prefix_length,
4820-
arg);
4818+
} else
4819+
arg = to_free = prefix_filename(state->prefix, arg);
48214820

48224821
fd = open(arg, O_RDONLY);
48234822
if (fd < 0) {
48244823
error(_("can't open patch '%s': %s"), arg, strerror(errno));
48254824
res = -128;
4825+
free(to_free);
48264826
goto end;
48274827
}
48284828
read_stdin = 0;
48294829
set_default_whitespace_mode(state);
48304830
res = apply_patch(state, fd, arg, options);
48314831
close(fd);
4832+
free(to_free);
48324833
if (res < 0)
48334834
goto end;
48344835
errs |= res;

builtin/bundle.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,15 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)
2020
struct bundle_header header;
2121
const char *cmd, *bundle_file;
2222
int bundle_fd = -1;
23-
char buffer[PATH_MAX];
2423

2524
if (argc < 3)
2625
usage(builtin_bundle_usage);
2726

2827
cmd = argv[1];
29-
bundle_file = argv[2];
28+
bundle_file = prefix_filename(prefix, argv[2]);
3029
argc -= 2;
3130
argv += 2;
3231

33-
if (prefix && bundle_file[0] != '/') {
34-
snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
35-
bundle_file = buffer;
36-
}
37-
3832
memset(&header, 0, sizeof(header));
3933
if (strcmp(cmd, "create") && (bundle_fd =
4034
read_bundle_header(bundle_file, &header)) < 0)

builtin/config.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
527527
else if (given_config_source.file) {
528528
if (!is_absolute_path(given_config_source.file) && prefix)
529529
given_config_source.file =
530-
xstrdup(prefix_filename(prefix,
531-
strlen(prefix),
532-
given_config_source.file));
530+
prefix_filename(prefix, given_config_source.file);
533531
}
534532

535533
if (respect_includes == -1)

builtin/hash-object.c

Lines changed: 5 additions & 5 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 = prefix_filename(prefix, prefix_length, vpath);
116+
vpath = xstrdup(prefix_filename(prefix, vpath));
119117

120118
git_config(git_default_config, NULL);
121119

@@ -144,11 +142,13 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
144142

145143
for (i = 0 ; i < argc; i++) {
146144
const char *arg = argv[i];
145+
char *to_free = NULL;
147146

148-
if (0 <= prefix_length)
149-
arg = prefix_filename(prefix, prefix_length, arg);
147+
if (prefix)
148+
arg = to_free = prefix_filename(prefix, arg);
150149
hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
151150
flags, literally);
151+
free(to_free);
152152
}
153153

154154
if (stdin_paths)

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 prefix_filename(prefix, output_directory);
10891088
}
10901089

10911090
static const char * const builtin_format_patch_usage[] = {

builtin/mailinfo.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
static const char mailinfo_usage[] =
1212
"git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
1313

14-
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));
19-
}
20-
2114
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
2215
{
2316
const char *def_charset;
@@ -60,8 +53,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
6053
mi.input = stdin;
6154
mi.output = stdout;
6255

63-
msgfile = prefix_copy(prefix, argv[1]);
64-
patchfile = prefix_copy(prefix, argv[2]);
56+
msgfile = prefix_filename(prefix, argv[1]);
57+
patchfile = prefix_filename(prefix, argv[2]);
6558

6659
status = !!mailinfo(&mi, msgfile, patchfile);
6760
clear_mailinfo(&mi);

builtin/merge-file.c

Lines changed: 11 additions & 7 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,15 +64,19 @@ 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+
char *fname;
69+
int ret;
70+
7371
if (!names[i])
7472
names[i] = argv[i];
75-
if (read_mmfile(mmfs + i, fname))
73+
74+
fname = prefix_filename(prefix, argv[i]);
75+
ret = read_mmfile(mmfs + i, fname);
76+
free(fname);
77+
if (ret)
7678
return -1;
79+
7780
if (mmfs[i].size > MAX_XDIFF_SIZE ||
7881
buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
7982
return error("Cannot merge binary files: %s",
@@ -90,7 +93,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
9093

9194
if (ret >= 0) {
9295
const char *filename = argv[0];
93-
const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
96+
char *fpath = prefix_filename(prefix, argv[0]);
9497
FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
9598

9699
if (!f)
@@ -102,6 +105,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
102105
else if (fclose(f))
103106
ret = error_errno("Could not close %s", filename);
104107
free(result.ptr);
108+
free(fpath);
105109
}
106110

107111
if (ret > 127)

builtin/rev-parse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ 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+
char *fname = prefix_filename(prefix, arg);
232+
show(fname);
233+
free(fname);
234234
} else
235235
show(arg);
236236
return 1;

builtin/worktree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ static int add(int ac, const char **av, const char *prefix)
318318
{
319319
struct add_opts opts;
320320
const char *new_branch_force = NULL;
321-
const char *path, *branch;
321+
char *path;
322+
const char *branch;
322323
struct option options[] = {
323324
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
324325
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -338,7 +339,7 @@ static int add(int ac, const char **av, const char *prefix)
338339
if (ac < 1 || ac > 2)
339340
usage_with_options(worktree_usage, options);
340341

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

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

0 commit comments

Comments
 (0)