Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 struct got_worktree;
18 struct got_commitable;
19 struct got_commit_object;
20 struct got_fileindex;
22 /* status codes */
23 #define GOT_STATUS_NO_CHANGE ' '
24 #define GOT_STATUS_ADD 'A'
25 #define GOT_STATUS_EXISTS 'E'
26 #define GOT_STATUS_UPDATE 'U'
27 #define GOT_STATUS_DELETE 'D'
28 #define GOT_STATUS_MODIFY 'M'
29 #define GOT_STATUS_CONFLICT 'C'
30 #define GOT_STATUS_MERGE 'G'
31 #define GOT_STATUS_MISSING '!'
32 #define GOT_STATUS_UNVERSIONED '?'
33 #define GOT_STATUS_OBSTRUCTED '~'
34 #define GOT_STATUS_REVERT 'R'
35 #define GOT_STATUS_CANNOT_DELETE 'd'
36 #define GOT_STATUS_BUMP_BASE 'b'
38 /*
39 * Attempt to initialize a new work tree on disk.
40 * The first argument is the path to a directory where the work tree
41 * will be created. The path itself must not yet exist, but the dirname(3)
42 * of the path must already exist.
43 * The reference provided will be used to determine the new worktree's
44 * base commit. The third argument speficies the work tree's path prefix.
45 */
46 const struct got_error *got_worktree_init(const char *, struct got_reference *,
47 const char *, struct got_repository *);
49 /*
50 * Attempt to open a worktree at or above the specified path.
51 * The caller must dispose of it with got_worktree_close().
52 */
53 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
55 /* Dispose of an open work tree. */
56 const struct got_error *got_worktree_close(struct got_worktree *);
58 /*
59 * Get the path to the root directory of a worktree.
60 */
61 const char *got_worktree_get_root_path(struct got_worktree *);
63 /*
64 * Get the path to the repository associated with a worktree.
65 */
66 const char *got_worktree_get_repo_path(struct got_worktree *);
68 /*
69 * Get the path prefix associated with a worktree.
70 */
71 const char *got_worktree_get_path_prefix(struct got_worktree *);
73 /*
74 * Check if a user-provided path prefix matches that of the worktree.
75 */
76 const struct got_error *got_worktree_match_path_prefix(int *,
77 struct got_worktree *, const char *);
79 /*
80 * Get the name of a work tree's HEAD reference.
81 */
82 const char *got_worktree_get_head_ref_name(struct got_worktree *);
84 /*
85 * Set the branch head reference of the work tree.
86 */
87 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
88 struct got_reference *);
90 /*
91 * Get the current base commit ID of a worktree.
92 */
93 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
95 /*
96 * Set the base commit Id of a worktree.
97 */
98 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
99 struct got_repository *, struct got_object_id *);
101 /* A callback function which is invoked when a path is checked out. */
102 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
103 unsigned char, const char *);
105 /* A callback function which is invoked at cancellation points.
106 * May return GOT_ERR_CANCELLED to abort the runing operation. */
107 typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
109 /*
110 * Attempt to check out files into a work tree from its associated repository
111 * and path prefix, and update the work tree's file index accordingly.
112 * File content is obtained from blobs within the work tree's path prefix
113 * inside the tree corresponding to the work tree's base commit.
114 * The checkout progress callback will be invoked with the provided
115 * void * argument, and the path of each checked out file.
117 * It is possible to restrict the checkout operation to specific paths in
118 * the work tree, in which case all files outside those paths will remain at
119 * their currently recorded base commit. Inconsistent base commits can be
120 * repaired later by running another update operation across the entire work
121 * tree. Inconsistent base-commits may also occur if this function runs into
122 * an error or if the checkout operation is cancelled by the cancel callback.
123 * Allspecified paths are relative to the work tree's root. Pass a pathlist
124 * with a single empty path "" to check out files across the entire work tree.
126 * Some operations may refuse to run while the work tree contains files from
127 * multiple base commits.
128 */
129 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
130 struct got_pathlist_head *, struct got_repository *,
131 got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
133 /* Merge the differences between two commits into a work tree. */
134 const struct got_error *
135 got_worktree_merge_files(struct got_worktree *,
136 struct got_object_id *, struct got_object_id *,
137 struct got_repository *, got_worktree_checkout_cb, void *,
138 got_worktree_cancel_cb, void *);
140 /* A callback function which is invoked to report a path's status. */
141 typedef const struct got_error *(*got_worktree_status_cb)(void *,
142 unsigned char, unsigned char, const char *, struct got_object_id *,
143 struct got_object_id *, struct got_object_id *);
145 /*
146 * Report the status of paths in the work tree.
147 * The status callback will be invoked with the provided void * argument,
148 * a path, and a corresponding status code.
149 */
150 const struct got_error *got_worktree_status(struct got_worktree *,
151 struct got_pathlist_head *, struct got_repository *,
152 got_worktree_status_cb, void *, got_worktree_cancel_cb cancel_cb, void *);
154 /*
155 * Try to resolve a user-provided path to an on-disk path in the work tree.
156 * The caller must dispose of the resolved path with free(3).
157 */
158 const struct got_error *got_worktree_resolve_path(char **,
159 struct got_worktree *, const char *);
161 /* Schedule files at on-disk paths for addition in the next commit. */
162 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
163 struct got_pathlist_head *, got_worktree_status_cb, void *,
164 struct got_repository *);
166 /*
167 * Remove files from disk and schedule them to be deleted in the next commit.
168 * Don't allow deleting files with uncommitted modifications, unless the
169 * parameter 'delete_local_mods' is set.
170 */
171 const struct got_error *
172 got_worktree_schedule_delete(struct got_worktree *,
173 struct got_pathlist_head *, int, got_worktree_status_cb, void *,
174 struct got_repository *);
176 /*
177 * Revert a file at the specified path such that it matches its
178 * original state in the worktree's base commit.
179 */
180 const struct got_error *got_worktree_revert(struct got_worktree *,
181 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
182 struct got_repository *);
184 /*
185 * A callback function which is invoked when a commit message is requested.
186 * Passes a pathlist with a struct got_commitable * in the data pointer of
187 * each element, a pointer to the log message that must be set by the
188 * callback and will be freed after committing, and an argument passed
189 * through to the callback.
190 */
191 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
192 struct got_pathlist_head *, char **, void *);
194 /*
195 * Create a new commit from changes in the work tree.
196 * Return the ID of the newly created commit.
197 * The worktree's base commit will be set to this new commit.
198 * Files unaffected by this commit operation will retain their
199 * current base commit.
200 * An author and a non-empty log message must be specified.
201 * The name of the committer is optional (may be NULL).
202 */
203 const struct got_error *got_worktree_commit(struct got_object_id **,
204 struct got_worktree *, struct got_pathlist_head *, const char *,
205 const char *, got_worktree_commit_msg_cb, void *,
206 got_worktree_status_cb, void *, struct got_repository *);
208 /* Get the path of a commitable worktree item. */
209 const char *got_commitable_get_path(struct got_commitable *);
211 /* Get the status of a commitable worktree item. */
212 unsigned int got_commitable_get_status(struct got_commitable *);
214 /*
215 * Prepare for rebasing a branch onto the work tree's current branch.
216 * This function creates references to a temporary branch, the branch
217 * being rebased, and the work tree's current branch, under the
218 * "got/worktree/rebase/" namespace. These references are used to
219 * keep track of rebase operation state and are used as input and/or
220 * output arguments with other rebase-related functions.
221 * The function also returns a pointer to a fileindex which must be
222 * passed back to other rebase-related functions.
223 */
224 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
225 struct got_reference **, struct got_fileindex **, struct got_worktree *,
226 struct got_reference *, struct got_repository *);
228 /*
229 * Continue an interrupted rebase operation.
230 * This function returns existing references created when rebase was prepared,
231 * and the ID of the commit currently being rebased. This should be called
232 * before either resuming or aborting a rebase operation.
233 * The function also returns a pointer to a fileindex which must be
234 * passed back to other rebase-related functions.
235 */
236 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
237 struct got_reference **, struct got_reference **, struct got_reference **,
238 struct got_fileindex **, struct got_worktree *, struct got_repository *);
240 /* Check whether a, potentially interrupted, rebase operation is in progress. */
241 const struct got_error *got_worktree_rebase_in_progress(int *,
242 struct got_worktree *);
244 /*
245 * Merge changes from the commit currently being rebased into the work tree.
246 * Report affected files, including merge conflicts, via the specified
247 * progress callback. Also populate a list of affected paths which should
248 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
249 * This list must be initialized with TAILQ_INIT() and disposed of with
250 * got_worktree_rebase_pathlist_free().
251 */
252 const struct got_error *got_worktree_rebase_merge_files(
253 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
254 struct got_object_id *, struct got_object_id *, struct got_repository *,
255 got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
257 /*
258 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
259 * branch and return the ID of the newly created commit. An optional list of
260 * merged paths can be provided; otherwise this function will perform a status
261 * crawl across the entire work tree to find paths to commit.
262 */
263 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
264 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
265 struct got_reference *, struct got_commit_object *,
266 struct got_object_id *, struct got_repository *);
268 /* Free a list of merged paths from got_worktree_merge_files. */
269 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
271 /* Postpone the rebase operation. Should be called after a merge conflict. */
272 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
273 struct got_fileindex *);
275 /*
276 * Complete the current rebase operation. This should be called once all
277 * commits have been rebased successfully.
278 */
279 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
280 struct got_fileindex *, struct got_reference *, struct got_reference *,
281 struct got_reference *, struct got_repository *);
283 /*
284 * Abort the current rebase operation.
285 * Report reverted files via the specified progress callback.
286 */
287 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
288 struct got_fileindex *, struct got_repository *, struct got_reference *,
289 got_worktree_checkout_cb, void *);
291 /*
292 * Prepare for editing the history of the work tree's current branch.
293 * This function creates references to a temporary branch, and the
294 * work tree's current branch, under the "got/worktree/histedit/" namespace.
295 * These references are used to keep track of histedit operation state and
296 * are used as input and/or output arguments with other histedit-related
297 * functions.
298 */
299 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
300 struct got_reference **, struct got_object_id **, struct got_fileindex **,
301 struct got_worktree *, struct got_repository *);
303 /*
304 * Continue an interrupted histedit operation.
305 * This function returns existing references created when histedit was
306 * prepared and the ID of the commit currently being edited.
307 * It should be called before resuming or aborting a histedit operation.
308 */
309 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
310 struct got_reference **, struct got_reference **, struct got_object_id **,
311 struct got_fileindex **, struct got_worktree *, struct got_repository *);
313 /* Check whether a histedit operation is in progress. */
314 const struct got_error *got_worktree_histedit_in_progress(int *,
315 struct got_worktree *);
317 /*
318 * Merge changes from the commit currently being edited into the work tree.
319 * Report affected files, including merge conflicts, via the specified
320 * progress callback. Also populate a list of affected paths which should
321 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
322 * This list must be initialized with TAILQ_INIT() and disposed of with
323 * got_worktree_rebase_pathlist_free().
324 */
325 const struct got_error *got_worktree_histedit_merge_files(
326 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
327 struct got_object_id *, struct got_object_id *, struct got_repository *,
328 got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
330 /*
331 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
332 * branch and return the ID of the newly created commit. An optional list of
333 * merged paths can be provided; otherwise this function will perform a status
334 * crawl across the entire work tree to find paths to commit.
335 * An optional log message can be provided which will be used instead of the
336 * commit's original message.
337 */
338 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
339 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
340 struct got_reference *, struct got_commit_object *,
341 struct got_object_id *, const char *, struct got_repository *);
343 /*
344 * Record the specified commit as skipped during histedit.
345 * This should be called for commits which get dropped or get folded into
346 * a subsequent commit.
347 */
348 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
349 struct got_object_id *, struct got_repository *);
351 /* Postpone the histedit operation. */
352 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
353 struct got_fileindex *);
355 /*
356 * Complete the current histedit operation. This should be called once all
357 * commits have been edited successfully.
358 */
359 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
360 struct got_fileindex *, struct got_reference *, struct got_reference *,
361 struct got_repository *);
363 /*
364 * Abort the current histedit operation.
365 * Report reverted files via the specified progress callback.
366 */
367 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
368 struct got_fileindex *, struct got_repository *, struct got_reference *,
369 struct got_object_id *, got_worktree_checkout_cb, void *);
371 /* Get the path to this work tree's histedit script file. */
372 const struct got_error *got_worktree_get_histedit_script_path(char **,
373 struct got_worktree *);
375 /*
376 * Stage the specified paths for commit.
377 * If the 'data' pointer of a pathlist element on the path list is NULL then
378 * stage the content of the entire file at this path. Otherwise, the 'data'
379 * pointer is expected to point at a const char * path of a file which
380 * contains alternative content to be staged instead.
381 */
382 const struct got_error *got_worktree_stage(struct got_worktree *,
383 struct got_pathlist_head *, got_worktree_status_cb, void *,
384 struct got_repository *);