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