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, the path to a file which contains a diff of changes to be
224 * committed (may be NULL), and a pointer to the log message that must be
225 * set by the callback and will be freed after committing, and an argument
226 * passed through to the callback.
227 */
228 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
229 struct got_pathlist_head *, const char *, char **, void *);
231 /*
232 * Create a new commit from changes in the work tree.
233 * Return the ID of the newly created commit.
234 * The worktree's base commit will be set to this new commit.
235 * Files unaffected by this commit operation will retain their
236 * current base commit.
237 * An author and a non-empty log message must be specified.
238 * The name of the committer is optional (may be NULL).
239 * If a path to be committed contains a symlink which points outside
240 * of the path space under version control, raise an error unless
241 * committing of such paths is being forced by the caller.
242 */
243 const struct got_error *got_worktree_commit(struct got_object_id **,
244 struct got_worktree *, struct got_pathlist_head *, const char *,
245 const char *, int, int, got_worktree_commit_msg_cb, void *,
246 got_worktree_status_cb, void *, struct got_repository *);
248 /* Get the path of a commitable worktree item. */
249 const char *got_commitable_get_path(struct got_commitable *);
251 /* Get the status of a commitable worktree item. */
252 unsigned int got_commitable_get_status(struct got_commitable *);
254 /*
255 * Prepare for rebasing a branch onto the work tree's current branch.
256 * This function creates references to a temporary branch, the branch
257 * being rebased, and the work tree's current branch, under the
258 * "got/worktree/rebase/" namespace. These references are used to
259 * keep track of rebase operation state and are used as input and/or
260 * output arguments with other rebase-related functions.
261 * The function also returns a pointer to a fileindex which must be
262 * passed back to other rebase-related functions.
263 */
264 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
265 struct got_reference **, struct got_fileindex **, struct got_worktree *,
266 struct got_reference *, struct got_repository *);
268 /*
269 * Continue an interrupted rebase operation.
270 * This function returns existing references created when rebase was prepared,
271 * and the ID of the commit currently being rebased. This should be called
272 * before either resuming or aborting a rebase operation.
273 * The function also returns a pointer to a fileindex which must be
274 * passed back to other rebase-related functions.
275 */
276 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
277 struct got_reference **, struct got_reference **, struct got_reference **,
278 struct got_fileindex **, struct got_worktree *, struct got_repository *);
280 /* Check whether a, potentially interrupted, rebase operation is in progress. */
281 const struct got_error *got_worktree_rebase_in_progress(int *,
282 struct got_worktree *);
284 /*
285 * Merge changes from the commit currently being rebased into the work tree.
286 * Report affected files, including merge conflicts, via the specified
287 * progress callback. Also populate a list of affected paths which should
288 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
289 * This list must be initialized with TAILQ_INIT() and disposed of with
290 * got_worktree_rebase_pathlist_free().
291 */
292 const struct got_error *got_worktree_rebase_merge_files(
293 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
294 struct got_object_id *, struct got_object_id *, struct got_repository *,
295 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
297 /*
298 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
299 * branch and return the ID of the newly created commit. An optional list of
300 * merged paths can be provided; otherwise this function will perform a status
301 * crawl across the entire work tree to find paths to commit.
302 */
303 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
304 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
305 struct got_reference *, const char *, struct got_commit_object *,
306 struct got_object_id *, struct got_repository *);
308 /* Free a list of merged paths from got_worktree_merge_files. */
309 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
311 /* Postpone the rebase operation. Should be called after a merge conflict. */
312 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
313 struct got_fileindex *);
315 /*
316 * Complete the current rebase operation. This should be called once all
317 * commits have been rebased successfully.
318 * The create_backup parameter controls whether the rebased branch will
319 * be backed up via a reference in refs/got/backup/rebase/.
320 */
321 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
322 struct got_fileindex *, struct got_reference *, struct got_reference *,
323 struct got_reference *, struct got_repository *, int create_backup);
325 /*
326 * Abort the current rebase operation.
327 * Report reverted files via the specified progress callback.
328 */
329 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
330 struct got_fileindex *, struct got_repository *, struct got_reference *,
331 got_worktree_checkout_cb, void *);
333 /*
334 * Prepare for editing the history of the work tree's current branch.
335 * This function creates references to a temporary branch, and the
336 * work tree's current branch, under the "got/worktree/histedit/" namespace.
337 * These references are used to keep track of histedit operation state and
338 * are used as input and/or output arguments with other histedit-related
339 * functions.
340 */
341 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
342 struct got_reference **, struct got_object_id **, struct got_fileindex **,
343 struct got_worktree *, struct got_repository *);
345 /*
346 * Continue an interrupted histedit operation.
347 * This function returns existing references created when histedit was
348 * prepared and the ID of the commit currently being edited.
349 * It should be called before resuming or aborting a histedit operation.
350 */
351 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
352 struct got_reference **, struct got_reference **, struct got_object_id **,
353 struct got_fileindex **, struct got_worktree *, struct got_repository *);
355 /* Check whether a histedit operation is in progress. */
356 const struct got_error *got_worktree_histedit_in_progress(int *,
357 struct got_worktree *);
359 /*
360 * Merge changes from the commit currently being edited into the work tree.
361 * Report affected files, including merge conflicts, via the specified
362 * progress callback. Also populate a list of affected paths which should
363 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
364 * This list must be initialized with TAILQ_INIT() and disposed of with
365 * got_worktree_rebase_pathlist_free().
366 */
367 const struct got_error *got_worktree_histedit_merge_files(
368 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
369 struct got_object_id *, struct got_object_id *, struct got_repository *,
370 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
372 /*
373 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
374 * branch and return the ID of the newly created commit. An optional list of
375 * merged paths can be provided; otherwise this function will perform a status
376 * crawl across the entire work tree to find paths to commit.
377 * An optional log message can be provided which will be used instead of the
378 * commit's original message.
379 */
380 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
381 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
382 struct got_reference *, const char *, struct got_commit_object *,
383 struct got_object_id *, const char *, struct got_repository *);
385 /*
386 * Record the specified commit as skipped during histedit.
387 * This should be called for commits which get dropped or get folded into
388 * a subsequent commit.
389 */
390 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
391 struct got_object_id *, struct got_repository *);
393 /* Postpone the histedit operation. */
394 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
395 struct got_fileindex *);
397 /*
398 * Complete the current histedit operation. This should be called once all
399 * commits have been edited successfully.
400 */
401 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
402 struct got_fileindex *, struct got_reference *, struct got_reference *,
403 struct got_repository *);
405 /*
406 * Abort the current histedit operation.
407 * Report reverted files via the specified progress callback.
408 */
409 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
410 struct got_fileindex *, struct got_repository *, struct got_reference *,
411 struct got_object_id *, got_worktree_checkout_cb, void *);
413 /* Get the path to this work tree's histedit script file. */
414 const struct got_error *got_worktree_get_histedit_script_path(char **,
415 struct got_worktree *);
417 /*
418 * Prepare a work tree for integrating a branch.
419 * Return pointers to a fileindex and locked references which must be
420 * passed back to other integrate-related functions.
421 */
422 const struct got_error *
423 got_worktree_integrate_prepare(struct got_fileindex **,
424 struct got_reference **, struct got_reference **,
425 struct got_worktree *, const char *, struct got_repository *);
427 /*
428 * Carry out a prepared branch integration operation.
429 * Report affected files via the specified progress callback.
430 */
431 const struct got_error *got_worktree_integrate_continue(
432 struct got_worktree *, struct got_fileindex *, struct got_repository *,
433 struct got_reference *, struct got_reference *,
434 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
436 /* Abort a prepared branch integration operation. */
437 const struct got_error *got_worktree_integrate_abort(struct got_worktree *,
438 struct got_fileindex *, struct got_repository *,
439 struct got_reference *, struct got_reference *);
441 /* Postpone the merge operation. Should be called after a merge conflict. */
442 const struct got_error *got_worktree_merge_postpone(struct got_worktree *,
443 struct got_fileindex *);
445 /* Merge changes from the merge source branch into the worktree. */
446 const struct got_error *
447 got_worktree_merge_branch(struct got_worktree *worktree,
448 struct got_fileindex *fileindex,
449 struct got_object_id *yca_commit_id,
450 struct got_object_id *branch_tip,
451 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
452 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg);
454 /* Attempt to commit merged changes. */
455 const struct got_error *
456 got_worktree_merge_commit(struct got_object_id **new_commit_id,
457 struct got_worktree *worktree, struct got_fileindex *fileindex,
458 const char *author, const char *committer, int allow_bad_symlinks,
459 struct got_object_id *branch_tip, const char *branch_name,
460 struct got_repository *repo,
461 got_worktree_status_cb status_cb, void *status_arg);
463 /*
464 * Complete the merge operation.
465 * This should be called once changes have been successfully committed.
466 */
467 const struct got_error *got_worktree_merge_complete(
468 struct got_worktree *worktree, struct got_fileindex *fileindex,
469 struct got_repository *repo);
471 /* Check whether a merge operation is in progress. */
472 const struct got_error *got_worktree_merge_in_progress(int *,
473 struct got_worktree *, struct got_repository *);
475 /*
476 * Prepare for merging a branch into the work tree's current branch.
477 * This function creates a reference to the branch being merged, and to
478 * this branch's current tip commit, in the "got/worktree/merge/" namespace.
479 * These references are used to keep track of merge operation state and are
480 * used as input and/or output arguments with other merge-related functions.
481 * The function also returns a pointer to a fileindex which must be
482 * passed back to other merge-related functions.
483 */
484 const struct got_error *got_worktree_merge_prepare(struct got_fileindex **,
485 struct got_worktree *, struct got_reference *, struct got_repository *);
487 /*
488 * Continue an interrupted merge operation.
489 * This function returns name of the branch being merged, and the ID of the
490 * tip commit being merged.
491 * This function should be called before either resuming or aborting a
492 * merge operation.
493 * The function also returns a pointer to a fileindex which must be
494 * passed back to other merge-related functions.
495 */
496 const struct got_error *got_worktree_merge_continue(char **,
497 struct got_object_id **, struct got_fileindex **,
498 struct got_worktree *, struct got_repository *);
500 /*
501 * Abort the current rebase operation.
502 * Report reverted files via the specified progress callback.
503 */
504 const struct got_error *got_worktree_merge_abort(struct got_worktree *,
505 struct got_fileindex *, struct got_repository *,
506 got_worktree_checkout_cb, void *);
508 /*
509 * Stage the specified paths for commit.
510 * If the patch callback is not NULL, call it to select patch hunks for
511 * staging. Otherwise, stage the full file content found at each path.
512 * If a path being staged contains a symlink which points outside
513 * of the path space under version control, raise an error unless
514 * staging of such paths is being forced by the caller.
515 */
516 const struct got_error *got_worktree_stage(struct got_worktree *,
517 struct got_pathlist_head *, got_worktree_status_cb, void *,
518 got_worktree_patch_cb, void *, int, struct got_repository *);
520 /*
521 * Merge staged changes for the specified paths back into the work tree
522 * and mark the paths as non-staged again.
523 */
524 const struct got_error *got_worktree_unstage(struct got_worktree *,
525 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
526 got_worktree_patch_cb, void *, struct got_repository *);
528 /* A callback function which is invoked with per-path info. */
529 typedef const struct got_error *(*got_worktree_path_info_cb)(void *,
530 const char *path, mode_t mode, time_t mtime,
531 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
532 struct got_object_id *commit_id);
534 /*
535 * Report work-tree meta data for paths in the work tree.
536 * The info callback will be invoked with the provided void * argument,
537 * a path, and meta-data arguments (see got_worktree_path_info_cb).
538 */
539 const struct got_error *
540 got_worktree_path_info(struct got_worktree *, struct got_pathlist_head *,
541 got_worktree_path_info_cb, void *, got_cancel_cb , void *);
543 /* References pointing at pre-rebase commit backups. */
544 #define GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX "refs/got/backup/rebase"
546 /* References pointing at pre-histedit commit backups. */
547 #define GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX "refs/got/backup/histedit"
549 /*
550 * Prepare for applying a patch.
551 */
552 const struct got_error *
553 got_worktree_patch_prepare(struct got_fileindex **, char **,
554 struct got_worktree *);
556 /*
557 * Lookup paths for the "old" and "new" file before patching and check their
558 * status.
559 */
560 const struct got_error *
561 got_worktree_patch_check_path(const char *, const char *, char **, char **,
562 struct got_worktree *, struct got_repository *, struct got_fileindex *);
564 const struct got_error *
565 got_worktree_patch_schedule_add(const char *, struct got_repository *,
566 struct got_worktree *, struct got_fileindex *, got_worktree_checkout_cb,
567 void *);
569 const struct got_error *
570 got_worktree_patch_schedule_rm(const char *, struct got_repository *,
571 struct got_worktree *, struct got_fileindex *, got_worktree_delete_cb,
572 void *);
574 /* Complete the patch operation. */
575 const struct got_error *
576 got_worktree_patch_complete(struct got_fileindex *, const char *);