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'
41 /*
42 * Attempt to initialize a new work tree on disk.
43 * The first argument is the path to a directory where the work tree
44 * will be created. The path itself must not yet exist, but the dirname(3)
45 * of the path must already exist.
46 * The reference provided will be used to determine the new worktree's
47 * base commit. The third argument speficies the work tree's path prefix.
48 */
49 const struct got_error *got_worktree_init(const char *, struct got_reference *,
50 const char *, struct got_repository *);
52 /*
53 * Attempt to open a worktree at or above the specified path.
54 * The caller must dispose of it with got_worktree_close().
55 */
56 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
58 /* Dispose of an open work tree. */
59 const struct got_error *got_worktree_close(struct got_worktree *);
61 /*
62 * Get the path to the root directory of a worktree.
63 */
64 const char *got_worktree_get_root_path(struct got_worktree *);
66 /*
67 * Get the path to the repository associated with a worktree.
68 */
69 const char *got_worktree_get_repo_path(struct got_worktree *);
71 /*
72 * Get the path prefix associated with a worktree.
73 */
74 const char *got_worktree_get_path_prefix(struct got_worktree *);
76 /*
77 * Check if a user-provided path prefix matches that of the worktree.
78 */
79 const struct got_error *got_worktree_match_path_prefix(int *,
80 struct got_worktree *, const char *);
82 /*
83 * Get the name of a work tree's HEAD reference.
84 */
85 const char *got_worktree_get_head_ref_name(struct got_worktree *);
87 /*
88 * Set the branch head reference of the work tree.
89 */
90 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
91 struct got_reference *);
93 /*
94 * Get the current base commit ID of a worktree.
95 */
96 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
98 /*
99 * Set the base commit Id of a worktree.
100 */
101 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
102 struct got_repository *, struct got_object_id *);
104 /* A callback function which is invoked when a path is checked out. */
105 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
106 unsigned char, const char *);
108 /* A callback function which is invoked when a path is removed. */
109 typedef const struct got_error *(*got_worktree_delete_cb)(void *,
110 unsigned char, unsigned char, const char *);
112 /*
113 * Attempt to check out files into a work tree from its associated repository
114 * and path prefix, and update the work tree's file index accordingly.
115 * File content is obtained from blobs within the work tree's path prefix
116 * inside the tree corresponding to the work tree's base commit.
117 * The checkout progress callback will be invoked with the provided
118 * void * argument, and the path of each checked out file.
120 * It is possible to restrict the checkout operation to specific paths in
121 * the work tree, in which case all files outside those paths will remain at
122 * their currently recorded base commit. Inconsistent base commits can be
123 * repaired later by running another update operation across the entire work
124 * tree. Inconsistent base-commits may also occur if this function runs into
125 * an error or if the checkout operation is cancelled by the cancel callback.
126 * Allspecified paths are relative to the work tree's root. Pass a pathlist
127 * with a single empty path "" to check out files across the entire work tree.
129 * Some operations may refuse to run while the work tree contains files from
130 * multiple base commits.
131 */
132 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
133 struct got_pathlist_head *, struct got_repository *,
134 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
136 /* Merge the differences between two commits into a work tree. */
137 const struct got_error *
138 got_worktree_merge_files(struct got_worktree *,
139 struct got_object_id *, struct got_object_id *,
140 struct got_repository *, got_worktree_checkout_cb, void *,
141 got_cancel_cb, void *);
143 /*
144 * A callback function which is invoked to report a file's status.
146 * If a valid directory file descriptor and a directory entry name are passed,
147 * these should be used to open the file instead of opening the file by path.
148 * This prevents race conditions if the filesystem is modified concurrently.
149 * If the directory descriptor is not available then its value will be -1.
150 */
151 typedef const struct got_error *(*got_worktree_status_cb)(void *,
152 unsigned char, unsigned char, const char *, struct got_object_id *,
153 struct got_object_id *, struct got_object_id *, int, const char *);
155 /*
156 * Report the status of paths in the work tree.
157 * The status callback will be invoked with the provided void * argument,
158 * a path, and a corresponding status code.
159 */
160 const struct got_error *got_worktree_status(struct got_worktree *,
161 struct got_pathlist_head *, struct got_repository *,
162 got_worktree_status_cb, void *, got_cancel_cb cancel_cb, void *);
164 /*
165 * Try to resolve a user-provided path to an on-disk path in the work tree.
166 * The caller must dispose of the resolved path with free(3).
167 */
168 const struct got_error *got_worktree_resolve_path(char **,
169 struct got_worktree *, const char *);
171 /* Schedule files at on-disk paths for addition in the next commit. */
172 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
173 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
174 struct got_repository *, int);
176 /*
177 * Remove files from disk and schedule them to be deleted in the next commit.
178 * Don't allow deleting files with uncommitted modifications, unless the
179 * parameter 'delete_local_mods' is set.
180 */
181 const struct got_error *
182 got_worktree_schedule_delete(struct got_worktree *,
183 struct got_pathlist_head *, int, got_worktree_delete_cb, void *,
184 struct got_repository *, int);
186 /* A callback function which is used to select or reject a patch. */
187 typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
188 unsigned char, const char *, FILE *, int, int);
190 /* Values for result output parameter of got_wortree_patch_cb. */
191 #define GOT_PATCH_CHOICE_NONE 0
192 #define GOT_PATCH_CHOICE_YES 1
193 #define GOT_PATCH_CHOICE_NO 2
194 #define GOT_PATCH_CHOICE_QUIT 3
196 /*
197 * Revert a file at the specified path such that it matches its
198 * original state in the worktree's base commit.
199 * If the patch callback is not NULL, call it to select patch hunks to
200 * revert. Otherwise, revert the whole file found at each path.
201 */
202 const struct got_error *got_worktree_revert(struct got_worktree *,
203 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
204 got_worktree_patch_cb patch_cb, void *patch_arg,
205 struct got_repository *);
207 /*
208 * A callback function which is invoked when a commit message is requested.
209 * Passes a pathlist with a struct got_commitable * in the data pointer of
210 * each element, a pointer to the log message that must be set by the
211 * callback and will be freed after committing, and an argument passed
212 * through to the callback.
213 */
214 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
215 struct got_pathlist_head *, char **, void *);
217 /*
218 * Create a new commit from changes in the work tree.
219 * Return the ID of the newly created commit.
220 * The worktree's base commit will be set to this new commit.
221 * Files unaffected by this commit operation will retain their
222 * current base commit.
223 * An author and a non-empty log message must be specified.
224 * The name of the committer is optional (may be NULL).
225 */
226 const struct got_error *got_worktree_commit(struct got_object_id **,
227 struct got_worktree *, struct got_pathlist_head *, const char *,
228 const char *, got_worktree_commit_msg_cb, void *,
229 got_worktree_status_cb, void *, struct got_repository *);
231 /* Get the path of a commitable worktree item. */
232 const char *got_commitable_get_path(struct got_commitable *);
234 /* Get the status of a commitable worktree item. */
235 unsigned int got_commitable_get_status(struct got_commitable *);
237 /*
238 * Prepare for rebasing a branch onto the work tree's current branch.
239 * This function creates references to a temporary branch, the branch
240 * being rebased, and the work tree's current branch, under the
241 * "got/worktree/rebase/" namespace. These references are used to
242 * keep track of rebase operation state and are used as input and/or
243 * output arguments with other rebase-related functions.
244 * The function also returns a pointer to a fileindex which must be
245 * passed back to other rebase-related functions.
246 */
247 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
248 struct got_reference **, struct got_fileindex **, struct got_worktree *,
249 struct got_reference *, struct got_repository *);
251 /*
252 * Continue an interrupted rebase operation.
253 * This function returns existing references created when rebase was prepared,
254 * and the ID of the commit currently being rebased. This should be called
255 * before either resuming or aborting a rebase operation.
256 * The function also returns a pointer to a fileindex which must be
257 * passed back to other rebase-related functions.
258 */
259 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
260 struct got_reference **, struct got_reference **, struct got_reference **,
261 struct got_fileindex **, struct got_worktree *, struct got_repository *);
263 /* Check whether a, potentially interrupted, rebase operation is in progress. */
264 const struct got_error *got_worktree_rebase_in_progress(int *,
265 struct got_worktree *);
267 /*
268 * Merge changes from the commit currently being rebased into the work tree.
269 * Report affected files, including merge conflicts, via the specified
270 * progress callback. Also populate a list of affected paths which should
271 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
272 * This list must be initialized with TAILQ_INIT() and disposed of with
273 * got_worktree_rebase_pathlist_free().
274 */
275 const struct got_error *got_worktree_rebase_merge_files(
276 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
277 struct got_object_id *, struct got_object_id *, struct got_repository *,
278 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
280 /*
281 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
282 * branch and return the ID of the newly created commit. An optional list of
283 * merged paths can be provided; otherwise this function will perform a status
284 * crawl across the entire work tree to find paths to commit.
285 */
286 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
287 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
288 struct got_reference *, struct got_commit_object *,
289 struct got_object_id *, struct got_repository *);
291 /* Free a list of merged paths from got_worktree_merge_files. */
292 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
294 /* Postpone the rebase operation. Should be called after a merge conflict. */
295 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
296 struct got_fileindex *);
298 /*
299 * Complete the current rebase operation. This should be called once all
300 * commits have been rebased successfully.
301 */
302 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
303 struct got_fileindex *, struct got_reference *, struct got_reference *,
304 struct got_reference *, struct got_repository *);
306 /*
307 * Abort the current rebase operation.
308 * Report reverted files via the specified progress callback.
309 */
310 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
311 struct got_fileindex *, struct got_repository *, struct got_reference *,
312 got_worktree_checkout_cb, void *);
314 /*
315 * Prepare for editing the history of the work tree's current branch.
316 * This function creates references to a temporary branch, and the
317 * work tree's current branch, under the "got/worktree/histedit/" namespace.
318 * These references are used to keep track of histedit operation state and
319 * are used as input and/or output arguments with other histedit-related
320 * functions.
321 */
322 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
323 struct got_reference **, struct got_object_id **, struct got_fileindex **,
324 struct got_worktree *, struct got_repository *);
326 /*
327 * Continue an interrupted histedit operation.
328 * This function returns existing references created when histedit was
329 * prepared and the ID of the commit currently being edited.
330 * It should be called before resuming or aborting a histedit operation.
331 */
332 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
333 struct got_reference **, struct got_reference **, struct got_object_id **,
334 struct got_fileindex **, struct got_worktree *, struct got_repository *);
336 /* Check whether a histedit operation is in progress. */
337 const struct got_error *got_worktree_histedit_in_progress(int *,
338 struct got_worktree *);
340 /*
341 * Merge changes from the commit currently being edited into the work tree.
342 * Report affected files, including merge conflicts, via the specified
343 * progress callback. Also populate a list of affected paths which should
344 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
345 * This list must be initialized with TAILQ_INIT() and disposed of with
346 * got_worktree_rebase_pathlist_free().
347 */
348 const struct got_error *got_worktree_histedit_merge_files(
349 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
350 struct got_object_id *, struct got_object_id *, struct got_repository *,
351 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
353 /*
354 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
355 * branch and return the ID of the newly created commit. An optional list of
356 * merged paths can be provided; otherwise this function will perform a status
357 * crawl across the entire work tree to find paths to commit.
358 * An optional log message can be provided which will be used instead of the
359 * commit's original message.
360 */
361 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
362 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
363 struct got_reference *, struct got_commit_object *,
364 struct got_object_id *, const char *, struct got_repository *);
366 /*
367 * Record the specified commit as skipped during histedit.
368 * This should be called for commits which get dropped or get folded into
369 * a subsequent commit.
370 */
371 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
372 struct got_object_id *, struct got_repository *);
374 /* Postpone the histedit operation. */
375 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
376 struct got_fileindex *);
378 /*
379 * Complete the current histedit operation. This should be called once all
380 * commits have been edited successfully.
381 */
382 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
383 struct got_fileindex *, struct got_reference *, struct got_reference *,
384 struct got_repository *);
386 /*
387 * Abort the current histedit operation.
388 * Report reverted files via the specified progress callback.
389 */
390 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
391 struct got_fileindex *, struct got_repository *, struct got_reference *,
392 struct got_object_id *, got_worktree_checkout_cb, void *);
394 /* Get the path to this work tree's histedit script file. */
395 const struct got_error *got_worktree_get_histedit_script_path(char **,
396 struct got_worktree *);
398 /*
399 * Prepare a work tree for integrating a branch.
400 * Return pointers to a fileindex and locked references which must be
401 * passed back to other integrate-related functions.
402 */
403 const struct got_error *
404 got_worktree_integrate_prepare(struct got_fileindex **,
405 struct got_reference **, struct got_reference **,
406 struct got_worktree *, const char *, struct got_repository *);
408 /*
409 * Carry out a prepared branch integration operation.
410 * Report affected files via the specified progress callback.
411 */
412 const struct got_error *got_worktree_integrate_continue(
413 struct got_worktree *, struct got_fileindex *, struct got_repository *,
414 struct got_reference *, struct got_reference *,
415 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
417 /* Abort a prepared branch integration operation. */
418 const struct got_error *got_worktree_integrate_abort(struct got_worktree *,
419 struct got_fileindex *, struct got_repository *,
420 struct got_reference *, struct got_reference *);
422 /*
423 * Stage the specified paths for commit.
424 * If the patch callback is not NULL, call it to select patch hunks for
425 * staging. Otherwise, stage the full file content found at each path.
426 */
427 const struct got_error *got_worktree_stage(struct got_worktree *,
428 struct got_pathlist_head *, got_worktree_status_cb, void *,
429 got_worktree_patch_cb, void *, struct got_repository *);
431 /*
432 * Merge staged changes for the specified paths back into the work tree
433 * and mark the paths as non-staged again.
434 */
435 const struct got_error *got_worktree_unstage(struct got_worktree *,
436 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
437 got_worktree_patch_cb, void *, struct got_repository *);