The fifth batch
[alt-git.git] / path.h
blob5f6c85e5f8d795f14a20c008cbb47578a870292f
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);
160 /* The bits are as follows:
162 * - ENTER_REPO_STRICT: callers that require exact paths (as opposed
163 * to allowing known suffixes like ".git", ".git/.git" to be
164 * omitted) can set this bit.
166 * - ENTER_REPO_ANY_OWNER_OK: callers that are willing to run without
167 * ownership check can set this bit.
169 enum {
170 ENTER_REPO_STRICT = (1<<0),
171 ENTER_REPO_ANY_OWNER_OK = (1<<1),
174 const char *enter_repo(const char *path, unsigned flags);
175 const char *remove_leading_path(const char *in, const char *prefix);
176 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
177 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
178 int normalize_path_copy(char *dst, const char *src);
180 * Normalize in-place the path contained in the strbuf. If an error occurs,
181 * the contents of "sb" are left untouched, and -1 is returned.
183 int strbuf_normalize_path(struct strbuf *src);
184 int longest_ancestor_length(const char *path, struct string_list *prefixes);
185 char *strip_path_suffix(const char *path, const char *suffix);
186 int daemon_avoid_alias(const char *path);
189 * These functions match their is_hfs_dotgit() counterparts; see utf8.h for
190 * details.
192 int is_ntfs_dotgit(const char *name);
193 int is_ntfs_dotgitmodules(const char *name);
194 int is_ntfs_dotgitignore(const char *name);
195 int is_ntfs_dotgitattributes(const char *name);
196 int is_ntfs_dotmailmap(const char *name);
199 * Returns true iff "str" could be confused as a command-line option when
200 * passed to a sub-program like "ssh". Note that this has nothing to do with
201 * shell-quoting, which should be handled separately; we're assuming here that
202 * the string makes it verbatim to the sub-program.
204 int looks_like_command_line_option(const char *str);
207 * Return a newly allocated string with the evaluation of
208 * "$XDG_CONFIG_HOME/$subdir/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
209 * "$HOME/.config/$subdir/$filename". Return NULL upon error.
211 char *xdg_config_home_for(const char *subdir, const char *filename);
214 * Return a newly allocated string with the evaluation of
215 * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
216 * "$HOME/.config/git/$filename". Return NULL upon error.
218 char *xdg_config_home(const char *filename);
221 * Return a newly allocated string with the evaluation of
222 * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
223 * "$HOME/.cache/git/$filename". Return NULL upon error.
225 char *xdg_cache_home(const char *filename);
228 * Create a directory and (if share is nonzero) adjust its permissions
229 * according to the shared_repository setting. Only use this for
230 * directories under $GIT_DIR. Don't use it for working tree
231 * directories.
233 void safe_create_dir(const char *dir, int share);
236 * Do not use this function. It is only exported to other subsystems until we
237 * can get rid of the below block of functions that implicitly rely on
238 * `the_repository`.
240 struct strbuf *get_pathname(void);
242 # ifdef USE_THE_REPOSITORY_VARIABLE
243 # include "strbuf.h"
244 # include "repository.h"
247 * Return a statically allocated path into the main repository's
248 * (the_repository) common git directory.
250 __attribute__((format (printf, 1, 2)))
251 static inline const char *git_common_path(const char *fmt, ...)
253 struct strbuf *pathname = get_pathname();
254 va_list args;
255 va_start(args, fmt);
256 repo_common_pathv(the_repository, pathname, fmt, args);
257 va_end(args);
258 return pathname->buf;
262 * Construct a path into the main repository's (the_repository) git directory
263 * and place it in the provided buffer `buf`, the contents of the buffer will
264 * be overridden.
266 __attribute__((format (printf, 2, 3)))
267 static inline char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
269 va_list args;
270 strbuf_reset(buf);
271 va_start(args, fmt);
272 repo_git_pathv(the_repository, NULL, buf, fmt, args);
273 va_end(args);
274 return buf->buf;
278 * Construct a path into the main repository's (the_repository) git directory
279 * and append it to the provided buffer `sb`.
281 __attribute__((format (printf, 2, 3)))
282 static inline void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
284 va_list args;
285 va_start(args, fmt);
286 repo_git_pathv(the_repository, NULL, sb, fmt, args);
287 va_end(args);
291 * Return a statically allocated path into the main repository's
292 * (the_repository) git directory.
294 __attribute__((format (printf, 1, 2)))
295 static inline const char *git_path(const char *fmt, ...)
297 struct strbuf *pathname = get_pathname();
298 va_list args;
299 va_start(args, fmt);
300 repo_git_pathv(the_repository, NULL, pathname, fmt, args);
301 va_end(args);
302 return pathname->buf;
305 #define GIT_PATH_FUNC(func, filename) \
306 const char *func(void) \
308 static char *ret; \
309 if (!ret) \
310 ret = git_pathdup(filename); \
311 return ret; \
315 * Return a path into the main repository's (the_repository) git directory.
317 __attribute__((format (printf, 1, 2)))
318 static inline char *git_pathdup(const char *fmt, ...)
320 struct strbuf path = STRBUF_INIT;
321 va_list args;
322 va_start(args, fmt);
323 repo_git_pathv(the_repository, NULL, &path, fmt, args);
324 va_end(args);
325 return strbuf_detach(&path, NULL);
328 # endif /* USE_THE_REPOSITORY_VARIABLE */
330 #endif /* PATH_H */