Merge branch 'jk/httpd-test-updates'
[alt-git.git] / worktree.h
blob9dcea6fc8c1b4fcc387818967d2de99713bfb50b
1 #ifndef WORKTREE_H
2 #define WORKTREE_H
4 #include "cache.h"
5 #include "refs.h"
7 struct strbuf;
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 */
24 * Get the worktrees. The primary worktree will always be the first returned,
25 * and linked worktrees will follow in no particular order.
27 * The caller is responsible for freeing the memory from the returned
28 * worktrees by calling free_worktrees().
30 struct worktree **get_worktrees(void);
33 * Returns 1 if linked worktrees exist, 0 otherwise.
35 int submodule_uses_worktrees(const char *path);
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.
41 const char *get_worktree_git_dir(const struct worktree *wt);
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.
52 * `prefix` should be the `prefix` handed to top-level Git commands along with
53 * `argc` and `argv`.
55 * Return the worktree identified by `arg`, or NULL if not found.
57 struct worktree *find_worktree(struct worktree **list,
58 const char *prefix,
59 const char *arg);
62 * Return the worktree corresponding to `path`, or NULL if no such worktree
63 * exists.
65 struct worktree *find_worktree_by_path(struct worktree **, const char *path);
68 * Return true if the given worktree is the main one.
70 int is_main_worktree(const struct worktree *wt);
73 * Return the reason string if the given worktree is locked or NULL
74 * otherwise.
76 const char *worktree_lock_reason(struct worktree *wt);
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.
83 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire);
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.
91 * `expire` defines a grace period to prune the worktree when its path
92 * does not exist.
94 int should_prune_worktree(const char *id,
95 struct strbuf *reason,
96 char **wtpath,
97 timestamp_t expire);
99 #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0)
102 * Return zero if the worktree is in good condition. Error message is
103 * returned if "errmsg" is not NULL.
105 int validate_worktree(const struct worktree *wt,
106 struct strbuf *errmsg,
107 unsigned flags);
110 * Update worktrees/xxx/gitdir with the new path.
112 void update_worktree_location(struct worktree *wt,
113 const char *path_);
115 typedef void (* worktree_repair_fn)(int iserr, const char *path,
116 const char *msg, void *cb_data);
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.
124 void repair_worktrees(worktree_repair_fn, void *cb_data);
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.
136 void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
139 * Free up the memory for worktree(s)
141 void free_worktrees(struct worktree **);
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.
148 const struct worktree *find_shared_symref(struct worktree **worktrees,
149 const char *symref,
150 const char *target);
153 * Similar to head_ref() for all HEADs _except_ one from the current
154 * worktree, which is covered by head_ref().
156 int other_head_refs(each_ref_fn fn, void *cb_data);
158 int is_worktree_being_rebased(const struct worktree *wt, const char *target);
159 int is_worktree_being_bisected(const struct worktree *wt, const char *target);
162 * Similar to git_path() but can produce paths for a specified
163 * worktree instead of current one
165 const char *worktree_git_path(const struct worktree *wt,
166 const char *fmt, ...)
167 __attribute__((format (printf, 2, 3)));
170 * Return a refname suitable for access from the current ref store.
172 void strbuf_worktree_ref(const struct worktree *wt,
173 struct strbuf *sb,
174 const char *refname);
177 * Enable worktree config for the first time. This will make the following
178 * adjustments:
180 * 1. Add extensions.worktreeConfig=true in the common config file.
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.
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.
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.
192 * Returns 0 on success. Reports an error message and returns non-zero
193 * if any of these steps fail.
195 int init_worktree_config(struct repository *r);
197 #endif