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 /*
112 * Obtain a parsed representation of this worktree's got.conf file.
113 * Return NULL if this configuration file could not be read.
114 */
115 const struct got_gotconfig *got_worktree_get_gotconfig(struct got_worktree *);
117 /* A callback function which is invoked when a path is checked out. */
118 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
119 unsigned char, const char *);
121 /* A callback function which is invoked when a path is removed. */
122 typedef const struct got_error *(*got_worktree_delete_cb)(void *,
123 unsigned char, unsigned char, const char *);
125 /*
126 * Attempt to check out files into a work tree from its associated repository
127 * and path prefix, and update the work tree's file index accordingly.
128 * File content is obtained from blobs within the work tree's path prefix
129 * inside the tree corresponding to the work tree's base commit.
130 * The checkout progress callback will be invoked with the provided
131 * void * argument, and the path of each checked out file.
133 * It is possible to restrict the checkout operation to specific paths in
134 * the work tree, in which case all files outside those paths will remain at
135 * their currently recorded base commit. Inconsistent base commits can be
136 * repaired later by running another update operation across the entire work
137 * tree. Inconsistent base-commits may also occur if this function runs into
138 * an error or if the checkout operation is cancelled by the cancel callback.
139 * Allspecified paths are relative to the work tree's root. Pass a pathlist
140 * with a single empty path "" to check out files across the entire work tree.
142 * Some operations may refuse to run while the work tree contains files from
143 * multiple base commits.
144 */
145 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
146 struct got_pathlist_head *, struct got_repository *,
147 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
149 /* Merge the differences between two commits into a work tree. */
150 const struct got_error *
151 got_worktree_merge_files(struct got_worktree *,
152 struct got_object_id *, struct got_object_id *,
153 struct got_repository *, got_worktree_checkout_cb, void *,
154 got_cancel_cb, void *);
156 /*
157 * A callback function which is invoked to report a file's status.
159 * If a valid directory file descriptor and a directory entry name are passed,
160 * these should be used to open the file instead of opening the file by path.
161 * This prevents race conditions if the filesystem is modified concurrently.
162 * If the directory descriptor is not available then its value will be -1.
163 */
164 typedef const struct got_error *(*got_worktree_status_cb)(void *,
165 unsigned char, unsigned char, const char *, struct got_object_id *,
166 struct got_object_id *, struct got_object_id *, int, const char *);
168 /*
169 * Report the status of paths in the work tree.
170 * The status callback will be invoked with the provided void * argument,
171 * a path, and a corresponding status code.
172 */
173 const struct got_error *got_worktree_status(struct got_worktree *,
174 struct got_pathlist_head *, struct got_repository *, int no_ignores,
175 got_worktree_status_cb, void *, got_cancel_cb cancel_cb, void *);
177 /*
178 * Try to resolve a user-provided path to an on-disk path in the work tree.
179 * The caller must dispose of the resolved path with free(3).
180 */
181 const struct got_error *got_worktree_resolve_path(char **,
182 struct got_worktree *, const char *);
184 /* Schedule files at on-disk paths for addition in the next commit. */
185 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
186 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
187 struct got_repository *, int);
189 /*
190 * Remove files from disk and schedule them to be deleted in the next commit.
191 * Don't allow deleting files with uncommitted modifications, unless the
192 * parameter 'delete_local_mods' is set.
193 */
194 const struct got_error *
195 got_worktree_schedule_delete(struct got_worktree *,
196 struct got_pathlist_head *, int, const char *,
197 got_worktree_delete_cb, void *, struct got_repository *, int, int);
199 /* A callback function which is used to select or reject a patch. */
200 typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
201 unsigned char, const char *, FILE *, int, int);
203 /* Values for result output parameter of got_wortree_patch_cb. */
204 #define GOT_PATCH_CHOICE_NONE 0
205 #define GOT_PATCH_CHOICE_YES 1
206 #define GOT_PATCH_CHOICE_NO 2
207 #define GOT_PATCH_CHOICE_QUIT 3
209 /*
210 * Revert a file at the specified path such that it matches its
211 * original state in the worktree's base commit.
212 * If the patch callback is not NULL, call it to select patch hunks to
213 * revert. Otherwise, revert the whole file found at each path.
214 */
215 const struct got_error *got_worktree_revert(struct got_worktree *,
216 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
217 got_worktree_patch_cb patch_cb, void *patch_arg,
218 struct got_repository *);
220 /*
221 * A callback function which is invoked when a commit message is requested.
222 * Passes a pathlist with a struct got_commitable * in the data pointer of
223 * each element, a pointer to the log message that must be set by the
224 * callback and will be freed after committing, and an argument passed
225 * through to the callback.
226 */
227 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
228 struct got_pathlist_head *, char **, void *);
230 /*
231 * Create a new commit from changes in the work tree.
232 * Return the ID of the newly created commit.
233 * The worktree's base commit will be set to this new commit.
234 * Files unaffected by this commit operation will retain their
235 * current base commit.
236 * An author and a non-empty log message must be specified.
237 * The name of the committer is optional (may be NULL).
238 * If a path to be committed contains a symlink which points outside
239 * of the path space under version control, raise an error unless
240 * committing of such paths is being forced by the caller.
241 */
242 const struct got_error *got_worktree_commit(struct got_object_id **,
243 struct got_worktree *, struct got_pathlist_head *, const char *,
244 const char *, int, got_worktree_commit_msg_cb, void *,
245 got_worktree_status_cb, void *, struct got_repository *);
247 /* Get the path of a commitable worktree item. */
248 const char *got_commitable_get_path(struct got_commitable *);
250 /* Get the status of a commitable worktree item. */
251 unsigned int got_commitable_get_status(struct got_commitable *);
253 /*
254 * Prepare for rebasing a branch onto the work tree's current branch.
255 * This function creates references to a temporary branch, the branch
256 * being rebased, and the work tree's current branch, under the
257 * "got/worktree/rebase/" namespace. These references are used to
258 * keep track of rebase operation state and are used as input and/or
259 * output arguments with other rebase-related functions.
260 * The function also returns a pointer to a fileindex which must be
261 * passed back to other rebase-related functions.
262 */
263 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
264 struct got_reference **, struct got_fileindex **, struct got_worktree *,
265 struct got_reference *, struct got_repository *);
267 /*
268 * Continue an interrupted rebase operation.
269 * This function returns existing references created when rebase was prepared,
270 * and the ID of the commit currently being rebased. This should be called
271 * before either resuming or aborting a rebase operation.
272 * The function also returns a pointer to a fileindex which must be
273 * passed back to other rebase-related functions.
274 */
275 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
276 struct got_reference **, struct got_reference **, struct got_reference **,
277 struct got_fileindex **, struct got_worktree *, struct got_repository *);
279 /* Check whether a, potentially interrupted, rebase operation is in progress. */
280 const struct got_error *got_worktree_rebase_in_progress(int *,
281 struct got_worktree *);
283 /*
284 * Merge changes from the commit currently being rebased into the work tree.
285 * Report affected files, including merge conflicts, via the specified
286 * progress callback. Also populate a list of affected paths which should
287 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
288 * This list must be initialized with TAILQ_INIT() and disposed of with
289 * got_worktree_rebase_pathlist_free().
290 */
291 const struct got_error *got_worktree_rebase_merge_files(
292 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
293 struct got_object_id *, struct got_object_id *, struct got_repository *,
294 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
296 /*
297 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
298 * branch and return the ID of the newly created commit. An optional list of
299 * merged paths can be provided; otherwise this function will perform a status
300 * crawl across the entire work tree to find paths to commit.
301 */
302 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
303 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
304 struct got_reference *, const char *, struct got_commit_object *,
305 struct got_object_id *, struct got_repository *);
307 /* Free a list of merged paths from got_worktree_merge_files. */
308 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
310 /* Postpone the rebase operation. Should be called after a merge conflict. */
311 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
312 struct got_fileindex *);
314 /*
315 * Complete the current rebase operation. This should be called once all
316 * commits have been rebased successfully.
317 * The create_backup parameter controls whether the rebased branch will
318 * be backed up via a reference in refs/got/backup/rebase/.
319 */
320 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
321 struct got_fileindex *, struct got_reference *, struct got_reference *,
322 struct got_reference *, struct got_repository *, int create_backup);
324 /*
325 * Abort the current rebase operation.
326 * Report reverted files via the specified progress callback.
327 */
328 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
329 struct got_fileindex *, struct got_repository *, struct got_reference *,
330 got_worktree_checkout_cb, void *);
332 /*
333 * Prepare for editing the history of the work tree's current branch.
334 * This function creates references to a temporary branch, and the
335 * work tree's current branch, under the "got/worktree/histedit/" namespace.
336 * These references are used to keep track of histedit operation state and
337 * are used as input and/or output arguments with other histedit-related
338 * functions.
339 */
340 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
341 struct got_reference **, struct got_object_id **, struct got_fileindex **,
342 struct got_worktree *, struct got_repository *);
344 /*
345 * Continue an interrupted histedit operation.
346 * This function returns existing references created when histedit was
347 * prepared and the ID of the commit currently being edited.
348 * It should be called before resuming or aborting a histedit operation.
349 */
350 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
351 struct got_reference **, struct got_reference **, struct got_object_id **,
352 struct got_fileindex **, struct got_worktree *, struct got_repository *);
354 /* Check whether a histedit operation is in progress. */
355 const struct got_error *got_worktree_histedit_in_progress(int *,
356 struct got_worktree *);
358 /*
359 * Merge changes from the commit currently being edited into the work tree.
360 * Report affected files, including merge conflicts, via the specified
361 * progress callback. Also populate a list of affected paths which should
362 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
363 * This list must be initialized with TAILQ_INIT() and disposed of with
364 * got_worktree_rebase_pathlist_free().
365 */
366 const struct got_error *got_worktree_histedit_merge_files(
367 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
368 struct got_object_id *, struct got_object_id *, struct got_repository *,
369 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
371 /*
372 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
373 * branch and return the ID of the newly created commit. An optional list of
374 * merged paths can be provided; otherwise this function will perform a status
375 * crawl across the entire work tree to find paths to commit.
376 * An optional log message can be provided which will be used instead of the
377 * commit's original message.
378 */
379 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
380 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
381 struct got_reference *, const char *, struct got_commit_object *,
382 struct got_object_id *, const char *, struct got_repository *);
384 /*
385 * Record the specified commit as skipped during histedit.
386 * This should be called for commits which get dropped or get folded into
387 * a subsequent commit.
388 */
389 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
390 struct got_object_id *, struct got_repository *);
392 /* Postpone the histedit operation. */
393 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
394 struct got_fileindex *);
396 /*
397 * Complete the current histedit operation. This should be called once all
398 * commits have been edited successfully.
399 */
400 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
401 struct got_fileindex *, struct got_reference *, struct got_reference *,
402 struct got_repository *);
404 /*
405 * Abort the current histedit operation.
406 * Report reverted files via the specified progress callback.
407 */
408 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
409 struct got_fileindex *, struct got_repository *, struct got_reference *,
410 struct got_object_id *, got_worktree_checkout_cb, void *);
412 /* Get the path to this work tree's histedit script file. */
413 const struct got_error *got_worktree_get_histedit_script_path(char **,
414 struct got_worktree *);
416 /*
417 * Prepare a work tree for integrating a branch.
418 * Return pointers to a fileindex and locked references which must be
419 * passed back to other integrate-related functions.
420 */
421 const struct got_error *
422 got_worktree_integrate_prepare(struct got_fileindex **,
423 struct got_reference **, struct got_reference **,
424 struct got_worktree *, const char *, struct got_repository *);
426 /*
427 * Carry out a prepared branch integration operation.
428 * Report affected files via the specified progress callback.
429 */
430 const struct got_error *got_worktree_integrate_continue(
431 struct got_worktree *, struct got_fileindex *, struct got_repository *,
432 struct got_reference *, struct got_reference *,
433 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
435 /* Abort a prepared branch integration operation. */
436 const struct got_error *got_worktree_integrate_abort(struct got_worktree *,
437 struct got_fileindex *, struct got_repository *,
438 struct got_reference *, struct got_reference *);
440 /* Postpone the merge operation. Should be called after a merge conflict. */
441 const struct got_error *got_worktree_merge_postpone(struct got_worktree *,
442 struct got_fileindex *);
444 /* Merge changes from the merge source branch into the worktree. */
445 const struct got_error *
446 got_worktree_merge_branch(struct got_worktree *worktree,
447 struct got_fileindex *fileindex,
448 struct got_object_id *yca_commit_id,
449 struct got_object_id *branch_tip,
450 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
451 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg);
453 /* Attempt to commit merged changes. */
454 const struct got_error *
455 got_worktree_merge_commit(struct got_object_id **new_commit_id,
456 struct got_worktree *worktree, struct got_fileindex *fileindex,
457 const char *author, const char *committer, int allow_bad_symlinks,
458 struct got_object_id *branch_tip, const char *branch_name,
459 struct got_repository *repo,
460 got_worktree_status_cb status_cb, void *status_arg);
462 /*
463 * Complete the merge operation.
464 * This should be called once changes have been successfully committed.
465 */
466 const struct got_error *got_worktree_merge_complete(
467 struct got_worktree *worktree, struct got_fileindex *fileindex,
468 struct got_repository *repo);
470 /* Check whether a merge operation is in progress. */
471 const struct got_error *got_worktree_merge_in_progress(int *,
472 struct got_worktree *, struct got_repository *);
474 /*
475 * Prepare for merging a branch into the work tree's current branch.
476 * This function creates a reference to the branch being merged, and to
477 * this branch's current tip commit, in the "got/worktree/merge/" namespace.
478 * These references are used to keep track of merge operation state and are
479 * used as input and/or output arguments with other merge-related functions.
480 * The function also returns a pointer to a fileindex which must be
481 * passed back to other merge-related functions.
482 */
483 const struct got_error *got_worktree_merge_prepare(struct got_fileindex **,
484 struct got_worktree *, struct got_reference *, struct got_repository *);
486 /*
487 * Continue an interrupted merge operation.
488 * This function returns name of the branch being merged, and the ID of the
489 * tip commit being merged.
490 * This function should be called before either resuming or aborting a
491 * merge operation.
492 * The function also returns a pointer to a fileindex which must be
493 * passed back to other merge-related functions.
494 */
495 const struct got_error *got_worktree_merge_continue(char **,
496 struct got_object_id **, struct got_fileindex **,
497 struct got_worktree *, struct got_repository *);
499 /*
500 * Abort the current rebase operation.
501 * Report reverted files via the specified progress callback.
502 */
503 const struct got_error *got_worktree_merge_abort(struct got_worktree *,
504 struct got_fileindex *, struct got_repository *,
505 got_worktree_checkout_cb, void *);
507 /*
508 * Stage the specified paths for commit.
509 * If the patch callback is not NULL, call it to select patch hunks for
510 * staging. Otherwise, stage the full file content found at each path.
511 * If a path being staged contains a symlink which points outside
512 * of the path space under version control, raise an error unless
513 * staging of such paths is being forced by the caller.
514 */
515 const struct got_error *got_worktree_stage(struct got_worktree *,
516 struct got_pathlist_head *, got_worktree_status_cb, void *,
517 got_worktree_patch_cb, void *, int, struct got_repository *);
519 /*
520 * Merge staged changes for the specified paths back into the work tree
521 * and mark the paths as non-staged again.
522 */
523 const struct got_error *got_worktree_unstage(struct got_worktree *,
524 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
525 got_worktree_patch_cb, void *, struct got_repository *);
527 /* A callback function which is invoked with per-path info. */
528 typedef const struct got_error *(*got_worktree_path_info_cb)(void *,
529 const char *path, mode_t mode, time_t mtime,
530 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
531 struct got_object_id *commit_id);
533 /*
534 * Report work-tree meta data for paths in the work tree.
535 * The info callback will be invoked with the provided void * argument,
536 * a path, and meta-data arguments (see got_worktree_path_info_cb).
537 */
538 const struct got_error *
539 got_worktree_path_info(struct got_worktree *, struct got_pathlist_head *,
540 got_worktree_path_info_cb, void *, got_cancel_cb , void *);
542 /* References pointing at pre-rebase commit backups. */
543 #define GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX "refs/got/backup/rebase"
545 /* References pointing at pre-histedit commit backups. */
546 #define GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX "refs/got/backup/histedit"
548 /*
549 * Prepare for applying a patch.
550 */
551 const struct got_error *
552 got_worktree_patch_prepare(struct got_fileindex **, char **,
553 struct got_worktree *);
555 /*
556 * Lookup paths for the "old" and "new" file before patching and check their
557 * status.
558 */
559 const struct got_error *
560 got_worktree_patch_check_path(const char *, const char *, char **, char **,
561 struct got_worktree *, struct got_repository *, struct got_fileindex *);
563 const struct got_error *
564 got_worktree_patch_schedule_add(const char *, struct got_repository *,
565 struct got_worktree *, struct got_fileindex *, got_worktree_checkout_cb,
566 void *);
568 const struct got_error *
569 got_worktree_patch_schedule_rm(const char *, struct got_repository *,
570 struct got_worktree *, struct got_fileindex *, got_worktree_delete_cb,
571 void *);
573 /* Complete the patch operation. */
574 const struct got_error *
575 got_worktree_patch_complete(struct got_fileindex *, const char *);