Git 2.32
[git] / worktree.h
1 #ifndef WORKTREE_H
2 #define WORKTREE_H
3
4 #include "cache.h"
5 #include "refs.h"
6
7 struct strbuf;
8
9 struct worktree {
10         char *path;
11         char *id;
12         char *head_ref;         /* NULL if HEAD is broken or detached */
13         char *lock_reason;      /* private - use worktree_lock_reason */
14         char *prune_reason;     /* private - use worktree_prune_reason */
15         struct object_id head_oid;
16         int is_detached;
17         int is_bare;
18         int is_current;
19         int lock_reason_valid; /* private */
20         int prune_reason_valid; /* private */
21 };
22
23 /*
24  * Get the worktrees.  The primary worktree will always be the first returned,
25  * and linked worktrees will follow in no particular order.
26  *
27  * The caller is responsible for freeing the memory from the returned
28  * worktrees by calling free_worktrees().
29  */
30 struct worktree **get_worktrees(void);
31
32 /*
33  * Returns 1 if linked worktrees exist, 0 otherwise.
34  */
35 int submodule_uses_worktrees(const char *path);
36
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  */
41 const char *get_worktree_git_dir(const struct worktree *wt);
42
43 /*
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.
56  */
57 struct worktree *find_worktree(struct worktree **list,
58                                const char *prefix,
59                                const char *arg);
60
61 /*
62  * Return the worktree corresponding to `path`, or NULL if no such worktree
63  * exists.
64  */
65 struct worktree *find_worktree_by_path(struct worktree **, const char *path);
66
67 /*
68  * Return true if the given worktree is the main one.
69  */
70 int is_main_worktree(const struct worktree *wt);
71
72 /*
73  * Return the reason string if the given worktree is locked or NULL
74  * otherwise.
75  */
76 const char *worktree_lock_reason(struct worktree *wt);
77
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  */
83 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
84
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  */
94 int should_prune_worktree(const char *id,
95                           struct strbuf *reason,
96                           char **wtpath,
97                           timestamp_t expire);
98
99 #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
100
101 /*
102  * Return zero if the worktree is in good condition. Error message is
103  * returned if "errmsg" is not NULL.
104  */
105 int validate_worktree(const struct worktree *wt,
106                       struct strbuf *errmsg,
107                       unsigned flags);
108
109 /*
110  * Update worktrees/xxx/gitdir with the new path.
111  */
112 void update_worktree_location(struct worktree *wt,
113                               const char *path_);
114
115 typedef 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  */
124 void repair_worktrees(worktree_repair_fn, void *cb_data);
125
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  */
136 void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
137
138 /*
139  * Free up the memory for worktree(s)
140  */
141 void free_worktrees(struct worktree **);
142
143 /*
144  * Check if a per-worktree symref points to a ref in the main worktree
145  * or any linked worktree, and return the worktree that holds the ref,
146  * or NULL otherwise. The result may be destroyed by the next call.
147  */
148 const struct worktree *find_shared_symref(const char *symref,
149                                           const char *target);
150
151 /*
152  * Similar to head_ref() for all HEADs _except_ one from the current
153  * worktree, which is covered by head_ref().
154  */
155 int other_head_refs(each_ref_fn fn, void *cb_data);
156
157 int is_worktree_being_rebased(const struct worktree *wt, const char *target);
158 int is_worktree_being_bisected(const struct worktree *wt, const char *target);
159
160 /*
161  * Similar to git_path() but can produce paths for a specified
162  * worktree instead of current one
163  */
164 const char *worktree_git_path(const struct worktree *wt,
165                               const char *fmt, ...)
166         __attribute__((format (printf, 2, 3)));
167
168 /*
169  * Parse a worktree ref (i.e. with prefix main-worktree/ or
170  * worktrees/) and return the position of the worktree's name and
171  * length (or NULL and zero if it's main worktree), and ref.
172  *
173  * All name, name_length and ref arguments could be NULL.
174  */
175 int parse_worktree_ref(const char *worktree_ref, const char **name,
176                        int *name_length, const char **ref);
177
178 /*
179  * Return a refname suitable for access from the current ref store.
180  */
181 void strbuf_worktree_ref(const struct worktree *wt,
182                          struct strbuf *sb,
183                          const char *refname);
184
185 #endif