Skip to content

Commit 88dd321

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ee018d commit 88dd321

File tree

15 files changed

+128
-93
lines changed

15 files changed

+128
-93
lines changed

builtin/commit.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
352352
struct pathspec pathspec;
353353
int refresh_flags = REFRESH_QUIET;
354354
const char *ret;
355+
char *path = NULL;
355356

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

527-
hold_lock_file_for_update(&false_lock,
528-
git_path("next-index-%"PRIuMAX,
529-
(uintmax_t) getpid()),
528+
path = repo_git_path(the_repository, "next-index-%"PRIuMAX,
529+
(uintmax_t) getpid());
530+
hold_lock_file_for_update(&false_lock, path,
530531
LOCK_DIE_ON_ERROR);
531532

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

builtin/gc.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ static void process_log_file(void)
9999
/* There was some error recorded in the lock file */
100100
commit_lock_file(&log_lock);
101101
} else {
102+
char *path = repo_git_path(the_repository, "gc.log");
102103
/* No error, clean up any old gc.log */
103-
unlink(git_path("gc.log"));
104+
unlink(path);
104105
rollback_lock_file(&log_lock);
106+
free(path);
105107
}
106108
}
107109

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

303-
dir = opendir(git_path("objects/17"));
306+
path = repo_git_path(the_repository, "objects/17");
307+
dir = opendir(path);
308+
free(path);
304309
if (!dir)
305310
return 0;
306311

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

823828
if (daemonized) {
824-
hold_lock_file_for_update(&log_lock,
825-
git_path("gc.log"),
829+
char *path = repo_git_path(the_repository, "gc.log");
830+
hold_lock_file_for_update(&log_lock, path,
826831
LOCK_DIE_ON_ERROR);
827832
dup2(get_lock_file_fd(&log_lock), 2);
828833
atexit(process_log_file_at_exit);
834+
free(path);
829835
}
830836

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

890-
if (!daemonized)
891-
unlink(git_path("gc.log"));
896+
if (!daemonized) {
897+
char *path = repo_git_path(the_repository, "gc.log");
898+
unlink(path);
899+
free(path);
900+
}
892901

893902
out:
894903
gc_config_release(&cfg);

builtin/notes.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,8 @@ static int merge(int argc, const char **argv, const char *prefix,
979979
else { /* Merge has unresolved conflicts */
980980
struct worktree **worktrees;
981981
const struct worktree *wt;
982+
char *path;
983+
982984
/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
983985
refs_update_ref(get_main_ref_store(the_repository), msg.buf,
984986
"NOTES_MERGE_PARTIAL", &result_oid, NULL,
@@ -994,10 +996,13 @@ static int merge(int argc, const char **argv, const char *prefix,
994996
if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
995997
die(_("failed to store link to current notes ref (%s)"),
996998
notes_ref);
999+
1000+
path = repo_git_path(the_repository, NOTES_MERGE_WORKTREE);
9971001
fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
9981002
"and commit the result with 'git notes merge --commit', "
9991003
"or abort the merge with 'git notes merge --abort'.\n"),
1000-
git_path(NOTES_MERGE_WORKTREE));
1004+
path);
1005+
free(path);
10011006
}
10021007

10031008
free_notes(t);

builtin/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ static int run_am(struct rebase_options *opts)
644644
return run_command(&am);
645645
}
646646

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

builtin/remote.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,11 @@ static int migrate_file(struct remote *remote)
644644
git_config_set_multivar(buf.buf, remote->fetch.items[i].raw, "^$", 0);
645645
#ifndef WITH_BREAKING_CHANGES
646646
if (remote->origin == REMOTE_REMOTES)
647-
unlink_or_warn(git_path("remotes/%s", remote->name));
647+
unlink_or_warn(repo_git_path_replace(the_repository, &buf,
648+
"remotes/%s", remote->name));
648649
else if (remote->origin == REMOTE_BRANCHES)
649-
unlink_or_warn(git_path("branches/%s", remote->name));
650+
unlink_or_warn(repo_git_path_replace(the_repository, &buf,
651+
"branches/%s", remote->name));
650652
#endif /* WITH_BREAKING_CHANGES */
651653
strbuf_release(&buf);
652654

builtin/rev-parse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,8 @@ int cmd_rev_parse(int argc,
789789
if (!strcmp(arg, "--git-path")) {
790790
if (!argv[i + 1])
791791
die(_("--git-path requires an argument"));
792-
strbuf_reset(&buf);
793-
print_path(git_path("%s", argv[i + 1]), prefix,
792+
print_path(repo_git_path_replace(the_repository, &buf,
793+
"%s", argv[i + 1]), prefix,
794794
format,
795795
DEFAULT_RELATIVE_IF_SHARED);
796796
i++;
@@ -1083,7 +1083,7 @@ int cmd_rev_parse(int argc,
10831083
die(_("Could not read the index"));
10841084
if (the_repository->index->split_index) {
10851085
const struct object_id *oid = &the_repository->index->split_index->base_oid;
1086-
const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1086+
const char *path = repo_git_path_replace(the_repository, &buf, "sharedindex.%s", oid_to_hex(oid));
10871087
print_path(path, prefix, format, DEFAULT_RELATIVE);
10881088
}
10891089
continue;

builtin/worktree.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ static int delete_git_dir(const char *id)
163163

164164
static void delete_worktrees_dir_if_empty(void)
165165
{
166-
rmdir(git_path("worktrees")); /* ignore failed removal */
166+
char *path = repo_git_path(the_repository, "worktrees");
167+
rmdir(path); /* ignore failed removal */
168+
free(path);
167169
}
168170

169171
static void prune_worktree(const char *id, const char *reason)
@@ -212,8 +214,13 @@ static void prune_worktrees(void)
212214
struct strbuf reason = STRBUF_INIT;
213215
struct strbuf main_path = STRBUF_INIT;
214216
struct string_list kept = STRING_LIST_INIT_DUP;
215-
DIR *dir = opendir(git_path("worktrees"));
217+
char *path;
218+
DIR *dir;
216219
struct dirent *d;
220+
221+
path = repo_git_path(the_repository, "worktrees");
222+
dir = opendir(path);
223+
free(path);
217224
if (!dir)
218225
return;
219226
while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) {

notes-merge.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,34 +275,38 @@ static void diff_tree_local(struct notes_merge_options *o,
275275

276276
static void check_notes_merge_worktree(struct notes_merge_options *o)
277277
{
278+
struct strbuf buf = STRBUF_INIT;
279+
278280
if (!o->has_worktree) {
279281
/*
280282
* Must establish NOTES_MERGE_WORKTREE.
281283
* Abort if NOTES_MERGE_WORKTREE already exists
282284
*/
283-
if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
284-
!is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
285+
if (file_exists(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE)) &&
286+
!is_empty_dir(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE))) {
285287
if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
286288
die(_("You have not concluded your previous "
287289
"notes merge (%s exists).\nPlease, use "
288290
"'git notes merge --commit' or 'git notes "
289291
"merge --abort' to commit/abort the "
290292
"previous merge before you start a new "
291-
"notes merge."), git_path("NOTES_MERGE_*"));
293+
"notes merge."), repo_git_path_replace(the_repository, &buf, "NOTES_MERGE_*"));
292294
else
293295
die(_("You have not concluded your notes merge "
294-
"(%s exists)."), git_path("NOTES_MERGE_*"));
296+
"(%s exists)."), repo_git_path_replace(the_repository, &buf, "NOTES_MERGE_*"));
295297
}
296298

297-
if (safe_create_leading_directories_const(git_path(
299+
if (safe_create_leading_directories_const(repo_git_path_replace(the_repository, &buf,
298300
NOTES_MERGE_WORKTREE "/.test")))
299301
die_errno("unable to create directory %s",
300-
git_path(NOTES_MERGE_WORKTREE));
302+
repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE));
301303
o->has_worktree = 1;
302-
} else if (!file_exists(git_path(NOTES_MERGE_WORKTREE)))
304+
} else if (!file_exists(repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE)))
303305
/* NOTES_MERGE_WORKTREE should already be established */
304306
die("missing '%s'. This should not happen",
305-
git_path(NOTES_MERGE_WORKTREE));
307+
repo_git_path_replace(the_repository, &buf, NOTES_MERGE_WORKTREE));
308+
309+
strbuf_release(&buf);
306310
}
307311

308312
static void write_buf_to_worktree(const struct object_id *obj,

path.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int get_st_mode_bits(const char *path, int *mode)
3030
return 0;
3131
}
3232

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

420-
void repo_git_pathv(const struct repository *repo,
421-
const struct worktree *wt, struct strbuf *buf,
422-
const char *fmt, va_list args)
420+
static void repo_git_pathv(const struct repository *repo,
421+
const struct worktree *wt, struct strbuf *buf,
422+
const char *fmt, va_list args)
423423
{
424424
int gitdir_len;
425425
strbuf_worktree_gitdir(buf, repo, wt);

path.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -221,37 +221,10 @@ char *xdg_cache_home(const char *filename);
221221
*/
222222
void safe_create_dir(const char *dir, int share);
223223

224-
/*
225-
* Do not use this function. It is only exported to other subsystems until we
226-
* can get rid of the below block of functions that implicitly rely on
227-
* `the_repository`.
228-
*/
229-
struct strbuf *get_pathname(void);
230-
231224
# ifdef USE_THE_REPOSITORY_VARIABLE
232225
# include "strbuf.h"
233226
# include "repository.h"
234227

235-
/* Internal implementation details that should not be used. */
236-
void repo_git_pathv(const struct repository *repo,
237-
const struct worktree *wt, struct strbuf *buf,
238-
const char *fmt, va_list args);
239-
240-
/*
241-
* Return a statically allocated path into the main repository's
242-
* (the_repository) git directory.
243-
*/
244-
__attribute__((format (printf, 1, 2)))
245-
static inline const char *git_path(const char *fmt, ...)
246-
{
247-
struct strbuf *pathname = get_pathname();
248-
va_list args;
249-
va_start(args, fmt);
250-
repo_git_pathv(the_repository, NULL, pathname, fmt, args);
251-
va_end(args);
252-
return pathname->buf;
253-
}
254-
255228
#define GIT_PATH_FUNC(func, filename) \
256229
const char *func(void) \
257230
{ \

0 commit comments

Comments
 (0)