blob: 639372edd9ee36127e3b89084f89ee59d12ff60f [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;
Elijah Newren905f9692023-03-21 06:26:00 +00006struct string_list;
Brandon Williamsb3371722017-06-22 11:43:37 -07007
Brandon Williamse7d72d02017-06-22 11:43:35 -07008/*
Brandon Williamsc07b3ad2017-12-13 10:28:02 -08009 * The result to all functions which return statically allocated memory may be
10 * overwritten by another call to _any_ one of these functions. Consider using
11 * the safer variants which operate on strbufs or return allocated memory.
Brandon Williamse7d72d02017-06-22 11:43:35 -070012 */
Brandon Williamse7d72d02017-06-22 11:43:35 -070013
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080014/*
15 * Return a statically allocated path.
16 */
Denton Liub199d712019-04-29 04:28:20 -040017const char *mkpath(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080018 __attribute__((format (printf, 1, 2)));
19
20/*
21 * Return a path.
22 */
Denton Liub199d712019-04-29 04:28:20 -040023char *mkpathdup(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080024 __attribute__((format (printf, 1, 2)));
25
26/*
27 * Construct a path and place the result in the provided buffer `buf`.
28 */
Denton Liub199d712019-04-29 04:28:20 -040029char *mksnpath(char *buf, size_t n, const char *fmt, ...)
Brandon Williamse7d72d02017-06-22 11:43:35 -070030 __attribute__((format (printf, 3, 4)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080031
32/*
33 * The `git_common_path` family of functions will construct a path into a
34 * repository's common git directory, which is shared by all worktrees.
35 */
36
37/*
38 * Constructs a path into the common git directory of repository `repo` and
39 * append it in the provided buffer `sb`.
40 */
Denton Liub199d712019-04-29 04:28:20 -040041void strbuf_git_common_path(struct strbuf *sb,
Denton Liuad6dad02019-04-29 04:28:23 -040042 const struct repository *repo,
43 const char *fmt, ...)
Brandon Williamsb3371722017-06-22 11:43:37 -070044 __attribute__((format (printf, 3, 4)));
Brandon Williamse7d72d02017-06-22 11:43:35 -070045
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080046/*
47 * Return a statically allocated path into the main repository's
48 * (the_repository) common git directory.
49 */
Denton Liub199d712019-04-29 04:28:20 -040050const char *git_common_path(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080051 __attribute__((format (printf, 1, 2)));
52
53
54/*
55 * The `git_path` family of functions will construct a path into a repository's
56 * git directory.
57 *
58 * These functions will perform adjustments to the resultant path to account
59 * for special paths which are either considered common among worktrees (e.g.
60 * paths into the object directory) or have been explicitly set via an
61 * environment variable or config (e.g. path to the index file).
62 *
63 * For an exhaustive list of the adjustments made look at `common_list` and
64 * `adjust_git_path` in path.c.
65 */
66
67/*
68 * Return a path into the git directory of repository `repo`.
69 */
Denton Liub199d712019-04-29 04:28:20 -040070char *repo_git_path(const struct repository *repo,
Denton Liuad6dad02019-04-29 04:28:23 -040071 const char *fmt, ...)
Brandon Williams3181d862017-06-22 11:43:40 -070072 __attribute__((format (printf, 2, 3)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080073
74/*
75 * Construct a path into the git directory of repository `repo` and append it
76 * to the provided buffer `sb`.
77 */
Denton Liub199d712019-04-29 04:28:20 -040078void strbuf_repo_git_path(struct strbuf *sb,
Denton Liuad6dad02019-04-29 04:28:23 -040079 const struct repository *repo,
80 const char *fmt, ...)
Brandon Williams3181d862017-06-22 11:43:40 -070081 __attribute__((format (printf, 3, 4)));
82
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080083/*
84 * Return a statically allocated path into the main repository's
85 * (the_repository) git directory.
86 */
Denton Liub199d712019-04-29 04:28:20 -040087const char *git_path(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080088 __attribute__((format (printf, 1, 2)));
89
90/*
91 * Return a path into the main repository's (the_repository) git directory.
92 */
Denton Liub199d712019-04-29 04:28:20 -040093char *git_pathdup(const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -080094 __attribute__((format (printf, 1, 2)));
95
96/*
97 * Construct a path into the main repository's (the_repository) git directory
98 * and place it in the provided buffer `buf`, the contents of the buffer will
99 * be overridden.
100 */
Denton Liub199d712019-04-29 04:28:20 -0400101char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800102 __attribute__((format (printf, 2, 3)));
103
104/*
105 * Construct a path into the main repository's (the_repository) git directory
106 * and append it to the provided buffer `sb`.
107 */
Denton Liub199d712019-04-29 04:28:20 -0400108void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800109 __attribute__((format (printf, 2, 3)));
110
111/*
112 * Return a path into the worktree of repository `repo`.
113 *
114 * If the repository doesn't have a worktree NULL is returned.
115 */
Denton Liub199d712019-04-29 04:28:20 -0400116char *repo_worktree_path(const struct repository *repo,
Brandon Williamsb42b0c02017-06-22 11:43:41 -0700117 const char *fmt, ...)
118 __attribute__((format (printf, 2, 3)));
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800119
120/*
121 * Construct a path into the worktree of repository `repo` and append it
122 * to the provided buffer `sb`.
123 *
124 * If the repository doesn't have a worktree nothing will be appended to `sb`.
125 */
Denton Liub199d712019-04-29 04:28:20 -0400126void strbuf_repo_worktree_path(struct strbuf *sb,
Brandon Williamsb42b0c02017-06-22 11:43:41 -0700127 const struct repository *repo,
128 const char *fmt, ...)
129 __attribute__((format (printf, 3, 4)));
130
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800131/*
132 * Return a path into a submodule's git directory located at `path`. `path`
133 * must only reference a submodule of the main repository (the_repository).
134 */
Denton Liub199d712019-04-29 04:28:20 -0400135char *git_pathdup_submodule(const char *path, const char *fmt, ...)
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800136 __attribute__((format (printf, 2, 3)));
137
138/*
139 * Construct a path into a submodule's git directory located at `path` and
140 * append it to the provided buffer `sb`. `path` must only reference a
141 * submodule of the main repository (the_repository).
142 */
Denton Liub199d712019-04-29 04:28:20 -0400143int strbuf_git_path_submodule(struct strbuf *sb, const char *path,
Brandon Williamsc07b3ad2017-12-13 10:28:02 -0800144 const char *fmt, ...)
145 __attribute__((format (printf, 3, 4)));
146
Denton Liu55454422019-04-29 04:28:14 -0400147void report_linked_checkout_garbage(void);
Brandon Williamse7d72d02017-06-22 11:43:35 -0700148
149/*
150 * You can define a static memoized git path like:
151 *
Beat Bolli9ad36352018-07-09 21:25:35 +0200152 * static GIT_PATH_FUNC(git_path_foo, "FOO")
Brandon Williamse7d72d02017-06-22 11:43:35 -0700153 *
154 * or use one of the global ones below.
155 */
156#define GIT_PATH_FUNC(func, filename) \
157 const char *func(void) \
158 { \
159 static char *ret; \
160 if (!ret) \
161 ret = git_pathdup(filename); \
162 return ret; \
163 }
164
Stefan Beller102de882018-05-17 15:51:51 -0700165#define REPO_GIT_PATH_FUNC(var, filename) \
166 const char *git_path_##var(struct repository *r) \
167 { \
168 if (!r->cached_paths.var) \
Stefan Bellerb6b24fc2018-12-14 16:09:41 -0800169 r->cached_paths.var = repo_git_path(r, filename); \
Stefan Beller102de882018-05-17 15:51:51 -0700170 return r->cached_paths.var; \
171 }
172
Stefan Beller102de882018-05-17 15:51:51 -0700173const char *git_path_squash_msg(struct repository *r);
174const char *git_path_merge_msg(struct repository *r);
175const char *git_path_merge_rr(struct repository *r);
176const char *git_path_merge_mode(struct repository *r);
177const char *git_path_merge_head(struct repository *r);
Denton Liua03b5552020-04-07 10:28:07 -0400178const char *git_path_merge_autostash(struct repository *r);
Elijah Newren52918282021-03-20 00:03:52 +0000179const char *git_path_auto_merge(struct repository *r);
Stefan Beller102de882018-05-17 15:51:51 -0700180const char *git_path_fetch_head(struct repository *r);
181const char *git_path_shallow(struct repository *r);
Brandon Williamse7d72d02017-06-22 11:43:35 -0700182
brian m. carlsonce17feb2019-08-25 23:33:39 +0000183int ends_with_path_components(const char *path, const char *components);
Elijah Newren905f9692023-03-21 06:26:00 +0000184int validate_headref(const char *ref);
185
186int adjust_shared_perm(const char *path);
187
188char *interpolate_path(const char *path, int real_home);
189const char *enter_repo(const char *path, int strict);
190const char *remove_leading_path(const char *in, const char *prefix);
191const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
192int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
193int normalize_path_copy(char *dst, const char *src);
Calvin Wanaba07062023-06-06 19:48:42 +0000194/**
195 * Normalize in-place the path contained in the strbuf. If an error occurs,
196 * the contents of "sb" are left untouched, and -1 is returned.
197 */
198int strbuf_normalize_path(struct strbuf *src);
Elijah Newren905f9692023-03-21 06:26:00 +0000199int longest_ancestor_length(const char *path, struct string_list *prefixes);
200char *strip_path_suffix(const char *path, const char *suffix);
201int daemon_avoid_alias(const char *path);
202
203/*
204 * These functions match their is_hfs_dotgit() counterparts; see utf8.h for
205 * details.
206 */
207int is_ntfs_dotgit(const char *name);
208int is_ntfs_dotgitmodules(const char *name);
209int is_ntfs_dotgitignore(const char *name);
210int is_ntfs_dotgitattributes(const char *name);
211int is_ntfs_dotmailmap(const char *name);
212
213/*
214 * Returns true iff "str" could be confused as a command-line option when
215 * passed to a sub-program like "ssh". Note that this has nothing to do with
216 * shell-quoting, which should be handled separately; we're assuming here that
217 * the string makes it verbatim to the sub-program.
218 */
219int looks_like_command_line_option(const char *str);
220
221/**
222 * Return a newly allocated string with the evaluation of
223 * "$XDG_CONFIG_HOME/$subdir/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
224 * "$HOME/.config/$subdir/$filename". Return NULL upon error.
225 */
226char *xdg_config_home_for(const char *subdir, const char *filename);
227
228/**
229 * Return a newly allocated string with the evaluation of
230 * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
231 * "$HOME/.config/git/$filename". Return NULL upon error.
232 */
233char *xdg_config_home(const char *filename);
234
235/**
236 * Return a newly allocated string with the evaluation of
237 * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
238 * "$HOME/.cache/git/$filename". Return NULL upon error.
239 */
240char *xdg_cache_home(const char *filename);
241
242/*
243 * Create a directory and (if share is nonzero) adjust its permissions
244 * according to the shared_repository setting. Only use this for
245 * directories under $GIT_DIR. Don't use it for working tree
246 * directories.
247 */
248void safe_create_dir(const char *dir, int share);
brian m. carlsonce17feb2019-08-25 23:33:39 +0000249
Brandon Williamse7d72d02017-06-22 11:43:35 -0700250#endif /* PATH_H */