interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / path.h

309 lines · 10884 bytes Raw Blame History
1#ifndef PATH_H
2#define PATH_H
3
4struct repository;
5struct strbuf;
6struct string_list;
7struct worktree;
8
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.
13 */
14
15/*
16 * Return a statically allocated path.
17 */
18const char *mkpath(const char *fmt, ...)
19 __attribute__((format (printf, 1, 2)));
20
21/*
22 * Return a path.
23 */
24char *mkpathdup(const char *fmt, ...)
25 __attribute__((format (printf, 1, 2)));
26
27/*
28 * The `repo_common_path` family of functions will construct a path into a
29 * repository's common git directory, which is shared by all worktrees.
30 */
31char *repo_common_path(const struct repository *repo,
32 const char *fmt, ...)
33 __attribute__((format (printf, 2, 3)));
34const char *repo_common_path_append(const struct repository *repo,
35 struct strbuf *sb,
36 const char *fmt, ...)
37 __attribute__((format (printf, 3, 4)));
38const char *repo_common_path_replace(const struct repository *repo,
39 struct strbuf *sb,
40 const char *fmt, ...)
41 __attribute__((format (printf, 3, 4)));
42
43/*
44 * The `repo_git_path` family of functions will construct a path into a repository's
45 * git directory.
46 *
47 * These functions will perform adjustments to the resultant path to account
48 * for special paths which are either considered common among worktrees (e.g.
49 * paths into the object directory) or have been explicitly set via an
50 * environment variable or config (e.g. path to the index file).
51 *
52 * For an exhaustive list of the adjustments made look at `common_list` and
53 * `adjust_git_path` in path.c.
54 */
55char *repo_git_path(struct repository *repo,
56 const char *fmt, ...)
57 __attribute__((format (printf, 2, 3)));
58const char *repo_git_path_append(struct repository *repo,
59 struct strbuf *sb,
60 const char *fmt, ...)
61 __attribute__((format (printf, 3, 4)));
62const char *repo_git_path_replace(struct repository *repo,
63 struct strbuf *sb,
64 const char *fmt, ...)
65 __attribute__((format (printf, 3, 4)));
66
67/*
68 * Similar to repo_git_path() but can produce paths for a specified
69 * worktree instead of current one.
70 */
71const char *worktree_git_path(const struct worktree *wt,
72 const char *fmt, ...)
73 __attribute__((format (printf, 2, 3)));
74
75/*
76 * The `repo_worktree_path` family of functions will construct a path into a
77 * repository's worktree.
78 *
79 * Returns a `NULL` pointer in case the repository has no worktree.
80 */
81char *repo_worktree_path(const struct repository *repo,
82 const char *fmt, ...)
83 __attribute__((format (printf, 2, 3)));
84const char *repo_worktree_path_append(const struct repository *repo,
85 struct strbuf *sb,
86 const char *fmt, ...)
87 __attribute__((format (printf, 3, 4)));
88const char *repo_worktree_path_replace(const struct repository *repo,
89 struct strbuf *sb,
90 const char *fmt, ...)
91 __attribute__((format (printf, 3, 4)));
92
93/*
94 * The `repo_submodule_path` family of functions will construct a path into a
95 * submodule's git directory located at `path`. `path` must be a submodule path
96 * as found in the index and must be part of the given repository.
97 *
98 * Returns a `NULL` pointer in case the submodule cannot be found.
99 */
100char *repo_submodule_path(struct repository *repo,
101 const char *path,
102 const char *fmt, ...)
103 __attribute__((format (printf, 3, 4)));
104const char *repo_submodule_path_append(struct repository *repo,
105 struct strbuf *sb,
106 const char *path,
107 const char *fmt, ...)
108 __attribute__((format (printf, 4, 5)));
109const char *repo_submodule_path_replace(struct repository *repo,
110 struct strbuf *sb,
111 const char *path,
112 const char *fmt, ...)
113 __attribute__((format (printf, 4, 5)));
114
115/*
116 * Given a directory name 'dir' (not ending with a trailing '/'),
117 * determine if 'buf' is equal to 'dir' or has prefix 'dir'+'/'.
118 */
119int dir_prefix(const char *buf, const char *dir);
120
121void report_linked_checkout_garbage(struct repository *r);
122
123/*
124 * You can define a static memoized git path like:
125 *
126 * static REPO_GIT_PATH_FUNC(git_path_foo, "FOO")
127 *
128 * or use one of the global ones below.
129 */
130#define REPO_GIT_PATH_FUNC(var, filename) \
131 const char *git_path_##var(struct repository *r) \
132 { \
133 if (!r->cached_paths.var) \
134 r->cached_paths.var = repo_git_path(r, filename); \
135 return r->cached_paths.var; \
136 }
137
138const char *git_path_squash_msg(struct repository *r);
139const char *git_path_merge_msg(struct repository *r);
140const char *git_path_merge_rr(struct repository *r);
141const char *git_path_merge_mode(struct repository *r);
142const char *git_path_merge_head(struct repository *r);
143const char *git_path_fetch_head(struct repository *r);
144const char *git_path_shallow(struct repository *r);
145const char *git_path_merge_labels(struct repository *r);
146
147int ends_with_path_components(const char *path, const char *components);
148
149int calc_shared_perm(struct repository *repo, int mode);
150int adjust_shared_perm(struct repository *repo, const char *path);
151
152char *interpolate_path(const char *path, int real_home);
153
154const char *remove_leading_path(const char *in, const char *prefix);
155const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
156int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
157int normalize_path_copy(char *dst, const char *src);
158/**
159 * Normalize in-place the path contained in the strbuf. If an error occurs,
160 * the contents of "sb" are left untouched, and -1 is returned.
161 */
162int strbuf_normalize_path(struct strbuf *src);
163int longest_ancestor_length(const char *path, struct string_list *prefixes);
164char *strip_path_suffix(const char *path, const char *suffix);
165int daemon_avoid_alias(const char *path);
166
167/*
168 * These functions match their is_hfs_dotgit() counterparts; see utf8.h for
169 * details.
170 */
171int is_ntfs_dotgit(const char *name);
172int is_ntfs_dotgitmodules(const char *name);
173int is_ntfs_dotgitignore(const char *name);
174int is_ntfs_dotgitattributes(const char *name);
175int is_ntfs_dotmailmap(const char *name);
176
177/*
178 * Returns true iff "str" could be confused as a command-line option when
179 * passed to a sub-program like "ssh". Note that this has nothing to do with
180 * shell-quoting, which should be handled separately; we're assuming here that
181 * the string makes it verbatim to the sub-program.
182 */
183int looks_like_command_line_option(const char *str);
184
185/**
186 * Return a newly allocated string with the evaluation of
187 * "$XDG_CONFIG_HOME/$subdir/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
188 * "$HOME/.config/$subdir/$filename". Return NULL upon error.
189 */
190char *xdg_config_home_for(const char *subdir, const char *filename);
191
192/**
193 * Return a newly allocated string with the evaluation of
194 * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
195 * "$HOME/.config/git/$filename". Return NULL upon error.
196 */
197char *xdg_config_home(const char *filename);
198
199/**
200 * Return a newly allocated string with the evaluation of
201 * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
202 * "$HOME/.cache/git/$filename". Return NULL upon error.
203 */
204char *xdg_cache_home(const char *filename);
205
206/*
207 * Create a directory and (if share is nonzero) adjust its permissions
208 * according to the shared_repository setting. Only use this for
209 * directories under $GIT_DIR. Don't use it for working tree
210 * directories.
211 */
212void safe_create_dir(struct repository *repo, const char *dir, int share);
213
214/*
215 * Similar to `safe_create_dir()`, but with two differences:
216 *
217 * - It knows to resolve gitlink files for symlinked worktrees.
218 *
219 * - It always adjusts shared permissions.
220 *
221 * Returns a negative error code on error, 0 on success.
222 */
223int safe_create_dir_in_gitdir(struct repository *repo, const char *path);
224
225/*
226 * Create the directory containing the named path, using care to be
227 * somewhat safe against races. Return one of the scld_error values to
228 * indicate success/failure. On error, set errno to describe the
229 * problem.
230 *
231 * SCLD_VANISHED indicates that one of the ancestor directories of the
232 * path existed at one point during the function call and then
233 * suddenly vanished, probably because another process pruned the
234 * directory while we were working. To be robust against this kind of
235 * race, callers might want to try invoking the function again when it
236 * returns SCLD_VANISHED.
237 *
238 * The default variants honor "core.sharedRepository" and temporarily modify
239 * `path`. Note that this configuration should be honored for all files in the
240 * git directory. The `no_share()` variants ignore "core.sharedRepository",
241 * and should be used for working tree files. The `const()` variants do not
242 * modify `path`.
243 */
244enum scld_error {
245 SCLD_OK = 0,
246 SCLD_FAILED = -1,
247 SCLD_PERMS = -2,
248 SCLD_EXISTS = -3,
249 SCLD_VANISHED = -4
250};
251enum scld_error safe_create_leading_directories(struct repository *repo, char *path);
252enum scld_error safe_create_leading_directories_const(struct repository *repo,
253 const char *path);
254enum scld_error safe_create_leading_directories_no_share(char *path);
255enum scld_error safe_create_leading_directories_no_share_const(const char *path);
256
257/*
258 * Create a file, potentially creating its leading directories in case they
259 * don't exist. Returns the return value of the open(3p) call.
260 */
261int safe_create_file_with_leading_directories(struct repository *repo,
262 const char *path);
263
264/**
265 * The formatting strategy to apply when writing a path into a buffer.
266 */
267enum path_format {
268 /* Output the path exactly as-is without any modifications. */
269 PATH_FORMAT_UNMODIFIED,
270
271 /* Output a path relative to the provided directory prefix. */
272 PATH_FORMAT_RELATIVE,
273
274 /* Output a relative path only if the path shares a root with the prefix. */
275 PATH_FORMAT_RELATIVE_IF_SHARED,
276
277 /* Output a fully resolved, absolute canonical path. */
278 PATH_FORMAT_CANONICAL
279};
280
281/**
282 * Format a path according to the specified formatting strategy and store
283 * the result in the given strbuf, replacing any existing contents.
284 *
285 * `dest` : The string buffer to store the formatted path into.
286 * `path` : The path string that needs to be formatted.
287 * `prefix` : The directory prefix to calculate relative offsets against.
288 * Pass NULL to default to the current working directory where applicable.
289 * `format` : The formatting behavior rule to execute.
290 */
291void format_path(struct strbuf *dest, const char *path,
292 const char *prefix, enum path_format format);
293
294# ifdef USE_THE_REPOSITORY_VARIABLE
295# include "strbuf.h"
296# include "repository.h"
297
298#define GIT_PATH_FUNC(func, filename) \
299 const char *func(void) \
300 { \
301 static char *ret; \
302 if (!ret) \
303 ret = repo_git_path(the_repository, filename); \
304 return ret; \
305 }
306
307# endif /* USE_THE_REPOSITORY_VARIABLE */
308
309#endif /* PATH_H */