blob: 0a59c85a62eda03acd290fa564b7e197288e26ad [file] [log] [blame]
Brandon Williamse7d72d02017-06-22 11:43:35 -07001#ifndef PATH_H
2#define PATH_H
3
Brandon Williamsb3371722017-06-22 11:43:37 -07004struct repository;
Elijah Newrenef3ca952018-08-15 10:54:05 -07005struct strbuf;
Brandon Williamsb3371722017-06-22 11:43:37 -07006
Brandon Williamse7d72d02017-06-22 11:43:35 -07007/*
Brandon Williamsc07b3ad2017-12-13 10:28:02 -08008 * The result to all functions which return statically allocated memory may be
9 * overwritten by another call to _any_ one of these functions. Consider using
10 * the safer variants which operate on strbufs or return allocated memory.
Brandon Williamse7d72d02017-06-22 11:43:35 -070011 */
Brandon Williamse7d72d02017-06-22 11:43:35 -070012
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080013/*
14 * Return a statically allocated path.
15 */
Denton Liub199d712019-04-29 04:28:20 -040016const char *mkpath(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080017 __attribute__((format (printf, 1, 2)));
18
19/*
20 * Return a path.
21 */
Denton Liub199d712019-04-29 04:28:20 -040022char *mkpathdup(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080023 __attribute__((format (printf, 1, 2)));
24
25/*
26 * Construct a path and place the result in the provided buffer `buf`.
27 */
Denton Liub199d712019-04-29 04:28:20 -040028char *mksnpath(char *buf, size_t n, const char *fmt, ...)
Brandon Williamse7d72d02017-06-22 11:43:35 -070029 __attribute__((format (printf, 3, 4)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080030
31/*
32 * The `git_common_path` family of functions will construct a path into a
33 * repository's common git directory, which is shared by all worktrees.
34 */
35
36/*
37 * Constructs a path into the common git directory of repository `repo` and
38 * append it in the provided buffer `sb`.
39 */
Denton Liub199d712019-04-29 04:28:20 -040040void strbuf_git_common_path(struct strbuf *sb,
Denton Liuad6dad02019-04-29 04:28:23 -040041 const struct repository *repo,
42 const char *fmt, ...)
Brandon Williamsb3371722017-06-22 11:43:37 -070043 __attribute__((format (printf, 3, 4)));
Brandon Williamse7d72d02017-06-22 11:43:35 -070044
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080045/*
46 * Return a statically allocated path into the main repository's
47 * (the_repository) common git directory.
48 */
Denton Liub199d712019-04-29 04:28:20 -040049const char *git_common_path(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080050 __attribute__((format (printf, 1, 2)));
51
52
53/*
54 * The `git_path` family of functions will construct a path into a repository's
55 * git directory.
56 *
57 * These functions will perform adjustments to the resultant path to account
58 * for special paths which are either considered common among worktrees (e.g.
59 * paths into the object directory) or have been explicitly set via an
60 * environment variable or config (e.g. path to the index file).
61 *
62 * For an exhaustive list of the adjustments made look at `common_list` and
63 * `adjust_git_path` in path.c.
64 */
65
66/*
67 * Return a path into the git directory of repository `repo`.
68 */
Denton Liub199d712019-04-29 04:28:20 -040069char *repo_git_path(const struct repository *repo,
Denton Liuad6dad02019-04-29 04:28:23 -040070 const char *fmt, ...)
Brandon Williams3181d862017-06-22 11:43:40 -070071 __attribute__((format (printf, 2, 3)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080072
73/*
74 * Construct a path into the git directory of repository `repo` and append it
75 * to the provided buffer `sb`.
76 */
Denton Liub199d712019-04-29 04:28:20 -040077void strbuf_repo_git_path(struct strbuf *sb,
Denton Liuad6dad02019-04-29 04:28:23 -040078 const struct repository *repo,
79 const char *fmt, ...)
Brandon Williams3181d862017-06-22 11:43:40 -070080 __attribute__((format (printf, 3, 4)));
81
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080082/*
83 * Return a statically allocated path into the main repository's
84 * (the_repository) git directory.
85 */
Denton Liub199d712019-04-29 04:28:20 -040086const char *git_path(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080087 __attribute__((format (printf, 1, 2)));
88
89/*
90 * Return a path into the main repository's (the_repository) git directory.
91 */
Denton Liub199d712019-04-29 04:28:20 -040092char *git_pathdup(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080093 __attribute__((format (printf, 1, 2)));
94
95/*
96 * Construct a path into the main repository's (the_repository) git directory
97 * and place it in the provided buffer `buf`, the contents of the buffer will
98 * be overridden.
99 */
Denton Liub199d712019-04-29 04:28:20 -0400100char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800101 __attribute__((format (printf, 2, 3)));
102
103/*
104 * Construct a path into the main repository's (the_repository) git directory
105 * and append it to the provided buffer `sb`.
106 */
Denton Liub199d712019-04-29 04:28:20 -0400107void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800108 __attribute__((format (printf, 2, 3)));
109
110/*
111 * Return a path into the worktree of repository `repo`.
112 *
113 * If the repository doesn't have a worktree NULL is returned.
114 */
Denton Liub199d712019-04-29 04:28:20 -0400115char *repo_worktree_path(const struct repository *repo,
Brandon Williamsb42b0c02017-06-22 11:43:41 -0700116 const char *fmt, ...)
117 __attribute__((format (printf, 2, 3)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800118
119/*
120 * Construct a path into the worktree of repository `repo` and append it
121 * to the provided buffer `sb`.
122 *
123 * If the repository doesn't have a worktree nothing will be appended to `sb`.
124 */
Denton Liub199d712019-04-29 04:28:20 -0400125void strbuf_repo_worktree_path(struct strbuf *sb,
Brandon Williamsb42b0c02017-06-22 11:43:41 -0700126 const struct repository *repo,
127 const char *fmt, ...)
128 __attribute__((format (printf, 3, 4)));
129
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800130/*
131 * Return a path into a submodule's git directory located at `path`. `path`
132 * must only reference a submodule of the main repository (the_repository).
133 */
Denton Liub199d712019-04-29 04:28:20 -0400134char *git_pathdup_submodule(const char *path, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800135 __attribute__((format (printf, 2, 3)));
136
137/*
138 * Construct a path into a submodule's git directory located at `path` and
139 * append it to the provided buffer `sb`. `path` must only reference a
140 * submodule of the main repository (the_repository).
141 */
Denton Liub199d712019-04-29 04:28:20 -0400142int strbuf_git_path_submodule(struct strbuf *sb, const char *path,
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800143 const char *fmt, ...)
144 __attribute__((format (printf, 3, 4)));
145
Denton Liu55454422019-04-29 04:28:14 -0400146void report_linked_checkout_garbage(void);
Brandon Williamse7d72d02017-06-22 11:43:35 -0700147
148/*
149 * You can define a static memoized git path like:
150 *
Beat Bolli9ad36352018-07-09 21:25:35 +0200151 * static GIT_PATH_FUNC(git_path_foo, "FOO")
Brandon Williamse7d72d02017-06-22 11:43:35 -0700152 *
153 * or use one of the global ones below.
154 */
155#define GIT_PATH_FUNC(func, filename) \
156 const char *func(void) \
157 { \
158 static char *ret; \
159 if (!ret) \
160 ret = git_pathdup(filename); \
161 return ret; \
162 }
163
Stefan Beller102de882018-05-17 15:51:51 -0700164#define REPO_GIT_PATH_FUNC(var, filename) \
165 const char *git_path_##var(struct repository *r) \
166 { \
167 if (!r->cached_paths.var) \
Stefan Bellerb6b24fc2018-12-14 16:09:41 -0800168 r->cached_paths.var = repo_git_path(r, filename); \
Stefan Beller102de882018-05-17 15:51:51 -0700169 return r->cached_paths.var; \
170 }
171
Stefan Beller102de882018-05-17 15:51:51 -0700172const char *git_path_squash_msg(struct repository *r);
173const char *git_path_merge_msg(struct repository *r);
174const char *git_path_merge_rr(struct repository *r);
175const char *git_path_merge_mode(struct repository *r);
176const char *git_path_merge_head(struct repository *r);
Denton Liua03b5552020-04-07 10:28:07 -0400177const char *git_path_merge_autostash(struct repository *r);
Elijah Newren52918282021-03-20 00:03:52 +0000178const char *git_path_auto_merge(struct repository *r);
Stefan Beller102de882018-05-17 15:51:51 -0700179const char *git_path_fetch_head(struct repository *r);
180const char *git_path_shallow(struct repository *r);
Brandon Williamse7d72d02017-06-22 11:43:35 -0700181
brian m. carlsonce17feb2019-08-25 23:33:39 +0000182
183int ends_with_path_components(const char *path, const char *components);
184
Brandon Williamse7d72d02017-06-22 11:43:35 -0700185#endif /* PATH_H */