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 /* A callback function which is invoked to report a path's status. */
143 typedef const struct got_error *(*got_worktree_status_cb)(void *,
144 unsigned char, unsigned char, const char *, struct got_object_id *,
145 struct got_object_id *, struct got_object_id *);
147 /*
148 * Report the status of paths in the work tree.
149 * The status callback will be invoked with the provided void * argument,
150 * a path, and a corresponding status code.
151 */
152 const struct got_error *got_worktree_status(struct got_worktree *,
153 struct got_pathlist_head *, struct got_repository *,
154 got_worktree_status_cb, void *, got_cancel_cb cancel_cb, void *);
156 /*
157 * Try to resolve a user-provided path to an on-disk path in the work tree.
158 * The caller must dispose of the resolved path with free(3).
159 */
160 const struct got_error *got_worktree_resolve_path(char **,
161 struct got_worktree *, const char *);
163 /* Schedule files at on-disk paths for addition in the next commit. */
164 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
165 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
166 struct got_repository *, int);
168 /*
169 * Remove files from disk and schedule them to be deleted in the next commit.
170 * Don't allow deleting files with uncommitted modifications, unless the
171 * parameter 'delete_local_mods' is set.
172 */
173 const struct got_error *
174 got_worktree_schedule_delete(struct got_worktree *,
175 struct got_pathlist_head *, int, got_worktree_delete_cb, void *,
176 struct got_repository *);
178 /* A callback function which is used to select or reject a patch. */
179 typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
180 unsigned char, const char *, FILE *, int, int);
182 /* Values for result output parameter of got_wortree_patch_cb. */
183 #define GOT_PATCH_CHOICE_NONE 0
184 #define GOT_PATCH_CHOICE_YES 1
185 #define GOT_PATCH_CHOICE_NO 2
186 #define GOT_PATCH_CHOICE_QUIT 3
188 /*
189 * Revert a file at the specified path such that it matches its
190 * original state in the worktree's base commit.
191 * If the patch callback is not NULL, call it to select patch hunks to
192 * revert. Otherwise, revert the whole file found at each path.
193 */
194 const struct got_error *got_worktree_revert(struct got_worktree *,
195 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
196 got_worktree_patch_cb patch_cb, void *patch_arg,
197 struct got_repository *);
199 /*
200 * A callback function which is invoked when a commit message is requested.
201 * Passes a pathlist with a struct got_commitable * in the data pointer of
202 * each element, a pointer to the log message that must be set by the
203 * callback and will be freed after committing, and an argument passed
204 * through to the callback.
205 */
206 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
207 struct got_pathlist_head *, char **, void *);
209 /*
210 * Create a new commit from changes in the work tree.
211 * Return the ID of the newly created commit.
212 * The worktree's base commit will be set to this new commit.
213 * Files unaffected by this commit operation will retain their
214 * current base commit.
215 * An author and a non-empty log message must be specified.
216 * The name of the committer is optional (may be NULL).
217 */
218 const struct got_error *got_worktree_commit(struct got_object_id **,
219 struct got_worktree *, struct got_pathlist_head *, const char *,
220 const char *, got_worktree_commit_msg_cb, void *,
221 got_worktree_status_cb, void *, struct got_repository *);
223 /* Get the path of a commitable worktree item. */
224 const char *got_commitable_get_path(struct got_commitable *);
226 /* Get the status of a commitable worktree item. */
227 unsigned int got_commitable_get_status(struct got_commitable *);
229 /*
230 * Prepare for rebasing a branch onto the work tree's current branch.
231 * This function creates references to a temporary branch, the branch
232 * being rebased, and the work tree's current branch, under the
233 * "got/worktree/rebase/" namespace. These references are used to
234 * keep track of rebase operation state and are used as input and/or
235 * output arguments with other rebase-related functions.
236 * The function also returns a pointer to a fileindex which must be
237 * passed back to other rebase-related functions.
238 */
239 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
240 struct got_reference **, struct got_fileindex **, struct got_worktree *,
241 struct got_reference *, struct got_repository *);
243 /*
244 * Continue an interrupted rebase operation.
245 * This function returns existing references created when rebase was prepared,
246 * and the ID of the commit currently being rebased. This should be called
247 * before either resuming or aborting a rebase operation.
248 * The function also returns a pointer to a fileindex which must be
249 * passed back to other rebase-related functions.
250 */
251 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
252 struct got_reference **, struct got_reference **, struct got_reference **,
253 struct got_fileindex **, struct got_worktree *, struct got_repository *);
255 /* Check whether a, potentially interrupted, rebase operation is in progress. */
256 const struct got_error *got_worktree_rebase_in_progress(int *,
257 struct got_worktree *);
259 /*
260 * Merge changes from the commit currently being rebased into the work tree.
261 * Report affected files, including merge conflicts, via the specified
262 * progress callback. Also populate a list of affected paths which should
263 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
264 * This list must be initialized with TAILQ_INIT() and disposed of with
265 * got_worktree_rebase_pathlist_free().
266 */
267 const struct got_error *got_worktree_rebase_merge_files(
268 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
269 struct got_object_id *, struct got_object_id *, struct got_repository *,
270 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
272 /*
273 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
274 * branch and return the ID of the newly created commit. An optional list of
275 * merged paths can be provided; otherwise this function will perform a status
276 * crawl across the entire work tree to find paths to commit.
277 */
278 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
279 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
280 struct got_reference *, struct got_commit_object *,
281 struct got_object_id *, struct got_repository *);
283 /* Free a list of merged paths from got_worktree_merge_files. */
284 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
286 /* Postpone the rebase operation. Should be called after a merge conflict. */
287 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
288 struct got_fileindex *);
290 /*
291 * Complete the current rebase operation. This should be called once all
292 * commits have been rebased successfully.
293 */
294 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
295 struct got_fileindex *, struct got_reference *, struct got_reference *,
296 struct got_reference *, struct got_repository *);
298 /*
299 * Abort the current rebase operation.
300 * Report reverted files via the specified progress callback.
301 */
302 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
303 struct got_fileindex *, struct got_repository *, struct got_reference *,
304 got_worktree_checkout_cb, void *);
306 /*
307 * Prepare for editing the history of the work tree's current branch.
308 * This function creates references to a temporary branch, and the
309 * work tree's current branch, under the "got/worktree/histedit/" namespace.
310 * These references are used to keep track of histedit operation state and
311 * are used as input and/or output arguments with other histedit-related
312 * functions.
313 */
314 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
315 struct got_reference **, struct got_object_id **, struct got_fileindex **,
316 struct got_worktree *, struct got_repository *);
318 /*
319 * Continue an interrupted histedit operation.
320 * This function returns existing references created when histedit was
321 * prepared and the ID of the commit currently being edited.
322 * It should be called before resuming or aborting a histedit operation.
323 */
324 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
325 struct got_reference **, struct got_reference **, struct got_object_id **,
326 struct got_fileindex **, struct got_worktree *, struct got_repository *);
328 /* Check whether a histedit operation is in progress. */
329 const struct got_error *got_worktree_histedit_in_progress(int *,
330 struct got_worktree *);
332 /*
333 * Merge changes from the commit currently being edited into the work tree.
334 * Report affected files, including merge conflicts, via the specified
335 * progress callback. Also populate a list of affected paths which should
336 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
337 * This list must be initialized with TAILQ_INIT() and disposed of with
338 * got_worktree_rebase_pathlist_free().
339 */
340 const struct got_error *got_worktree_histedit_merge_files(
341 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
342 struct got_object_id *, struct got_object_id *, struct got_repository *,
343 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
345 /*
346 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
347 * branch and return the ID of the newly created commit. An optional list of
348 * merged paths can be provided; otherwise this function will perform a status
349 * crawl across the entire work tree to find paths to commit.
350 * An optional log message can be provided which will be used instead of the
351 * commit's original message.
352 */
353 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
354 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
355 struct got_reference *, struct got_commit_object *,
356 struct got_object_id *, const char *, struct got_repository *);
358 /*
359 * Record the specified commit as skipped during histedit.
360 * This should be called for commits which get dropped or get folded into
361 * a subsequent commit.
362 */
363 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
364 struct got_object_id *, struct got_repository *);
366 /* Postpone the histedit operation. */
367 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
368 struct got_fileindex *);
370 /*
371 * Complete the current histedit operation. This should be called once all
372 * commits have been edited successfully.
373 */
374 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
375 struct got_fileindex *, struct got_reference *, struct got_reference *,
376 struct got_repository *);
378 /*
379 * Abort the current histedit operation.
380 * Report reverted files via the specified progress callback.
381 */
382 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
383 struct got_fileindex *, struct got_repository *, struct got_reference *,
384 struct got_object_id *, got_worktree_checkout_cb, void *);
386 /* Get the path to this work tree's histedit script file. */
387 const struct got_error *got_worktree_get_histedit_script_path(char **,
388 struct got_worktree *);
390 /*
391 * Prepare a work tree for integrating a branch.
392 * Return pointers to a fileindex and locked references which must be
393 * passed back to other integrate-related functions.
394 */
395 const struct got_error *
396 got_worktree_integrate_prepare(struct got_fileindex **,
397 struct got_reference **, struct got_reference **,
398 struct got_worktree *, const char *, struct got_repository *);
400 /*
401 * Carry out a prepared branch integration operation.
402 * Report affected files via the specified progress callback.
403 */
404 const struct got_error *got_worktree_integrate_continue(
405 struct got_worktree *, struct got_fileindex *, struct got_repository *,
406 struct got_reference *, struct got_reference *,
407 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
409 /* Abort a prepared branch integration operation. */
410 const struct got_error *got_worktree_integrate_abort(struct got_worktree *,
411 struct got_fileindex *, struct got_repository *,
412 struct got_reference *, struct got_reference *);
414 /*
415 * Stage the specified paths for commit.
416 * If the patch callback is not NULL, call it to select patch hunks for
417 * staging. Otherwise, stage the full file content found at each path.
418 */
419 const struct got_error *got_worktree_stage(struct got_worktree *,
420 struct got_pathlist_head *, got_worktree_status_cb, void *,
421 got_worktree_patch_cb, void *, struct got_repository *);
423 /*
424 * Merge staged changes for the specified paths back into the work tree
425 * and mark the paths as non-staged again.
426 */
427 const struct got_error *got_worktree_unstage(struct got_worktree *,
428 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
429 got_worktree_patch_cb, void *, struct got_repository *);