path: drop `git_path()` in favor of `repo_git_path()`

Remove `git_path()` in favor of the `repo_git_path()` family of
functions, which makes the implicit dependency on `the_repository` go
away.

Note that `git_path()` returned a string allocated via `get_pathname()`,
which uses a rotating set of statically allocated buffers. Consequently,
callers didn't have to free the returned string. The same isn't true for
`repo_common_path()`, so we also have to add logic to free the returned
strings.

This refactoring also allows us to remove `repo_common_pathv()` as well
as `get_pathname()` from the public interface.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Patrick Steinhardt 2025-02-07 12:03:37 +01:00 committed by Junio C Hamano
parent 8ee018d863
commit 88dd321cfe
15 changed files with 128 additions and 93 deletions

View File

@ -352,6 +352,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
struct pathspec pathspec;
int refresh_flags = REFRESH_QUIET;
const char *ret;
char *path = NULL;

if (is_status)
refresh_flags |= REFRESH_UNMERGED;
@ -524,9 +525,9 @@ static const char *prepare_index(const char **argv, const char *prefix,
if (write_locked_index(the_repository->index, &index_lock, 0))
die(_("unable to write new index file"));

hold_lock_file_for_update(&false_lock,
git_path("next-index-%"PRIuMAX,
(uintmax_t) getpid()),
path = repo_git_path(the_repository, "next-index-%"PRIuMAX,
(uintmax_t) getpid());
hold_lock_file_for_update(&false_lock, path,
LOCK_DIE_ON_ERROR);

create_base_index(current_head);
@ -542,6 +543,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
out:
string_list_clear(&partial, 0);
clear_pathspec(&pathspec);
free(path);
return ret;
}


View File

@ -99,9 +99,11 @@ static void process_log_file(void)
/* There was some error recorded in the lock file */
commit_lock_file(&log_lock);
} else {
char *path = repo_git_path(the_repository, "gc.log");
/* No error, clean up any old gc.log */
unlink(git_path("gc.log"));
unlink(path);
rollback_lock_file(&log_lock);
free(path);
}
}

@ -299,8 +301,11 @@ static int too_many_loose_objects(struct gc_config *cfg)
int num_loose = 0;
int needed = 0;
const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
char *path;

dir = opendir(git_path("objects/17"));
path = repo_git_path(the_repository, "objects/17");
dir = opendir(path);
free(path);
if (!dir)
return 0;

@ -821,11 +826,12 @@ struct repository *repo UNUSED)
}

if (daemonized) {
hold_lock_file_for_update(&log_lock,
git_path("gc.log"),
char *path = repo_git_path(the_repository, "gc.log");
hold_lock_file_for_update(&log_lock, path,
LOCK_DIE_ON_ERROR);
dup2(get_lock_file_fd(&log_lock), 2);
atexit(process_log_file_at_exit);
free(path);
}

gc_before_repack(&opts, &cfg);
@ -887,8 +893,11 @@ struct repository *repo UNUSED)
warning(_("There are too many unreachable loose objects; "
"run 'git prune' to remove them."));

if (!daemonized)
unlink(git_path("gc.log"));
if (!daemonized) {
char *path = repo_git_path(the_repository, "gc.log");
unlink(path);
free(path);
}

out:
gc_config_release(&cfg);

View File

@ -979,6 +979,8 @@ static int merge(int argc, const char **argv, const char *prefix,
else { /* Merge has unresolved conflicts */
struct worktree **worktrees;
const struct worktree *wt;
char *path;

/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
refs_update_ref(get_main_ref_store(the_repository), msg.buf,
"NOTES_MERGE_PARTIAL", &result_oid, NULL,
@ -994,10 +996,13 @@ static int merge(int argc, const char **argv, const char *prefix,
if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
die(_("failed to store link to current notes ref (%s)"),
notes_ref);

path = repo_git_path(the_repository, NOTES_MERGE_WORKTREE);
fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
"and commit the result with 'git notes merge --commit', "
"or abort the merge with 'git notes merge --abort'.\n"),
git_path(NOTES_MERGE_WORKTREE));
path);
free(path);
}

free_notes(t);

View File

@ -644,7 +644,7 @@ static int run_am(struct rebase_options *opts)
return run_command(&am);
}

rebased_patches = xstrdup(git_path("rebased-patches"));
rebased_patches = repo_git_path(the_repository, "rebased-patches");
format_patch.out = open(rebased_patches,
O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (format_patch.out < 0) {

View File

@ -644,9 +644,11 @@ static int migrate_file(struct remote *remote)
git_config_set_multivar(buf.buf, remote->fetch.items[i].raw, "^$", 0);
#ifndef WITH_BREAKING_CHANGES
if (remote->origin == REMOTE_REMOTES)
unlink_or_warn(git_path("remotes/%s", remote->name));
unlink_or_warn(repo_git_path_replace(the_repository, &buf,
"remotes/%s", remote->name));
else if (remote->origin == REMOTE_BRANCHES)
unlink_or_warn(git_path("branches/%s", remote->name));
unlink_or_warn(repo_git_path_replace(the_repository, &buf,
"branches/%s", remote->name));
#endif /* WITH_BREAKING_CHANGES */
strbuf_release(&buf);


View File

@ -789,8 +789,8 @@ int cmd_rev_parse(int argc,
if (!strcmp(arg, "--git-path")) {
if (!argv[i + 1])
die(_("--git-path requires an argument"));
strbuf_reset(&buf);
print_path(git_path("%s", argv[i + 1]), prefix,
print_path(repo_git_path_replace(the_repository, &buf,
"%s", argv[i + 1]), prefix,
format,
DEFAULT_RELATIVE_IF_SHARED);
i++;
@ -1083,7 +1083,7 @@ int cmd_rev_parse(int argc,
die(_("Could not read the index"));
if (the_repository->index->split_index) {
const struct object_id *oid = &the_repository->index->split_index->base_oid;
const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
const char *path = repo_git_path_replace(the_repository, &buf, "sharedindex.%s", oid_to_hex(oid));
print_path(path, prefix, format, DEFAULT_RELATIVE);
}
continue;

View File

@ -163,7 +163,9 @@ static int delete_git_dir(const char *id)

static void delete_worktrees_dir_if_empty(void)
{
rmdir(git_path("worktrees")); /* ignore failed removal */
char *path = repo_git_path(the_repository, "worktrees");
rmdir(path); /* ignore failed removal */
free(path);
}

static void prune_worktree(const char *id, const char *reason)
@ -212,8 +214,13 @@ static void prune_worktrees(void)
struct strbuf reason = STRBUF_INIT;
struct strbuf main_path = STRBUF_INIT;
struct string_list kept = STRING_LIST_INIT_DUP;
DIR *dir = opendir(git_path("worktrees"));
char *path;
DIR *dir;
struct dirent *d;

path = repo_git_path(the_repository, "worktrees");
dir = opendir(path);
free(path);
if (!dir)
return;
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {

View File

@ -275,34 +275,38 @@ static void diff_tree_local(struct notes_merge_options *o,

static void check_notes_merge_worktree(struct notes_merge_options *o)
{
struct strbuf buf = STRBUF_INIT;

if (!o->has_worktree) {
/*
* Must establish NOTES_MERGE_WORKTREE.
* Abort if NOTES_MERGE_WORKTREE already exists
*/
if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
!is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
if (file_exists(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE)) &&
!is_empty_dir(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE))) {
if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
die(_("You have not concluded your previous "
"notes merge (%s exists).\nPlease, use "
"'git notes merge --commit' or 'git notes "
"merge --abort' to commit/abort the "
"previous merge before you start a new "
"notes merge."), git_path("NOTES_MERGE_*"));
"notes merge."), repo_git_path_replace(the_repository, &buf, "NOTES_MERGE_*"));
else
die(_("You have not concluded your notes merge "
"(%s exists)."), git_path("NOTES_MERGE_*"));
"(%s exists)."), repo_git_path_replace(the_repository, &buf, "NOTES_MERGE_*"));
}

if (safe_create_leading_directories_const(git_path(
if (safe_create_leading_directories_const(repo_git_path_replace(the_repository, &buf,
NOTES_MERGE_WORKTREE "/.test")))
die_errno("unable to create directory %s",
git_path(NOTES_MERGE_WORKTREE));
repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE));
o->has_worktree = 1;
} else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))
} else if (!file_exists(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE)))
/* NOTES_MERGE_WORKTREE should already be established */
die("missing '%s'. This should not happen",
git_path(NOTES_MERGE_WORKTREE));
repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE));

strbuf_release(&buf);
}

static void write_buf_to_worktree(const struct object_id *obj,

8
path.c
View File

@ -30,7 +30,7 @@ static int get_st_mode_bits(const char *path, int *mode)
return 0;
}

struct strbuf *get_pathname(void)
static struct strbuf *get_pathname(void)
{
static struct strbuf pathname_array[4] = {
STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
@ -417,9 +417,9 @@ static void strbuf_worktree_gitdir(struct strbuf *buf,
repo_common_path_append(repo, buf, "worktrees/%s", wt->id);
}

void repo_git_pathv(const struct repository *repo,
const struct worktree *wt, struct strbuf *buf,
const char *fmt, va_list args)
static void repo_git_pathv(const struct repository *repo,
const struct worktree *wt, struct strbuf *buf,
const char *fmt, va_list args)
{
int gitdir_len;
strbuf_worktree_gitdir(buf, repo, wt);

27
path.h
View File

@ -221,37 +221,10 @@ char *xdg_cache_home(const char *filename);
*/
void safe_create_dir(const char *dir, int share);

/*
* Do not use this function. It is only exported to other subsystems until we
* can get rid of the below block of functions that implicitly rely on
* `the_repository`.
*/
struct strbuf *get_pathname(void);

# ifdef USE_THE_REPOSITORY_VARIABLE
# include "strbuf.h"
# include "repository.h"

/* Internal implementation details that should not be used. */
void repo_git_pathv(const struct repository *repo,
const struct worktree *wt, struct strbuf *buf,
const char *fmt, va_list args);

/*
* Return a statically allocated path into the main repository's
* (the_repository) git directory.
*/
__attribute__((format (printf, 1, 2)))
static inline const char *git_path(const char *fmt, ...)
{
struct strbuf *pathname = get_pathname();
va_list args;
va_start(args, fmt);
repo_git_pathv(the_repository, NULL, pathname, fmt, args);
va_end(args);
return pathname->buf;
}

#define GIT_PATH_FUNC(func, filename) \
const char *func(void) \
{ \

View File

@ -3251,15 +3251,18 @@ static int clean_shared_index_files(const char *current_hex)

while ((de = readdir(dir)) != NULL) {
const char *sha1_hex;
const char *shared_index_path;
char *shared_index_path;
if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex))
continue;
if (!strcmp(sha1_hex, current_hex))
continue;
shared_index_path = git_path("%s", de->d_name);

shared_index_path = repo_git_path(the_repository, "%s", de->d_name);
if (should_delete_shared_index(shared_index_path) > 0 &&
unlink(shared_index_path))
warning_errno(_("unable to unlink: %s"), shared_index_path);

free(shared_index_path);
}
closedir(dir);

@ -3271,6 +3274,7 @@ static int write_shared_index(struct index_state *istate,
{
struct split_index *si = istate->split_index;
int ret, was_full = !istate->sparse_index;
char *path;

move_cache_to_base_index(istate);
convert_to_sparse(istate, 0);
@ -3291,13 +3295,15 @@ static int write_shared_index(struct index_state *istate,
error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
return ret;
}
ret = rename_tempfile(temp,
git_path("sharedindex.%s", oid_to_hex(&si->base->oid)));

path = repo_git_path(the_repository, "sharedindex.%s", oid_to_hex(&si->base->oid));
ret = rename_tempfile(temp, path);
if (!ret) {
oidcpy(&si->base_oid, &si->base->oid);
clean_shared_index_files(oid_to_hex(&si->base->oid));
}

free(path);
return ret;
}

@ -3378,9 +3384,12 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
if (new_shared_index) {
struct tempfile *temp;
int saved_errno;
char *path;

/* Same initial permissions as the main .git/index file */
temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
path = repo_git_path(the_repository, "sharedindex_XXXXXX");
temp = mks_tempfile_sm(path, 0, 0666);
free(path);
if (!temp) {
ret = do_write_locked_index(istate, lock, flags,
~WRITE_SPLIT_INDEX_EXTENSION);
@ -3401,9 +3410,10 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,

/* Freshen the shared index only if the split-index was written */
if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) {
const char *shared_index = git_path("sharedindex.%s",
oid_to_hex(&si->base_oid));
char *shared_index = repo_git_path(the_repository, "sharedindex.%s",
oid_to_hex(&si->base_oid));
freshen_shared_index(shared_index, 1);
free(shared_index);
}

out:

View File

@ -321,10 +321,11 @@ static void read_remotes_file(struct remote_state *remote_state,
struct remote *remote)
{
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf,
"remotes/%s", remote->name), "r");

if (!f)
return;
goto out;

warn_about_deprecated_remote_type("remotes", remote);

@ -343,8 +344,10 @@ static void read_remotes_file(struct remote_state *remote_state,
else if (skip_prefix(buf.buf, "Pull:", &v))
refspec_append(&remote->fetch, skip_spaces(v));
}
strbuf_release(&buf);
fclose(f);

out:
strbuf_release(&buf);
}

static void read_branches_file(struct remote_state *remote_state,
@ -352,20 +355,19 @@ static void read_branches_file(struct remote_state *remote_state,
{
char *frag, *to_free = NULL;
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf,
"branches/%s", remote->name), "r");

if (!f)
return;
goto out;

warn_about_deprecated_remote_type("branches", remote);

strbuf_getline_lf(&buf, f);
fclose(f);
strbuf_trim(&buf);
if (!buf.len) {
strbuf_release(&buf);
return;
}
if (!buf.len)
goto out;

remote->configured_in_repo = 1;
remote->origin = REMOTE_BRANCHES;
@ -393,6 +395,7 @@ static void read_branches_file(struct remote_state *remote_state,
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
remote->fetch_tags = 1; /* always auto-follow */

out:
strbuf_release(&buf);
free(to_free);
}

View File

@ -127,8 +127,12 @@ static int is_rr_file(const char *name, const char *filename, int *variant)
static void scan_rerere_dir(struct rerere_dir *rr_dir)
{
struct dirent *de;
DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
char *path;
DIR *dir;

path = repo_git_path(the_repository, "rr-cache/%s", rr_dir->name);
dir = opendir(path);
free(path);
if (!dir)
return;
while ((de = readdir(dir)) != NULL) {
@ -1234,6 +1238,7 @@ void rerere_gc(struct repository *r, struct string_list *rr)
timestamp_t now = time(NULL);
timestamp_t cutoff_noresolve = now - 15 * 86400;
timestamp_t cutoff_resolve = now - 60 * 86400;
struct strbuf buf = STRBUF_INIT;

if (setup_rerere(r, rr, 0) < 0)
return;
@ -1243,7 +1248,7 @@ void rerere_gc(struct repository *r, struct string_list *rr)
repo_config_get_expiry_in_days(the_repository, "gc.rerereunresolved",
&cutoff_noresolve, now);
git_config(git_default_config, NULL);
dir = opendir(git_path("rr-cache"));
dir = opendir(repo_git_path_replace(the_repository, &buf, "rr-cache"));
if (!dir)
die_errno(_("unable to open rr-cache directory"));
/* Collect stale conflict IDs ... */
@ -1272,9 +1277,12 @@ void rerere_gc(struct repository *r, struct string_list *rr)

/* ... and then remove the empty directories */
for (i = 0; i < to_remove.nr; i++)
rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
rmdir(repo_git_path_replace(the_repository, &buf,
"rr-cache/%s", to_remove.items[i].string));

string_list_clear(&to_remove, 0);
rollback_lock_file(&write_lock);
strbuf_release(&buf);
}

/*

View File

@ -364,7 +364,9 @@ const char *setup_temporary_shallow(const struct oid_array *extra)
struct strbuf sb = STRBUF_INIT;

if (write_shallow_commits(&sb, 0, extra)) {
temp = xmks_tempfile(git_path("shallow_XXXXXX"));
char *path = repo_git_path(the_repository, "shallow_XXXXXX");
temp = xmks_tempfile(path);
free(path);

if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
close_tempfile_gently(temp) < 0)

View File

@ -1289,7 +1289,8 @@ static void show_am_in_progress(struct wt_status *s,
static char *read_line_from_git_path(const char *filename)
{
struct strbuf buf = STRBUF_INIT;
FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
FILE *fp = fopen_or_warn(repo_git_path_append(the_repository, &buf,
"%s", filename), "r");

if (!fp) {
strbuf_release(&buf);
@ -1383,27 +1384,33 @@ static void abbrev_oid_in_line(struct strbuf *line)

static int read_rebase_todolist(const char *fname, struct string_list *lines)
{
struct strbuf line = STRBUF_INIT;
FILE *f = fopen(git_path("%s", fname), "r");
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen(repo_git_path_append(the_repository, &buf, "%s", fname), "r");
int ret;

if (!f) {
if (errno == ENOENT)
return -1;
if (errno == ENOENT) {
ret = -1;
goto out;
}
die_errno("Could not open file %s for reading",
git_path("%s", fname));
repo_git_path_replace(the_repository, &buf, "%s", fname));
}
while (!strbuf_getline_lf(&line, f)) {
if (starts_with(line.buf, comment_line_str))
while (!strbuf_getline_lf(&buf, f)) {
if (starts_with(buf.buf, comment_line_str))
continue;
strbuf_trim(&line);
if (!line.len)
strbuf_trim(&buf);
if (!buf.len)
continue;
abbrev_oid_in_line(&line);
string_list_append(lines, line.buf);
abbrev_oid_in_line(&buf);
string_list_append(lines, buf.buf);
}
fclose(f);
strbuf_release(&line);
return 0;

ret = 0;
out:
strbuf_release(&buf);
return ret;
}

static void show_rebase_information(struct wt_status *s,
@ -1434,9 +1441,12 @@ static void show_rebase_information(struct wt_status *s,
i < have_done.nr;
i++)
status_printf_ln(s, color, " %s", have_done.items[i].string);
if (have_done.nr > nr_lines_to_show && s->hints)
if (have_done.nr > nr_lines_to_show && s->hints) {
char *path = repo_git_path(the_repository, "rebase-merge/done");
status_printf_ln(s, color,
_(" (see more in file %s)"), git_path("rebase-merge/done"));
_(" (see more in file %s)"), path);
free(path);
}
}

if (yet_to_do.nr == 0)