]>
Commit | Line | Data |
---|---|---|
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 |
7 | struct strbuf; |
8 | ||
51934904 MR |
9 | struct 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 */ |
fc0c7d5e | 14 | char *prune_reason; /* private - use worktree_prune_reason */ |
0f05154c | 15 | struct object_id head_oid; |
92718b74 MR |
16 | int is_detached; |
17 | int is_bare; | |
750e8a60 | 18 | int is_current; |
e21cc076 | 19 | int lock_reason_valid; /* private */ |
fc0c7d5e | 20 | int prune_reason_valid; /* private */ |
51934904 MR |
21 | }; |
22 | ||
51934904 MR |
23 | /* |
24 | * Get the worktrees. The primary worktree will always be the first returned, | |
d9c54c2b | 25 | * and linked worktrees will follow in no particular order. |
51934904 MR |
26 | * |
27 | * The caller is responsible for freeing the memory from the returned | |
d9c54c2b | 28 | * worktrees by calling free_worktrees(). |
51934904 | 29 | */ |
03f2465b | 30 | struct worktree **get_worktrees(void); |
51934904 | 31 | |
1a248cf2 SB |
32 | /* |
33 | * Returns 1 if linked worktrees exist, 0 otherwise. | |
34 | */ | |
55454427 | 35 | int submodule_uses_worktrees(const char *path); |
1a248cf2 | 36 | |
69dfe3b9 NTND |
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 | */ | |
55454427 | 41 | const char *get_worktree_git_dir(const struct worktree *wt); |
69dfe3b9 | 42 | |
68353144 | 43 | /* |
a80c4c22 ES |
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. | |
68353144 | 56 | */ |
55454427 | 57 | struct worktree *find_worktree(struct worktree **list, |
ad6dad09 DL |
58 | const char *prefix, |
59 | const char *arg); | |
68353144 | 60 | |
bb4995fc ES |
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 | ||
984ad9e5 NTND |
67 | /* |
68 | * Return true if the given worktree is the main one. | |
69 | */ | |
55454427 | 70 | int is_main_worktree(const struct worktree *wt); |
984ad9e5 | 71 | |
346ef530 NTND |
72 | /* |
73 | * Return the reason string if the given worktree is locked or NULL | |
74 | * otherwise. | |
75 | */ | |
55454427 | 76 | const char *worktree_lock_reason(struct worktree *wt); |
346ef530 | 77 | |
fc0c7d5e RS |
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 | ||
a29a8b75 RS |
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 | ||
ee6763af NTND |
99 | #define WT_VALIDATE_WORKTREE_MISSING_OK (1 << 0) |
100 | ||
4ddddc1f NTND |
101 | /* |
102 | * Return zero if the worktree is in good condition. Error message is | |
103 | * returned if "errmsg" is not NULL. | |
104 | */ | |
55454427 | 105 | int validate_worktree(const struct worktree *wt, |
ad6dad09 DL |
106 | struct strbuf *errmsg, |
107 | unsigned flags); | |
4ddddc1f | 108 | |
9c620fc7 NTND |
109 | /* |
110 | * Update worktrees/xxx/gitdir with the new path. | |
111 | */ | |
55454427 | 112 | void update_worktree_location(struct worktree *wt, |
ad6dad09 | 113 | const char *path_); |
9c620fc7 | 114 | |
bdd1f3e4 ES |
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 | ||
b214ab5a ES |
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 | ||
51934904 MR |
138 | /* |
139 | * Free up the memory for worktree(s) | |
140 | */ | |
55454427 | 141 | void free_worktrees(struct worktree **); |
51934904 | 142 | |
ac6c561b MR |
143 | /* |
144 | * Check if a per-worktree symref points to a ref in the main worktree | |
d3b9ac07 NTND |
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. | |
ac6c561b | 147 | */ |
55454427 | 148 | const struct worktree *find_shared_symref(const char *symref, |
ad6dad09 | 149 | const char *target); |
ac6c561b | 150 | |
d0c39a49 NTND |
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 | ||
14ace5b7 NTND |
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 | ||
2e641d58 NTND |
160 | /* |
161 | * Similar to git_path() but can produce paths for a specified | |
162 | * worktree instead of current one | |
163 | */ | |
b199d714 | 164 | const char *worktree_git_path(const struct worktree *wt, |
ad6dad09 | 165 | const char *fmt, ...) |
2e641d58 NTND |
166 | __attribute__((format (printf, 2, 3))); |
167 | ||
3a3b9d8c NTND |
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); | |
ab3e1f78 NTND |
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 | ||
ac6c561b | 185 | #endif |