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