Merge branch 'ps/reftable-concurrent-writes'
[git/gitster.git] / path.h
blobe91d19fff60f457e57ac94df3e63d7db7ab2f58d
1 #ifndef PATH_H
2 #define PATH_H
4 struct repository;
5 struct strbuf;
6 struct string_list;
7 struct worktree;
9 /*
10 * The result to all functions which return statically allocated memory may be
11 * overwritten by another call to _any_ one of these functions. Consider using
12 * the safer variants which operate on strbufs or return allocated memory.
16 * Return a statically allocated path.
18 const char *mkpath(const char *fmt, ...)
19 __attribute__((format (printf, 1, 2)));
22 * Return a path.
24 char *mkpathdup(const char *fmt, ...)
25 __attribute__((format (printf, 1, 2)));
28 * The `strbuf_git_common_path` family of functions will construct a path into a
29 * repository's common git directory, which is shared by all worktrees.
33 * Constructs a path into the common git directory of repository `repo` and
34 * append it in the provided buffer `sb`.
36 void strbuf_git_common_path(struct strbuf *sb,
37 const struct repository *repo,
38 const char *fmt, ...)
39 __attribute__((format (printf, 3, 4)));
40 void repo_common_pathv(const struct repository *repo,
41 struct strbuf *buf,
42 const char *fmt,
43 va_list args);
46 * The `repo_git_path` family of functions will construct a path into a repository's
47 * git directory.
49 * These functions will perform adjustments to the resultant path to account
50 * for special paths which are either considered common among worktrees (e.g.
51 * paths into the object directory) or have been explicitly set via an
52 * environment variable or config (e.g. path to the index file).
54 * For an exhaustive list of the adjustments made look at `common_list` and
55 * `adjust_git_path` in path.c.
59 * Return a path into the git directory of repository `repo`.
61 char *repo_git_path(const struct repository *repo,
62 const char *fmt, ...)
63 __attribute__((format (printf, 2, 3)));
66 * Print a path into the git directory of repository `repo` into the provided
67 * buffer.
69 void repo_git_pathv(const struct repository *repo,
70 const struct worktree *wt, struct strbuf *buf,
71 const char *fmt, va_list args);
74 * Construct a path into the git directory of repository `repo` and append it
75 * to the provided buffer `sb`.
77 void strbuf_repo_git_path(struct strbuf *sb,
78 const struct repository *repo,
79 const char *fmt, ...)
80 __attribute__((format (printf, 3, 4)));
83 * Similar to repo_git_path() but can produce paths for a specified
84 * worktree instead of current one. When no worktree is given, then the path is
85 * computed relative to main worktree of the given repository.
87 const char *worktree_git_path(struct repository *r,
88 const struct worktree *wt,
89 const char *fmt, ...)
90 __attribute__((format (printf, 3, 4)));
93 * Return a path into the worktree of repository `repo`.
95 * If the repository doesn't have a worktree NULL is returned.
97 char *repo_worktree_path(const struct repository *repo,
98 const char *fmt, ...)
99 __attribute__((format (printf, 2, 3)));
102 * Construct a path into the worktree of repository `repo` and append it
103 * to the provided buffer `sb`.
105 * If the repository doesn't have a worktree nothing will be appended to `sb`.
107 void strbuf_repo_worktree_path(struct strbuf *sb,
108 const struct repository *repo,
109 const char *fmt, ...)
110 __attribute__((format (printf, 3, 4)));
113 * Return a path into a submodule's git directory located at `path`. `path`
114 * must only reference a submodule of the main repository (the_repository).
116 char *git_pathdup_submodule(const char *path, const char *fmt, ...)
117 __attribute__((format (printf, 2, 3)));
120 * Construct a path into a submodule's git directory located at `path` and
121 * append it to the provided buffer `sb`. `path` must only reference a
122 * submodule of the main repository (the_repository).
124 int strbuf_git_path_submodule(struct strbuf *sb, const char *path,
125 const char *fmt, ...)
126 __attribute__((format (printf, 3, 4)));
128 void report_linked_checkout_garbage(struct repository *r);
131 * You can define a static memoized git path like:
133 * static REPO_GIT_PATH_FUNC(git_path_foo, "FOO")
135 * or use one of the global ones below.
137 #define REPO_GIT_PATH_FUNC(var, filename) \
138 const char *git_path_##var(struct repository *r) \
140 if (!r->cached_paths.var) \
141 r->cached_paths.var = repo_git_path(r, filename); \
142 return r->cached_paths.var; \
145 const char *git_path_squash_msg(struct repository *r);
146 const char *git_path_merge_msg(struct repository *r);
147 const char *git_path_merge_rr(struct repository *r);
148 const char *git_path_merge_mode(struct repository *r);
149 const char *git_path_merge_head(struct repository *r);
150 const char *git_path_fetch_head(struct repository *r);
151 const char *git_path_shallow(struct repository *r);
153 int ends_with_path_components(const char *path, const char *components);
155 int calc_shared_perm(int mode);
156 int adjust_shared_perm(const char *path);
158 char *interpolate_path(const char *path, int real_home);
159 const char *enter_repo(const char *path, int strict);
160 const char *remove_leading_path(const char *in, const char *prefix);
161 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
162 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
163 int normalize_path_copy(char *dst, const char *src);
165 * Normalize in-place the path contained in the strbuf. If an error occurs,
166 * the contents of "sb" are left untouched, and -1 is returned.
168 int strbuf_normalize_path(struct strbuf *src);
169 int longest_ancestor_length(const char *path, struct string_list *prefixes);
170 char *strip_path_suffix(const char *path, const char *suffix);
171 int daemon_avoid_alias(const char *path);
174 * These functions match their is_hfs_dotgit() counterparts; see utf8.h for
175 * details.
177 int is_ntfs_dotgit(const char *name);
178 int is_ntfs_dotgitmodules(const char *name);
179 int is_ntfs_dotgitignore(const char *name);
180 int is_ntfs_dotgitattributes(const char *name);
181 int is_ntfs_dotmailmap(const char *name);
184 * Returns true iff "str" could be confused as a command-line option when
185 * passed to a sub-program like "ssh". Note that this has nothing to do with
186 * shell-quoting, which should be handled separately; we're assuming here that
187 * the string makes it verbatim to the sub-program.
189 int looks_like_command_line_option(const char *str);
192 * Return a newly allocated string with the evaluation of
193 * "$XDG_CONFIG_HOME/$subdir/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
194 * "$HOME/.config/$subdir/$filename". Return NULL upon error.
196 char *xdg_config_home_for(const char *subdir, const char *filename);
199 * Return a newly allocated string with the evaluation of
200 * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
201 * "$HOME/.config/git/$filename". Return NULL upon error.
203 char *xdg_config_home(const char *filename);
206 * Return a newly allocated string with the evaluation of
207 * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
208 * "$HOME/.cache/git/$filename". Return NULL upon error.
210 char *xdg_cache_home(const char *filename);
213 * Create a directory and (if share is nonzero) adjust its permissions
214 * according to the shared_repository setting. Only use this for
215 * directories under $GIT_DIR. Don't use it for working tree
216 * directories.
218 void safe_create_dir(const char *dir, int share);
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`.
225 struct strbuf *get_pathname(void);
227 # ifdef USE_THE_REPOSITORY_VARIABLE
228 # include "strbuf.h"
229 # include "repository.h"
232 * Return a statically allocated path into the main repository's
233 * (the_repository) common git directory.
235 __attribute__((format (printf, 1, 2)))
236 static inline const char *git_common_path(const char *fmt, ...)
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;
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.
251 __attribute__((format (printf, 2, 3)))
252 static inline char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
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;
263 * Construct a path into the main repository's (the_repository) git directory
264 * and append it to the provided buffer `sb`.
266 __attribute__((format (printf, 2, 3)))
267 static inline void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
269 va_list args;
270 va_start(args, fmt);
271 repo_git_pathv(the_repository, NULL, sb, fmt, args);
272 va_end(args);
276 * Return a statically allocated path into the main repository's
277 * (the_repository) git directory.
279 __attribute__((format (printf, 1, 2)))
280 static inline const char *git_path(const char *fmt, ...)
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;
290 #define GIT_PATH_FUNC(func, filename) \
291 const char *func(void) \
293 static char *ret; \
294 if (!ret) \
295 ret = git_pathdup(filename); \
296 return ret; \
300 * Return a path into the main repository's (the_repository) git directory.
302 __attribute__((format (printf, 1, 2)))
303 static inline char *git_pathdup(const char *fmt, ...)
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);
313 # endif /* USE_THE_REPOSITORY_VARIABLE */
315 #endif /* PATH_H */