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