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