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_MODE_CHANGE 'm'
30 #define GOT_STATUS_CONFLICT 'C'
31 #define GOT_STATUS_MERGE 'G'
32 #define GOT_STATUS_MISSING '!'
33 #define GOT_STATUS_UNVERSIONED '?'
34 #define GOT_STATUS_OBSTRUCTED '~'
35 #define GOT_STATUS_NONEXISTENT 'N'
36 #define GOT_STATUS_REVERT 'R'
37 #define GOT_STATUS_CANNOT_DELETE 'd'
38 #define GOT_STATUS_BUMP_BASE 'b'
39 #define GOT_STATUS_BASE_REF_ERR 'B'
40 #define GOT_STATUS_CANNOT_UPDATE '#'
42 /*
43 * Attempt to initialize a new work tree on disk.
44 * The first argument is the path to a directory where the work tree
45 * will be created. The path itself must not yet exist, but the dirname(3)
46 * of the path must already exist.
47 * The reference provided will be used to determine the new worktree's
48 * base commit. The third argument speficies the work tree's path prefix.
49 */
50 const struct got_error *got_worktree_init(const char *, struct got_reference *,
51 const char *, struct got_repository *);
53 /*
54 * Attempt to open a worktree at or above the specified path.
55 * The caller must dispose of it with got_worktree_close().
56 */
57 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
59 /* Dispose of an open work tree. */
60 const struct got_error *got_worktree_close(struct got_worktree *);
62 /*
63 * Get the path to the root directory of a worktree.
64 */
65 const char *got_worktree_get_root_path(struct got_worktree *);
67 /*
68 * Get the path to the repository associated with a worktree.
69 */
70 const char *got_worktree_get_repo_path(struct got_worktree *);
72 /*
73 * Get the path prefix associated with a worktree.
74 */
75 const char *got_worktree_get_path_prefix(struct got_worktree *);
77 /*
78 * Get the UUID of a work tree as a string.
79 * The caller must dispose of the returned UUID string with free(3).
80 */
81 const struct got_error *got_worktree_get_uuid(char **, struct got_worktree *);
83 /*
84 * Check if a user-provided path prefix matches that of the worktree.
85 */
86 const struct got_error *got_worktree_match_path_prefix(int *,
87 struct got_worktree *, const char *);
89 /*
90 * Get the name of a work tree's HEAD reference.
91 */
92 const char *got_worktree_get_head_ref_name(struct got_worktree *);
94 /*
95 * Set the branch head reference of the work tree.
96 */
97 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
98 struct got_reference *);
100 /*
101 * Get the current base commit ID of a worktree.
102 */
103 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
105 /*
106 * Set the base commit Id of a worktree.
107 */
108 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
109 struct got_repository *, struct got_object_id *);
111 /* A callback function which is invoked when a path is checked out. */
112 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
113 unsigned char, const char *);
115 /* A callback function which is invoked when a path is removed. */
116 typedef const struct got_error *(*got_worktree_delete_cb)(void *,
117 unsigned char, unsigned char, const char *);
119 /*
120 * Attempt to check out files into a work tree from its associated repository
121 * and path prefix, and update the work tree's file index accordingly.
122 * File content is obtained from blobs within the work tree's path prefix
123 * inside the tree corresponding to the work tree's base commit.
124 * The checkout progress callback will be invoked with the provided
125 * void * argument, and the path of each checked out file.
127 * It is possible to restrict the checkout operation to specific paths in
128 * the work tree, in which case all files outside those paths will remain at
129 * their currently recorded base commit. Inconsistent base commits can be
130 * repaired later by running another update operation across the entire work
131 * tree. Inconsistent base-commits may also occur if this function runs into
132 * an error or if the checkout operation is cancelled by the cancel callback.
133 * Allspecified paths are relative to the work tree's root. Pass a pathlist
134 * with a single empty path "" to check out files across the entire work tree.
136 * Some operations may refuse to run while the work tree contains files from
137 * multiple base commits.
138 */
139 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
140 struct got_pathlist_head *, struct got_repository *,
141 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
143 /* Merge the differences between two commits into a work tree. */
144 const struct got_error *
145 got_worktree_merge_files(struct got_worktree *,
146 struct got_object_id *, struct got_object_id *,
147 struct got_repository *, got_worktree_checkout_cb, void *,
148 got_cancel_cb, void *);
150 /*
151 * A callback function which is invoked to report a file's status.
153 * If a valid directory file descriptor and a directory entry name are passed,
154 * these should be used to open the file instead of opening the file by path.
155 * This prevents race conditions if the filesystem is modified concurrently.
156 * If the directory descriptor is not available then its value will be -1.
157 */
158 typedef const struct got_error *(*got_worktree_status_cb)(void *,
159 unsigned char, unsigned char, const char *, struct got_object_id *,
160 struct got_object_id *, struct got_object_id *, int, const char *);
162 /*
163 * Report the status of paths in the work tree.
164 * The status callback will be invoked with the provided void * argument,
165 * a path, and a corresponding status code.
166 */
167 const struct got_error *got_worktree_status(struct got_worktree *,
168 struct got_pathlist_head *, struct got_repository *,
169 got_worktree_status_cb, void *, got_cancel_cb cancel_cb, void *);
171 /*
172 * Try to resolve a user-provided path to an on-disk path in the work tree.
173 * The caller must dispose of the resolved path with free(3).
174 */
175 const struct got_error *got_worktree_resolve_path(char **,
176 struct got_worktree *, const char *);
178 /* Schedule files at on-disk paths for addition in the next commit. */
179 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
180 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
181 struct got_repository *, int);
183 /*
184 * Remove files from disk and schedule them to be deleted in the next commit.
185 * Don't allow deleting files with uncommitted modifications, unless the
186 * parameter 'delete_local_mods' is set.
187 */
188 const struct got_error *
189 got_worktree_schedule_delete(struct got_worktree *,
190 struct got_pathlist_head *, int, const char *,
191 got_worktree_delete_cb, void *, struct got_repository *, int);
193 /* A callback function which is used to select or reject a patch. */
194 typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
195 unsigned char, const char *, FILE *, int, int);
197 /* Values for result output parameter of got_wortree_patch_cb. */
198 #define GOT_PATCH_CHOICE_NONE 0
199 #define GOT_PATCH_CHOICE_YES 1
200 #define GOT_PATCH_CHOICE_NO 2
201 #define GOT_PATCH_CHOICE_QUIT 3
203 /*
204 * Revert a file at the specified path such that it matches its
205 * original state in the worktree's base commit.
206 * If the patch callback is not NULL, call it to select patch hunks to
207 * revert. Otherwise, revert the whole file found at each path.
208 */
209 const struct got_error *got_worktree_revert(struct got_worktree *,
210 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
211 got_worktree_patch_cb patch_cb, void *patch_arg,
212 struct got_repository *);
214 /*
215 * A callback function which is invoked when a commit message is requested.
216 * Passes a pathlist with a struct got_commitable * in the data pointer of
217 * each element, a pointer to the log message that must be set by the
218 * callback and will be freed after committing, and an argument passed
219 * through to the callback.
220 */
221 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
222 struct got_pathlist_head *, char **, void *);
224 /*
225 * Create a new commit from changes in the work tree.
226 * Return the ID of the newly created commit.
227 * The worktree's base commit will be set to this new commit.
228 * Files unaffected by this commit operation will retain their
229 * current base commit.
230 * An author and a non-empty log message must be specified.
231 * The name of the committer is optional (may be NULL).
232 * If a path to be committed contains a symlink which points outside
233 * of the path space under version control, raise an error unless
234 * committing of such paths is being forced by the caller.
235 */
236 const struct got_error *got_worktree_commit(struct got_object_id **,
237 struct got_worktree *, struct got_pathlist_head *, const char *,
238 const char *, int, got_worktree_commit_msg_cb, void *,
239 got_worktree_status_cb, void *, struct got_repository *);
241 /* Get the path of a commitable worktree item. */
242 const char *got_commitable_get_path(struct got_commitable *);
244 /* Get the status of a commitable worktree item. */
245 unsigned int got_commitable_get_status(struct got_commitable *);
247 /*
248 * Prepare for rebasing a branch onto the work tree's current branch.
249 * This function creates references to a temporary branch, the branch
250 * being rebased, and the work tree's current branch, under the
251 * "got/worktree/rebase/" namespace. These references are used to
252 * keep track of rebase operation state and are used as input and/or
253 * output arguments with other rebase-related functions.
254 * The function also returns a pointer to a fileindex which must be
255 * passed back to other rebase-related functions.
256 */
257 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
258 struct got_reference **, struct got_fileindex **, struct got_worktree *,
259 struct got_reference *, struct got_repository *);
261 /*
262 * Continue an interrupted rebase operation.
263 * This function returns existing references created when rebase was prepared,
264 * and the ID of the commit currently being rebased. This should be called
265 * before either resuming or aborting a rebase operation.
266 * The function also returns a pointer to a fileindex which must be
267 * passed back to other rebase-related functions.
268 */
269 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
270 struct got_reference **, struct got_reference **, struct got_reference **,
271 struct got_fileindex **, struct got_worktree *, struct got_repository *);
273 /* Check whether a, potentially interrupted, rebase operation is in progress. */
274 const struct got_error *got_worktree_rebase_in_progress(int *,
275 struct got_worktree *);
277 /*
278 * Merge changes from the commit currently being rebased into the work tree.
279 * Report affected files, including merge conflicts, via the specified
280 * progress callback. Also populate a list of affected paths which should
281 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
282 * This list must be initialized with TAILQ_INIT() and disposed of with
283 * got_worktree_rebase_pathlist_free().
284 */
285 const struct got_error *got_worktree_rebase_merge_files(
286 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
287 struct got_object_id *, struct got_object_id *, struct got_repository *,
288 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
290 /*
291 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
292 * branch and return the ID of the newly created commit. An optional list of
293 * merged paths can be provided; otherwise this function will perform a status
294 * crawl across the entire work tree to find paths to commit.
295 */
296 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
297 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
298 struct got_reference *, struct got_commit_object *,
299 struct got_object_id *, struct got_repository *);
301 /* Free a list of merged paths from got_worktree_merge_files. */
302 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
304 /* Postpone the rebase operation. Should be called after a merge conflict. */
305 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
306 struct got_fileindex *);
308 /*
309 * Complete the current rebase operation. This should be called once all
310 * commits have been rebased successfully.
311 */
312 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
313 struct got_fileindex *, struct got_reference *, struct got_reference *,
314 struct got_reference *, struct got_repository *);
316 /*
317 * Abort the current rebase operation.
318 * Report reverted files via the specified progress callback.
319 */
320 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
321 struct got_fileindex *, struct got_repository *, struct got_reference *,
322 got_worktree_checkout_cb, void *);
324 /*
325 * Prepare for editing the history of the work tree's current branch.
326 * This function creates references to a temporary branch, and the
327 * work tree's current branch, under the "got/worktree/histedit/" namespace.
328 * These references are used to keep track of histedit operation state and
329 * are used as input and/or output arguments with other histedit-related
330 * functions.
331 */
332 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
333 struct got_reference **, struct got_object_id **, struct got_fileindex **,
334 struct got_worktree *, struct got_repository *);
336 /*
337 * Continue an interrupted histedit operation.
338 * This function returns existing references created when histedit was
339 * prepared and the ID of the commit currently being edited.
340 * It should be called before resuming or aborting a histedit operation.
341 */
342 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
343 struct got_reference **, struct got_reference **, struct got_object_id **,
344 struct got_fileindex **, struct got_worktree *, struct got_repository *);
346 /* Check whether a histedit operation is in progress. */
347 const struct got_error *got_worktree_histedit_in_progress(int *,
348 struct got_worktree *);
350 /*
351 * Merge changes from the commit currently being edited into the work tree.
352 * Report affected files, including merge conflicts, via the specified
353 * progress callback. Also populate a list of affected paths which should
354 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
355 * This list must be initialized with TAILQ_INIT() and disposed of with
356 * got_worktree_rebase_pathlist_free().
357 */
358 const struct got_error *got_worktree_histedit_merge_files(
359 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
360 struct got_object_id *, struct got_object_id *, struct got_repository *,
361 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
363 /*
364 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
365 * branch and return the ID of the newly created commit. An optional list of
366 * merged paths can be provided; otherwise this function will perform a status
367 * crawl across the entire work tree to find paths to commit.
368 * An optional log message can be provided which will be used instead of the
369 * commit's original message.
370 */
371 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
372 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
373 struct got_reference *, struct got_commit_object *,
374 struct got_object_id *, const char *, struct got_repository *);
376 /*
377 * Record the specified commit as skipped during histedit.
378 * This should be called for commits which get dropped or get folded into
379 * a subsequent commit.
380 */
381 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
382 struct got_object_id *, struct got_repository *);
384 /* Postpone the histedit operation. */
385 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
386 struct got_fileindex *);
388 /*
389 * Complete the current histedit operation. This should be called once all
390 * commits have been edited successfully.
391 */
392 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
393 struct got_fileindex *, struct got_reference *, struct got_reference *,
394 struct got_repository *);
396 /*
397 * Abort the current histedit operation.
398 * Report reverted files via the specified progress callback.
399 */
400 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
401 struct got_fileindex *, struct got_repository *, struct got_reference *,
402 struct got_object_id *, got_worktree_checkout_cb, void *);
404 /* Get the path to this work tree's histedit script file. */
405 const struct got_error *got_worktree_get_histedit_script_path(char **,
406 struct got_worktree *);
408 /*
409 * Prepare a work tree for integrating a branch.
410 * Return pointers to a fileindex and locked references which must be
411 * passed back to other integrate-related functions.
412 */
413 const struct got_error *
414 got_worktree_integrate_prepare(struct got_fileindex **,
415 struct got_reference **, struct got_reference **,
416 struct got_worktree *, const char *, struct got_repository *);
418 /*
419 * Carry out a prepared branch integration operation.
420 * Report affected files via the specified progress callback.
421 */
422 const struct got_error *got_worktree_integrate_continue(
423 struct got_worktree *, struct got_fileindex *, struct got_repository *,
424 struct got_reference *, struct got_reference *,
425 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
427 /* Abort a prepared branch integration operation. */
428 const struct got_error *got_worktree_integrate_abort(struct got_worktree *,
429 struct got_fileindex *, struct got_repository *,
430 struct got_reference *, struct got_reference *);
432 /*
433 * Stage the specified paths for commit.
434 * If the patch callback is not NULL, call it to select patch hunks for
435 * staging. Otherwise, stage the full file content found at each path.
436 * If a path being staged contains a symlink which points outside
437 * of the path space under version control, raise an error unless
438 * staging of such paths is being forced by the caller.
439 */
440 const struct got_error *got_worktree_stage(struct got_worktree *,
441 struct got_pathlist_head *, got_worktree_status_cb, void *,
442 got_worktree_patch_cb, void *, int, struct got_repository *);
444 /*
445 * Merge staged changes for the specified paths back into the work tree
446 * and mark the paths as non-staged again.
447 */
448 const struct got_error *got_worktree_unstage(struct got_worktree *,
449 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
450 got_worktree_patch_cb, void *, struct got_repository *);
452 /* A callback function which is invoked with per-path info. */
453 typedef const struct got_error *(*got_worktree_path_info_cb)(void *,
454 const char *path, mode_t mode, time_t mtime,
455 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
456 struct got_object_id *commit_id);
458 /*
459 * Report work-tree meta data for paths in the work tree.
460 * The info callback will be invoked with the provided void * argument,
461 * a path, and meta-data arguments (see got_worktree_path_info_cb).
462 */
463 const struct got_error *
464 got_worktree_path_info(struct got_worktree *, struct got_pathlist_head *,
465 got_worktree_path_info_cb, void *, got_cancel_cb , void *);