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