]> git.ipfire.org Git - thirdparty/git.git/blame - worktree.h
worktree: libify should_prune_worktree()
[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 */
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 */
51934904
MR
19};
20
51934904
MR
21/*
22 * Get the worktrees. The primary worktree will always be the first returned,
d9c54c2b 23 * and linked worktrees will follow in no particular order.
51934904
MR
24 *
25 * The caller is responsible for freeing the memory from the returned
d9c54c2b 26 * worktrees by calling free_worktrees().
51934904 27 */
03f2465b 28struct worktree **get_worktrees(void);
51934904 29
1a248cf2
SB
30/*
31 * Returns 1 if linked worktrees exist, 0 otherwise.
32 */
55454427 33int submodule_uses_worktrees(const char *path);
1a248cf2 34
69dfe3b9
NTND
35/*
36 * Return git dir of the worktree. Note that the path may be relative.
37 * If wt is NULL, git dir of current worktree is returned.
38 */
55454427 39const char *get_worktree_git_dir(const struct worktree *wt);
69dfe3b9 40
68353144 41/*
a80c4c22
ES
42 * Search for the worktree identified unambiguously by `arg` -- typically
43 * supplied by the user via the command-line -- which may be a pathname or some
44 * shorthand uniquely identifying a worktree, thus making it convenient for the
45 * user to specify a worktree with minimal typing. For instance, if the last
46 * component (say, "foo") of a worktree's pathname is unique among worktrees
47 * (say, "work/foo" and "work/bar"), it can be used to identify the worktree
48 * unambiguously.
49 *
50 * `prefix` should be the `prefix` handed to top-level Git commands along with
51 * `argc` and `argv`.
52 *
53 * Return the worktree identified by `arg`, or NULL if not found.
68353144 54 */
55454427 55struct worktree *find_worktree(struct worktree **list,
ad6dad09
DL
56 const char *prefix,
57 const char *arg);
68353144 58
bb4995fc
ES
59/*
60 * Return the worktree corresponding to `path`, or NULL if no such worktree
61 * exists.
62 */
63struct worktree *find_worktree_by_path(struct worktree **, const char *path);
64
984ad9e5
NTND
65/*
66 * Return true if the given worktree is the main one.
67 */
55454427 68int is_main_worktree(const struct worktree *wt);
984ad9e5 69
346ef530
NTND
70/*
71 * Return the reason string if the given worktree is locked or NULL
72 * otherwise.
73 */
55454427 74const char *worktree_lock_reason(struct worktree *wt);
346ef530 75
a29a8b75
RS
76/*
77 * Return true if worktree entry should be pruned, along with the reason for
78 * pruning. Otherwise, return false and the worktree's path in `wtpath`, or
79 * NULL if it cannot be determined. Caller is responsible for freeing
80 * returned path.
81 *
82 * `expire` defines a grace period to prune the worktree when its path
83 * does not exist.
84 */
85int should_prune_worktree(const char *id,
86 struct strbuf *reason,
87 char **wtpath,
88 timestamp_t expire);
89
ee6763af
NTND
90#define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
91
4ddddc1f
NTND
92/*
93 * Return zero if the worktree is in good condition. Error message is
94 * returned if "errmsg" is not NULL.
95 */
55454427 96int validate_worktree(const struct worktree *wt,
ad6dad09
DL
97 struct strbuf *errmsg,
98 unsigned flags);
4ddddc1f 99
9c620fc7
NTND
100/*
101 * Update worktrees/xxx/gitdir with the new path.
102 */
55454427 103void update_worktree_location(struct worktree *wt,
ad6dad09 104 const char *path_);
9c620fc7 105
bdd1f3e4
ES
106typedef void (* worktree_repair_fn)(int iserr, const char *path,
107 const char *msg, void *cb_data);
108
109/*
110 * Visit each registered linked worktree and repair corruptions. For each
111 * repair made or error encountered while attempting a repair, the callback
112 * function, if non-NULL, is called with the path of the worktree and a
113 * description of the repair or error, along with the callback user-data.
114 */
115void repair_worktrees(worktree_repair_fn, void *cb_data);
116
b214ab5a
ES
117/*
118 * Repair administrative files corresponding to the worktree at the given path.
119 * The worktree's .git file pointing at the repository must be intact for the
120 * repair to succeed. Useful for re-associating an orphaned worktree with the
121 * repository if the worktree has been moved manually (without using "git
122 * worktree move"). For each repair made or error encountered while attempting
123 * a repair, the callback function, if non-NULL, is called with the path of the
124 * worktree and a description of the repair or error, along with the callback
125 * user-data.
126 */
127void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
128
51934904
MR
129/*
130 * Free up the memory for worktree(s)
131 */
55454427 132void free_worktrees(struct worktree **);
51934904 133
ac6c561b
MR
134/*
135 * Check if a per-worktree symref points to a ref in the main worktree
d3b9ac07
NTND
136 * or any linked worktree, and return the worktree that holds the ref,
137 * or NULL otherwise. The result may be destroyed by the next call.
ac6c561b 138 */
55454427 139const struct worktree *find_shared_symref(const char *symref,
ad6dad09 140 const char *target);
ac6c561b 141
d0c39a49
NTND
142/*
143 * Similar to head_ref() for all HEADs _except_ one from the current
144 * worktree, which is covered by head_ref().
145 */
146int other_head_refs(each_ref_fn fn, void *cb_data);
147
14ace5b7
NTND
148int is_worktree_being_rebased(const struct worktree *wt, const char *target);
149int is_worktree_being_bisected(const struct worktree *wt, const char *target);
150
2e641d58
NTND
151/*
152 * Similar to git_path() but can produce paths for a specified
153 * worktree instead of current one
154 */
b199d714 155const char *worktree_git_path(const struct worktree *wt,
ad6dad09 156 const char *fmt, ...)
2e641d58
NTND
157 __attribute__((format (printf, 2, 3)));
158
3a3b9d8c
NTND
159/*
160 * Parse a worktree ref (i.e. with prefix main-worktree/ or
161 * worktrees/) and return the position of the worktree's name and
162 * length (or NULL and zero if it's main worktree), and ref.
163 *
164 * All name, name_length and ref arguments could be NULL.
165 */
166int parse_worktree_ref(const char *worktree_ref, const char **name,
167 int *name_length, const char **ref);
ab3e1f78
NTND
168
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
ac6c561b 176#endif