worktree: teach "repair" to fix worktree back-links to main worktree
[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         struct object_id head_oid;
15         int is_detached;
16         int is_bare;
17         int is_current;
18         int lock_reason_valid; /* private */
19 };
20
21 /*
22  * Get the worktrees.  The primary worktree will always be the first returned,
23  * and linked worktrees will follow in no particular order.
24  *
25  * The caller is responsible for freeing the memory from the returned
26  * worktrees by calling free_worktrees().
27  */
28 struct worktree **get_worktrees(void);
29
30 /*
31  * Returns 1 if linked worktrees exist, 0 otherwise.
32  */
33 int submodule_uses_worktrees(const char *path);
34
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  */
39 const char *get_worktree_git_dir(const struct worktree *wt);
40
41 /*
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.
54  */
55 struct worktree *find_worktree(struct worktree **list,
56                                const char *prefix,
57                                const char *arg);
58
59 /*
60  * Return the worktree corresponding to `path`, or NULL if no such worktree
61  * exists.
62  */
63 struct worktree *find_worktree_by_path(struct worktree **, const char *path);
64
65 /*
66  * Return true if the given worktree is the main one.
67  */
68 int is_main_worktree(const struct worktree *wt);
69
70 /*
71  * Return the reason string if the given worktree is locked or NULL
72  * otherwise.
73  */
74 const char *worktree_lock_reason(struct worktree *wt);
75
76 #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
77
78 /*
79  * Return zero if the worktree is in good condition. Error message is
80  * returned if "errmsg" is not NULL.
81  */
82 int validate_worktree(const struct worktree *wt,
83                       struct strbuf *errmsg,
84                       unsigned flags);
85
86 /*
87  * Update worktrees/xxx/gitdir with the new path.
88  */
89 void update_worktree_location(struct worktree *wt,
90                               const char *path_);
91
92 typedef void (* worktree_repair_fn)(int iserr, const char *path,
93                                     const char *msg, void *cb_data);
94
95 /*
96  * Visit each registered linked worktree and repair corruptions. For each
97  * repair made or error encountered while attempting a repair, the callback
98  * function, if non-NULL, is called with the path of the worktree and a
99  * description of the repair or error, along with the callback user-data.
100  */
101 void repair_worktrees(worktree_repair_fn, void *cb_data);
102
103 /*
104  * Free up the memory for worktree(s)
105  */
106 void free_worktrees(struct worktree **);
107
108 /*
109  * Check if a per-worktree symref points to a ref in the main worktree
110  * or any linked worktree, and return the worktree that holds the ref,
111  * or NULL otherwise. The result may be destroyed by the next call.
112  */
113 const struct worktree *find_shared_symref(const char *symref,
114                                           const char *target);
115
116 /*
117  * Similar to head_ref() for all HEADs _except_ one from the current
118  * worktree, which is covered by head_ref().
119  */
120 int other_head_refs(each_ref_fn fn, void *cb_data);
121
122 int is_worktree_being_rebased(const struct worktree *wt, const char *target);
123 int is_worktree_being_bisected(const struct worktree *wt, const char *target);
124
125 /*
126  * Similar to git_path() but can produce paths for a specified
127  * worktree instead of current one
128  */
129 const char *worktree_git_path(const struct worktree *wt,
130                               const char *fmt, ...)
131         __attribute__((format (printf, 2, 3)));
132
133 /*
134  * Parse a worktree ref (i.e. with prefix main-worktree/ or
135  * worktrees/) and return the position of the worktree's name and
136  * length (or NULL and zero if it's main worktree), and ref.
137  *
138  * All name, name_length and ref arguments could be NULL.
139  */
140 int parse_worktree_ref(const char *worktree_ref, const char **name,
141                        int *name_length, const char **ref);
142
143 /*
144  * Return a refname suitable for access from the current ref store.
145  */
146 void strbuf_worktree_ref(const struct worktree *wt,
147                          struct strbuf *sb,
148                          const char *refname);
149
150 /*
151  * Return a refname suitable for access from the current ref
152  * store. The result will be destroyed at the next call.
153  */
154 const char *worktree_ref(const struct worktree *wt,
155                          const char *refname);
156
157 #endif