pretty: clear signature check
[git/gitster.git] / worktree.h
blob11279d0c8fe249bb30642563bf221a8de7f3b0a3
1 #ifndef WORKTREE_H
2 #define WORKTREE_H
4 #include "refs.h"
6 struct strbuf;
8 struct worktree {
9 /* The repository this worktree belongs to. */
10 struct repository *repo;
11 char *path;
12 char *id;
13 char *head_ref; /* NULL if HEAD is broken or detached */
14 char *lock_reason; /* private - use worktree_lock_reason */
15 char *prune_reason; /* private - use worktree_prune_reason */
16 struct object_id head_oid;
17 int is_detached;
18 int is_bare;
19 int is_current;
20 int lock_reason_valid; /* private */
21 int prune_reason_valid; /* private */
25 * Get the worktrees. The primary worktree will always be the first returned,
26 * and linked worktrees will follow in no particular order.
28 * The caller is responsible for freeing the memory from the returned
29 * worktrees by calling free_worktrees().
31 struct worktree **get_worktrees(void);
34 * Returns 1 if linked worktrees exist, 0 otherwise.
36 int submodule_uses_worktrees(const char *path);
39 * Return git dir of the worktree. Note that the path may be relative.
40 * If wt is NULL, git dir of current worktree is returned.
42 const char *get_worktree_git_dir(const struct worktree *wt);
45 * Search for the worktree identified unambiguously by `arg` -- typically
46 * supplied by the user via the command-line -- which may be a pathname or some
47 * shorthand uniquely identifying a worktree, thus making it convenient for the
48 * user to specify a worktree with minimal typing. For instance, if the last
49 * component (say, "foo") of a worktree's pathname is unique among worktrees
50 * (say, "work/foo" and "work/bar"), it can be used to identify the worktree
51 * unambiguously.
53 * `prefix` should be the `prefix` handed to top-level Git commands along with
54 * `argc` and `argv`.
56 * Return the worktree identified by `arg`, or NULL if not found.
58 struct worktree *find_worktree(struct worktree **list,
59 const char *prefix,
60 const char *arg);
63 * Look up the worktree corresponding to `id`, or NULL of no such worktree
64 * exists.
66 struct worktree *get_linked_worktree(const char *id,
67 int skip_reading_head);
70 * Return the worktree corresponding to `path`, or NULL if no such worktree
71 * exists.
73 struct worktree *find_worktree_by_path(struct worktree **, const char *path);
76 * Return true if the given worktree is the main one.
78 int is_main_worktree(const struct worktree *wt);
81 * Return the reason string if the given worktree is locked or NULL
82 * otherwise.
84 const char *worktree_lock_reason(struct worktree *wt);
87 * Return the reason string if the given worktree should be pruned, otherwise
88 * NULL if it should not be pruned. `expire` defines a grace period to prune
89 * the worktree when its path does not exist.
91 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
94 * Return true if worktree entry should be pruned, along with the reason for
95 * pruning. Otherwise, return false and the worktree's path in `wtpath`, or
96 * NULL if it cannot be determined. Caller is responsible for freeing
97 * returned path.
99 * `expire` defines a grace period to prune the worktree when its path
100 * does not exist.
102 int should_prune_worktree(const char *id,
103 struct strbuf *reason,
104 char **wtpath,
105 timestamp_t expire);
107 #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
110 * Return zero if the worktree is in good condition. Error message is
111 * returned if "errmsg" is not NULL.
113 int validate_worktree(const struct worktree *wt,
114 struct strbuf *errmsg,
115 unsigned flags);
118 * Update worktrees/xxx/gitdir with the new path.
120 void update_worktree_location(struct worktree *wt,
121 const char *path_);
123 typedef void (* worktree_repair_fn)(int iserr, const char *path,
124 const char *msg, void *cb_data);
127 * Visit each registered linked worktree and repair corruptions. For each
128 * repair made or error encountered while attempting a repair, the callback
129 * function, if non-NULL, is called with the path of the worktree and a
130 * description of the repair or error, along with the callback user-data.
132 void repair_worktrees(worktree_repair_fn, void *cb_data);
135 * Repair administrative files corresponding to the worktree at the given path.
136 * The worktree's .git file pointing at the repository must be intact for the
137 * repair to succeed. Useful for re-associating an orphaned worktree with the
138 * repository if the worktree has been moved manually (without using "git
139 * worktree move"). For each repair made or error encountered while attempting
140 * a repair, the callback function, if non-NULL, is called with the path of the
141 * worktree and a description of the repair or error, along with the callback
142 * user-data.
144 void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
147 * Free up the memory for a worktree.
149 void free_worktree(struct worktree *);
152 * Free up the memory for worktree(s)
154 void free_worktrees(struct worktree **);
157 * Check if a per-worktree symref points to a ref in the main worktree
158 * or any linked worktree, and return the worktree that holds the ref,
159 * or NULL otherwise.
161 const struct worktree *find_shared_symref(struct worktree **worktrees,
162 const char *symref,
163 const char *target);
166 * Returns true if a symref points to a ref in a worktree.
168 int is_shared_symref(const struct worktree *wt,
169 const char *symref, const char *target);
172 * Similar to head_ref() for all HEADs _except_ one from the current
173 * worktree, which is covered by head_ref().
175 int other_head_refs(each_ref_fn fn, void *cb_data);
177 int is_worktree_being_rebased(const struct worktree *wt, const char *target);
178 int is_worktree_being_bisected(const struct worktree *wt, const char *target);
181 * Return a refname suitable for access from the current ref store.
183 void strbuf_worktree_ref(const struct worktree *wt,
184 struct strbuf *sb,
185 const char *refname);
188 * Enable worktree config for the first time. This will make the following
189 * adjustments:
191 * 1. Add extensions.worktreeConfig=true in the common config file.
193 * 2. If the common config file has a core.worktree value, then that value
194 * is moved to the main worktree's config.worktree file.
196 * 3. If the common config file has a core.bare enabled, then that value
197 * is moved to the main worktree's config.worktree file.
199 * If extensions.worktreeConfig is already true, then this method
200 * terminates early without any of the above steps. The existing config
201 * arrangement is assumed to be intentional.
203 * Returns 0 on success. Reports an error message and returns non-zero
204 * if any of these steps fail.
206 int init_worktree_config(struct repository *r);
208 #endif