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