Skip to content

Commit 7ac1664

Browse files
pks-tgitster
authored andcommitted
path: hide functions using the_repository by default
The path subsystem provides a bunch of legacy functions that compute paths relative to the "gitdir" and "commondir" directories of the global `the_repository` variable. Use of those functions is discouraged, and it is easy to miss the implicit dependency on `the_repository` that calls to those functions may cause. With `USE_THE_REPOSITORY_VARIABLE`, we have recently introduced a tool that allows us to get rid of such functions over time. With this macro, we can hide away functions that have such implicit dependency such that other subsystems that want to be free of `the_repository` will not use them by accident. Move all path-related functions that use `the_repository` into a block that gets only conditionally compiled depending on whether or not the macro has been defined. This also removes all dependencies on that variable in "path.c", allowing us to remove the definition of said preprocessor macro. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a973f60 commit 7ac1664

File tree

2 files changed

+100
-99
lines changed

2 files changed

+100
-99
lines changed

path.c

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Utilities for paths and pathnames
33
*/
44

5-
#define USE_THE_REPOSITORY_VARIABLE
6-
75
#include "git-compat-util.h"
86
#include "abspath.h"
97
#include "environment.h"
@@ -30,7 +28,7 @@ static int get_st_mode_bits(const char *path, int *mode)
3028
return 0;
3129
}
3230

33-
static struct strbuf *get_pathname(void)
31+
struct strbuf *get_pathname(void)
3432
{
3533
static struct strbuf pathname_array[4] = {
3634
STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
@@ -453,44 +451,6 @@ void strbuf_repo_git_path(struct strbuf *sb,
453451
va_end(args);
454452
}
455453

456-
char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
457-
{
458-
va_list args;
459-
strbuf_reset(buf);
460-
va_start(args, fmt);
461-
repo_git_pathv(the_repository, NULL, buf, fmt, args);
462-
va_end(args);
463-
return buf->buf;
464-
}
465-
466-
void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
467-
{
468-
va_list args;
469-
va_start(args, fmt);
470-
repo_git_pathv(the_repository, NULL, sb, fmt, args);
471-
va_end(args);
472-
}
473-
474-
const char *git_path(const char *fmt, ...)
475-
{
476-
struct strbuf *pathname = get_pathname();
477-
va_list args;
478-
va_start(args, fmt);
479-
repo_git_pathv(the_repository, NULL, pathname, fmt, args);
480-
va_end(args);
481-
return pathname->buf;
482-
}
483-
484-
char *git_pathdup(const char *fmt, ...)
485-
{
486-
struct strbuf path = STRBUF_INIT;
487-
va_list args;
488-
va_start(args, fmt);
489-
repo_git_pathv(the_repository, NULL, &path, fmt, args);
490-
va_end(args);
491-
return strbuf_detach(&path, NULL);
492-
}
493-
494454
char *mkpathdup(const char *fmt, ...)
495455
{
496456
struct strbuf sb = STRBUF_INIT;
@@ -634,16 +594,6 @@ void repo_common_pathv(const struct repository *repo,
634594
strbuf_cleanup_path(sb);
635595
}
636596

637-
const char *git_common_path(const char *fmt, ...)
638-
{
639-
struct strbuf *pathname = get_pathname();
640-
va_list args;
641-
va_start(args, fmt);
642-
repo_common_pathv(the_repository, pathname, fmt, args);
643-
va_end(args);
644-
return pathname->buf;
645-
}
646-
647597
void strbuf_git_common_path(struct strbuf *sb,
648598
const struct repository *repo,
649599
const char *fmt, ...)

path.h

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ char *mkpathdup(const char *fmt, ...)
2525
__attribute__((format (printf, 1, 2)));
2626

2727
/*
28-
* The `git_common_path` family of functions will construct a path into a
28+
* The `strbuf_git_common_path` family of functions will construct a path into a
2929
* repository's common git directory, which is shared by all worktrees.
3030
*/
3131

@@ -43,14 +43,7 @@ void repo_common_pathv(const struct repository *repo,
4343
va_list args);
4444

4545
/*
46-
* Return a statically allocated path into the main repository's
47-
* (the_repository) common git directory.
48-
*/
49-
const char *git_common_path(const char *fmt, ...)
50-
__attribute__((format (printf, 1, 2)));
51-
52-
/*
53-
* The `git_path` family of functions will construct a path into a repository's
46+
* The `repo_git_path` family of functions will construct a path into a repository's
5447
* git directory.
5548
*
5649
* These functions will perform adjustments to the resultant path to account
@@ -87,14 +80,7 @@ void strbuf_repo_git_path(struct strbuf *sb,
8780
__attribute__((format (printf, 3, 4)));
8881

8982
/*
90-
* Return a statically allocated path into the main repository's
91-
* (the_repository) git directory.
92-
*/
93-
const char *git_path(const char *fmt, ...)
94-
__attribute__((format (printf, 1, 2)));
95-
96-
/*
97-
* Similar to git_path() but can produce paths for a specified
83+
* Similar to repo_git_path() but can produce paths for a specified
9884
* worktree instead of current one. When no worktree is given, then the path is
9985
* computed relative to main worktree of the given repository.
10086
*/
@@ -103,27 +89,6 @@ const char *worktree_git_path(struct repository *r,
10389
const char *fmt, ...)
10490
__attribute__((format (printf, 3, 4)));
10591

106-
/*
107-
* Return a path into the main repository's (the_repository) git directory.
108-
*/
109-
char *git_pathdup(const char *fmt, ...)
110-
__attribute__((format (printf, 1, 2)));
111-
112-
/*
113-
* Construct a path into the main repository's (the_repository) git directory
114-
* and place it in the provided buffer `buf`, the contents of the buffer will
115-
* be overridden.
116-
*/
117-
char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
118-
__attribute__((format (printf, 2, 3)));
119-
120-
/*
121-
* Construct a path into the main repository's (the_repository) git directory
122-
* and append it to the provided buffer `sb`.
123-
*/
124-
void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
125-
__attribute__((format (printf, 2, 3)));
126-
12792
/*
12893
* Return a path into the worktree of repository `repo`.
12994
*
@@ -165,19 +130,10 @@ void report_linked_checkout_garbage(struct repository *r);
165130
/*
166131
* You can define a static memoized git path like:
167132
*
168-
* static GIT_PATH_FUNC(git_path_foo, "FOO")
133+
* static REPO_GIT_PATH_FUNC(git_path_foo, "FOO")
169134
*
170135
* or use one of the global ones below.
171136
*/
172-
#define GIT_PATH_FUNC(func, filename) \
173-
const char *func(void) \
174-
{ \
175-
static char *ret; \
176-
if (!ret) \
177-
ret = git_pathdup(filename); \
178-
return ret; \
179-
}
180-
181137
#define REPO_GIT_PATH_FUNC(var, filename) \
182138
const char *git_path_##var(struct repository *r) \
183139
{ \
@@ -261,4 +217,99 @@ char *xdg_cache_home(const char *filename);
261217
*/
262218
void safe_create_dir(const char *dir, int share);
263219

220+
/*
221+
* Do not use this function. It is only exported to other subsystems until we
222+
* can get rid of the below block of functions that implicitly rely on
223+
* `the_repository`.
224+
*/
225+
struct strbuf *get_pathname(void);
226+
227+
# ifdef USE_THE_REPOSITORY_VARIABLE
228+
# include "strbuf.h"
229+
# include "repository.h"
230+
231+
/*
232+
* Return a statically allocated path into the main repository's
233+
* (the_repository) common git directory.
234+
*/
235+
__attribute__((format (printf, 1, 2)))
236+
static inline const char *git_common_path(const char *fmt, ...)
237+
{
238+
struct strbuf *pathname = get_pathname();
239+
va_list args;
240+
va_start(args, fmt);
241+
repo_common_pathv(the_repository, pathname, fmt, args);
242+
va_end(args);
243+
return pathname->buf;
244+
}
245+
246+
/*
247+
* Construct a path into the main repository's (the_repository) git directory
248+
* and place it in the provided buffer `buf`, the contents of the buffer will
249+
* be overridden.
250+
*/
251+
__attribute__((format (printf, 2, 3)))
252+
static inline char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
253+
{
254+
va_list args;
255+
strbuf_reset(buf);
256+
va_start(args, fmt);
257+
repo_git_pathv(the_repository, NULL, buf, fmt, args);
258+
va_end(args);
259+
return buf->buf;
260+
}
261+
262+
/*
263+
* Construct a path into the main repository's (the_repository) git directory
264+
* and append it to the provided buffer `sb`.
265+
*/
266+
__attribute__((format (printf, 2, 3)))
267+
static inline void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
268+
{
269+
va_list args;
270+
va_start(args, fmt);
271+
repo_git_pathv(the_repository, NULL, sb, fmt, args);
272+
va_end(args);
273+
}
274+
275+
/*
276+
* Return a statically allocated path into the main repository's
277+
* (the_repository) git directory.
278+
*/
279+
__attribute__((format (printf, 1, 2)))
280+
static inline const char *git_path(const char *fmt, ...)
281+
{
282+
struct strbuf *pathname = get_pathname();
283+
va_list args;
284+
va_start(args, fmt);
285+
repo_git_pathv(the_repository, NULL, pathname, fmt, args);
286+
va_end(args);
287+
return pathname->buf;
288+
}
289+
290+
#define GIT_PATH_FUNC(func, filename) \
291+
const char *func(void) \
292+
{ \
293+
static char *ret; \
294+
if (!ret) \
295+
ret = git_pathdup(filename); \
296+
return ret; \
297+
}
298+
299+
/*
300+
* Return a path into the main repository's (the_repository) git directory.
301+
*/
302+
__attribute__((format (printf, 1, 2)))
303+
static inline char *git_pathdup(const char *fmt, ...)
304+
{
305+
struct strbuf path = STRBUF_INIT;
306+
va_list args;
307+
va_start(args, fmt);
308+
repo_git_pathv(the_repository, NULL, &path, fmt, args);
309+
va_end(args);
310+
return strbuf_detach(&path, NULL);
311+
}
312+
313+
# endif /* USE_THE_REPOSITORY_VARIABLE */
314+
264315
#endif /* PATH_H */

0 commit comments

Comments
 (0)