setup: stop using `the_repository` in `prefix_path()`

Stop using `the_repository` in `prefix_path()` and instead accept the
repository as a parameter. The injection of `the_repository` is thus
bumped one level higher, where callers now pass it in explicitly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Patrick Steinhardt 2026-05-19 11:52:08 +02:00 committed by Junio C Hamano
parent 8da5ecdb4d
commit 2c46e933fa
12 changed files with 26 additions and 23 deletions

View File

@ -708,7 +708,7 @@ static unsigned parse_score(const char *arg)

static char *add_prefix(const char *prefix, const char *path)
{
return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
return prefix_path(the_repository, prefix, prefix ? strlen(prefix) : 0, path);
}

static int git_blame_config(const char *var, const char *value,

View File

@ -67,7 +67,7 @@ static void check_attr(const char *prefix, struct attr_check *check,

{
char *full_path =
prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
prefix_path(the_repository, prefix, prefix ? strlen(prefix) : 0, file);

if (collect_all) {
git_all_attrs(the_repository->index, full_path, check);

View File

@ -303,7 +303,7 @@ int cmd_checkout_index(int argc,
die("git checkout-index: don't mix '--all' and explicit filenames");
if (read_from_stdin)
die("git checkout-index: don't mix '--stdin' and explicit filenames");
p = prefix_path(prefix, prefix_length, arg);
p = prefix_path(repo, prefix, prefix_length, arg);
err |= checkout_file(repo->index, p, prefix);
free(p);
}
@ -325,7 +325,7 @@ int cmd_checkout_index(int argc,
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
p = prefix_path(prefix, prefix_length, buf.buf);
p = prefix_path(repo, prefix, prefix_length, buf.buf);
err |= checkout_file(repo->index, p, prefix);
free(p);
}

View File

@ -71,7 +71,7 @@ static void internal_prefix_pathspec(struct strvec *out,

trimmed = xmemdupz(pathspec[i], to_copy);
maybe_basename = (flags & DUP_BASENAME) ? basename(trimmed) : trimmed;
prefixed_path = prefix_path(prefix, prefixlen, maybe_basename);
prefixed_path = prefix_path(the_repository, prefix, prefixlen, maybe_basename);
strvec_push(out, prefixed_path);

free(prefixed_path);
@ -394,7 +394,8 @@ dir_check:
for (j = 0; j < last - first; j++) {
const struct cache_entry *ce = the_repository->index->cache[first + j];
const char *path = ce->name;
char *prefixed_path = prefix_path(dst_with_slash, dst_with_slash_len, path + length + 1);
char *prefixed_path = prefix_path(the_repository, dst_with_slash,
dst_with_slash_len, path + length + 1);

strvec_push(&sources, path);
strvec_push(&destinations, prefixed_path);

View File

@ -735,7 +735,8 @@ static void sanitize_paths(struct repository *repo,
int prefix_len = strlen(prefix);

for (i = 0; i < args->nr; i++) {
char *prefixed_path = prefix_path(prefix, prefix_len, args->v[i]);
char *prefixed_path = prefix_path(the_repository, prefix,
prefix_len, args->v[i]);
strvec_replace(args, i, prefixed_path);
free(prefixed_path);
}

View File

@ -655,7 +655,7 @@ static int do_unresolve(int ac, const char **av,

for (i = 1; i < ac; i++) {
const char *arg = av[i];
char *p = prefix_path(prefix, prefix_length, arg);
char *p = prefix_path(the_repository, prefix, prefix_length, arg);
err |= unresolve_one(p);
free(p);
}
@ -1158,7 +1158,7 @@ int cmd_update_index(int argc,
}

setup_work_tree();
p = prefix_path(prefix, prefix_length, path);
p = prefix_path(the_repository, prefix, prefix_length, path);
update_one(p);
if (set_executable_bit)
chmod_path(set_executable_bit, p);
@ -1208,7 +1208,7 @@ int cmd_update_index(int argc,
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
p = prefix_path(prefix, prefix_length, buf.buf);
p = prefix_path(the_repository, prefix, prefix_length, buf.buf);
update_one(p);
if (set_executable_bit)
chmod_path(set_executable_bit, p);

View File

@ -589,7 +589,7 @@ parse_lines(struct repository *r, struct commit *commit,
range_part = xstrndup(item->string, name_part - item->string);
name_part++;

full_name = prefix_path(prefix, prefix ? strlen(prefix) : 0,
full_name = prefix_path(r, prefix, prefix ? strlen(prefix) : 0,
name_part);

spec = alloc_filespec(full_name);

View File

@ -1707,7 +1707,7 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
die(_("relative path syntax can't be used outside working tree"));

/* die() inside prefix_path() if resolved path is outside worktree */
return prefix_path(startup_info->prefix,
return prefix_path(the_repository, startup_info->prefix,
startup_info->prefix ? strlen(startup_info->prefix) : 0,
rel);
}

View File

@ -486,7 +486,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
match = xstrdup(copyfrom);
prefixlen = 0;
} else {
match = prefix_path_gently(prefix, prefixlen,
match = prefix_path_gently(the_repository, prefix, prefixlen,
&prefixlen, copyfrom);
if (!match) {
const char *hint_path;

15
setup.c
View File

@ -117,7 +117,8 @@ static int abspath_part_inside_repo(struct repository *repo, char *path)
* ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
* `pwd`/../bar -> sub1/bar (no remaining prefix)
*/
char *prefix_path_gently(const char *prefix, int len,
char *prefix_path_gently(struct repository *repo,
const char *prefix, int len,
int *remaining_prefix, const char *path)
{
const char *orig = path;
@ -130,7 +131,7 @@ char *prefix_path_gently(const char *prefix, int len,
free(sanitized);
return NULL;
}
if (abspath_part_inside_repo(the_repository, sanitized)) {
if (abspath_part_inside_repo(repo, sanitized)) {
free(sanitized);
return NULL;
}
@ -146,13 +147,13 @@ char *prefix_path_gently(const char *prefix, int len,
return sanitized;
}

char *prefix_path(const char *prefix, int len, const char *path)
char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path)
{
char *r = prefix_path_gently(prefix, len, NULL, path);
char *r = prefix_path_gently(repo, prefix, len, NULL, path);
if (!r) {
const char *hint_path = repo_get_work_tree(the_repository);
const char *hint_path = repo_get_work_tree(repo);
if (!hint_path)
hint_path = repo_get_git_dir(the_repository);
hint_path = repo_get_git_dir(repo);
die(_("'%s' is outside repository at '%s'"), path,
absolute_path(hint_path));
}
@ -162,7 +163,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
int path_inside_repo(const char *prefix, const char *path)
{
int len = prefix ? strlen(prefix) : 0;
char *r = prefix_path_gently(prefix, len, NULL, path);
char *r = prefix_path_gently(the_repository, prefix, len, NULL, path);
if (r) {
free(r);
return 1;

View File

@ -138,8 +138,8 @@ const char *enter_repo(const char *path, unsigned flags);

const char *setup_git_directory_gently(int *);
const char *setup_git_directory(void);
char *prefix_path(const char *prefix, int len, const char *path);
char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path);
char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path);
char *prefix_path_gently(struct repository *repo, const char *prefix, int len, int *remaining, const char *path);

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

View File

@ -379,7 +379,7 @@ int cmd__path_utils(int argc, const char **argv)
int nongit_ok;
setup_git_directory_gently(&nongit_ok);
while (argc > 3) {
char *pfx = prefix_path(prefix, prefix_len, argv[3]);
char *pfx = prefix_path(the_repository, prefix, prefix_len, argv[3]);

puts(pfx);
free(pfx);