]> git.ipfire.org Git - thirdparty/git.git/blame - worktree.h
The fifth batch
[thirdparty/git.git] / worktree.h
CommitLineData
ac6c561b
MR
1#ifndef WORKTREE_H
2#define WORKTREE_H
3
d0c39a49
NTND
4#include "refs.h"
5
4ddddc1f
NTND
6struct strbuf;
7
51934904 8struct worktree {
dc7fb4f7
PS
9 /* The repository this worktree belongs to. */
10 struct repository *repo;
51934904 11 char *path;
69dfe3b9 12 char *id;
fa099d23 13 char *head_ref; /* NULL if HEAD is broken or detached */
d236f12b 14 char *lock_reason; /* private - use worktree_lock_reason */
fc0c7d5e 15 char *prune_reason; /* private - use worktree_prune_reason */
0f05154c 16 struct object_id head_oid;
92718b74
MR
17 int is_detached;
18 int is_bare;
750e8a60 19 int is_current;
e21cc076 20 int lock_reason_valid; /* private */
fc0c7d5e 21 int prune_reason_valid; /* private */
51934904
MR
22};
23
51934904
MR
24/*
25 * Get the worktrees. The primary worktree will always be the first returned,
d9c54c2b 26 * and linked worktrees will follow in no particular order.
51934904
MR
27 *
28 * The caller is responsible for freeing the memory from the returned
d9c54c2b 29 * worktrees by calling free_worktrees().
51934904 30 */
03f2465b 31struct worktree **get_worktrees(void);
51934904 32
fdf3820b 33/*
34 * Like `get_worktrees`, but does not read HEAD. Skip reading HEAD allows to
35 * get the worktree without worrying about failures pertaining to parsing
36 * the HEAD ref. This is useful in contexts where it is assumed that the
37 * refdb may not be in a consistent state.
38 */
39struct worktree **get_worktrees_without_reading_head(void);
40
1a248cf2
SB
41/*
42 * Returns 1 if linked worktrees exist, 0 otherwise.
43 */
55454427 44int submodule_uses_worktrees(const char *path);
1a248cf2 45
69dfe3b9
NTND
46/*
47 * Return git dir of the worktree. Note that the path may be relative.
48 * If wt is NULL, git dir of current worktree is returned.
49 */
8e4710f0 50char *get_worktree_git_dir(const struct worktree *wt);
69dfe3b9 51
68353144 52/*
a80c4c22
ES
53 * Search for the worktree identified unambiguously by `arg` -- typically
54 * supplied by the user via the command-line -- which may be a pathname or some
55 * shorthand uniquely identifying a worktree, thus making it convenient for the
56 * user to specify a worktree with minimal typing. For instance, if the last
57 * component (say, "foo") of a worktree's pathname is unique among worktrees
58 * (say, "work/foo" and "work/bar"), it can be used to identify the worktree
59 * unambiguously.
60 *
61 * `prefix` should be the `prefix` handed to top-level Git commands along with
62 * `argc` and `argv`.
63 *
64 * Return the worktree identified by `arg`, or NULL if not found.
68353144 65 */
55454427 66struct worktree *find_worktree(struct worktree **list,
ad6dad09
DL
67 const char *prefix,
68 const char *arg);
68353144 69
b8a846b2
PS
70/*
71 * Look up the worktree corresponding to `id`, or NULL of no such worktree
72 * exists.
73 */
74struct worktree *get_linked_worktree(const char *id,
75 int skip_reading_head);
76
bb4995fc
ES
77/*
78 * Return the worktree corresponding to `path`, or NULL if no such worktree
79 * exists.
80 */
81struct worktree *find_worktree_by_path(struct worktree **, const char *path);
82
984ad9e5
NTND
83/*
84 * Return true if the given worktree is the main one.
85 */
55454427 86int is_main_worktree(const struct worktree *wt);
984ad9e5 87
346ef530
NTND
88/*
89 * Return the reason string if the given worktree is locked or NULL
90 * otherwise.
91 */
55454427 92const char *worktree_lock_reason(struct worktree *wt);
346ef530 93
fc0c7d5e
RS
94/*
95 * Return the reason string if the given worktree should be pruned, otherwise
96 * NULL if it should not be pruned. `expire` defines a grace period to prune
97 * the worktree when its path does not exist.
98 */
99const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
100
a29a8b75
RS
101/*
102 * Return true if worktree entry should be pruned, along with the reason for
103 * pruning. Otherwise, return false and the worktree's path in `wtpath`, or
104 * NULL if it cannot be determined. Caller is responsible for freeing
105 * returned path.
106 *
107 * `expire` defines a grace period to prune the worktree when its path
108 * does not exist.
109 */
110int should_prune_worktree(const char *id,
111 struct strbuf *reason,
112 char **wtpath,
113 timestamp_t expire);
114
ee6763af
NTND
115#define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
116
4ddddc1f
NTND
117/*
118 * Return zero if the worktree is in good condition. Error message is
119 * returned if "errmsg" is not NULL.
120 */
55454427 121int validate_worktree(const struct worktree *wt,
ad6dad09
DL
122 struct strbuf *errmsg,
123 unsigned flags);
4ddddc1f 124
9c620fc7
NTND
125/*
126 * Update worktrees/xxx/gitdir with the new path.
127 */
298d2917
CW
128void update_worktree_location(struct worktree *wt, const char *path_,
129 int use_relative_paths);
9c620fc7 130
bdd1f3e4
ES
131typedef void (* worktree_repair_fn)(int iserr, const char *path,
132 const char *msg, void *cb_data);
133
134/*
135 * Visit each registered linked worktree and repair corruptions. For each
136 * repair made or error encountered while attempting a repair, the callback
137 * function, if non-NULL, is called with the path of the worktree and a
138 * description of the repair or error, along with the callback user-data.
139 */
e6df1ee2 140void repair_worktrees(worktree_repair_fn, void *cb_data, int use_relative_paths);
bdd1f3e4 141
717af916
CW
142/*
143 * Repair the linked worktrees after the gitdir has been moved.
144 */
145void repair_worktrees_after_gitdir_move(const char *old_path);
146
147/*
148 * Repair the linked worktree after the gitdir has been moved.
149 */
150void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path);
151
b214ab5a
ES
152/*
153 * Repair administrative files corresponding to the worktree at the given path.
154 * The worktree's .git file pointing at the repository must be intact for the
155 * repair to succeed. Useful for re-associating an orphaned worktree with the
156 * repository if the worktree has been moved manually (without using "git
157 * worktree move"). For each repair made or error encountered while attempting
158 * a repair, the callback function, if non-NULL, is called with the path of the
159 * worktree and a description of the repair or error, along with the callback
160 * user-data.
161 */
e6df1ee2
CW
162void repair_worktree_at_path(const char *, worktree_repair_fn,
163 void *cb_data, int use_relative_paths);
b214ab5a 164
b8a846b2
PS
165/*
166 * Free up the memory for a worktree.
167 */
168void free_worktree(struct worktree *);
169
51934904
MR
170/*
171 * Free up the memory for worktree(s)
172 */
55454427 173void free_worktrees(struct worktree **);
51934904 174
ac6c561b
MR
175/*
176 * Check if a per-worktree symref points to a ref in the main worktree
d3b9ac07 177 * or any linked worktree, and return the worktree that holds the ref,
c8dd491f 178 * or NULL otherwise.
ac6c561b 179 */
c8dd491f
AK
180const struct worktree *find_shared_symref(struct worktree **worktrees,
181 const char *symref,
ad6dad09 182 const char *target);
ac6c561b 183
662078ca
RJ
184/*
185 * Returns true if a symref points to a ref in a worktree.
186 */
187int is_shared_symref(const struct worktree *wt,
188 const char *symref, const char *target);
189
d0c39a49
NTND
190/*
191 * Similar to head_ref() for all HEADs _except_ one from the current
192 * worktree, which is covered by head_ref().
193 */
194int other_head_refs(each_ref_fn fn, void *cb_data);
195
14ace5b7
NTND
196int is_worktree_being_rebased(const struct worktree *wt, const char *target);
197int is_worktree_being_bisected(const struct worktree *wt, const char *target);
198
ab3e1f78
NTND
199/*
200 * Return a refname suitable for access from the current ref store.
201 */
202void strbuf_worktree_ref(const struct worktree *wt,
203 struct strbuf *sb,
204 const char *refname);
205
615a84ad
DS
206/**
207 * Enable worktree config for the first time. This will make the following
208 * adjustments:
209 *
210 * 1. Add extensions.worktreeConfig=true in the common config file.
211 *
212 * 2. If the common config file has a core.worktree value, then that value
213 * is moved to the main worktree's config.worktree file.
214 *
215 * 3. If the common config file has a core.bare enabled, then that value
216 * is moved to the main worktree's config.worktree file.
217 *
218 * If extensions.worktreeConfig is already true, then this method
219 * terminates early without any of the above steps. The existing config
220 * arrangement is assumed to be intentional.
221 *
222 * Returns 0 on success. Reports an error message and returns non-zero
223 * if any of these steps fail.
224 */
225int init_worktree_config(struct repository *r);
226
4dac9e3c
CW
227/**
228 * Write the .git file and gitdir file that links the worktree to the repository.
229 *
230 * The `dotgit` parameter is the path to the worktree's .git file, and `gitdir`
231 * is the path to the repository's `gitdir` file.
232 *
233 * Example
234 * dotgit: "/path/to/foo/.git"
235 * gitdir: "/path/to/repo/worktrees/foo/gitdir"
236 */
237void write_worktree_linking_files(struct strbuf dotgit, struct strbuf gitdir,
238 int use_relative_paths);
239
ac6c561b 240#endif