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_CONFLICT 'C'
30 #define GOT_STATUS_MERGE 'G'
31 #define GOT_STATUS_MISSING '!'
32 #define GOT_STATUS_UNVERSIONED '?'
33 #define GOT_STATUS_OBSTRUCTED '~'
34 #define GOT_STATUS_NONEXISTENT 'N'
35 #define GOT_STATUS_REVERT 'R'
36 #define GOT_STATUS_CANNOT_DELETE 'd'
37 #define GOT_STATUS_BUMP_BASE 'b'
39 /*
40 * Attempt to initialize a new work tree on disk.
41 * The first argument is the path to a directory where the work tree
42 * will be created. The path itself must not yet exist, but the dirname(3)
43 * of the path must already exist.
44 * The reference provided will be used to determine the new worktree's
45 * base commit. The third argument speficies the work tree's path prefix.
46 */
47 const struct got_error *got_worktree_init(const char *, struct got_reference *,
48 const char *, struct got_repository *);
50 /*
51 * Attempt to open a worktree at or above the specified path.
52 * The caller must dispose of it with got_worktree_close().
53 */
54 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
56 /* Dispose of an open work tree. */
57 const struct got_error *got_worktree_close(struct got_worktree *);
59 /*
60 * Get the path to the root directory of a worktree.
61 */
62 const char *got_worktree_get_root_path(struct got_worktree *);
64 /*
65 * Get the path to the repository associated with a worktree.
66 */
67 const char *got_worktree_get_repo_path(struct got_worktree *);
69 /*
70 * Get the path prefix associated with a worktree.
71 */
72 const char *got_worktree_get_path_prefix(struct got_worktree *);
74 /*
75 * Check if a user-provided path prefix matches that of the worktree.
76 */
77 const struct got_error *got_worktree_match_path_prefix(int *,
78 struct got_worktree *, const char *);
80 /*
81 * Get the name of a work tree's HEAD reference.
82 */
83 const char *got_worktree_get_head_ref_name(struct got_worktree *);
85 /*
86 * Set the branch head reference of the work tree.
87 */
88 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
89 struct got_reference *);
91 /*
92 * Get the current base commit ID of a worktree.
93 */
94 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
96 /*
97 * Set the base commit Id of a worktree.
98 */
99 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
100 struct got_repository *, struct got_object_id *);
102 /* A callback function which is invoked when a path is checked out. */
103 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
104 unsigned char, const char *);
106 /*
107 * Attempt to check out files into a work tree from its associated repository
108 * and path prefix, and update the work tree's file index accordingly.
109 * File content is obtained from blobs within the work tree's path prefix
110 * inside the tree corresponding to the work tree's base commit.
111 * The checkout progress callback will be invoked with the provided
112 * void * argument, and the path of each checked out file.
114 * It is possible to restrict the checkout operation to specific paths in
115 * the work tree, in which case all files outside those paths will remain at
116 * their currently recorded base commit. Inconsistent base commits can be
117 * repaired later by running another update operation across the entire work
118 * tree. Inconsistent base-commits may also occur if this function runs into
119 * an error or if the checkout operation is cancelled by the cancel callback.
120 * Allspecified paths are relative to the work tree's root. Pass a pathlist
121 * with a single empty path "" to check out files across the entire work tree.
123 * Some operations may refuse to run while the work tree contains files from
124 * multiple base commits.
125 */
126 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
127 struct got_pathlist_head *, struct got_repository *,
128 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
130 /* Merge the differences between two commits into a work tree. */
131 const struct got_error *
132 got_worktree_merge_files(struct got_worktree *,
133 struct got_object_id *, struct got_object_id *,
134 struct got_repository *, got_worktree_checkout_cb, void *,
135 got_cancel_cb, void *);
137 /* A callback function which is invoked to report a path's status. */
138 typedef const struct got_error *(*got_worktree_status_cb)(void *,
139 unsigned char, unsigned char, const char *, struct got_object_id *,
140 struct got_object_id *, struct got_object_id *);
142 /*
143 * Report the status of paths in the work tree.
144 * The status callback will be invoked with the provided void * argument,
145 * a path, and a corresponding status code.
146 */
147 const struct got_error *got_worktree_status(struct got_worktree *,
148 struct got_pathlist_head *, struct got_repository *,
149 got_worktree_status_cb, void *, got_cancel_cb cancel_cb, void *);
151 /*
152 * Try to resolve a user-provided path to an on-disk path in the work tree.
153 * The caller must dispose of the resolved path with free(3).
154 */
155 const struct got_error *got_worktree_resolve_path(char **,
156 struct got_worktree *, const char *);
158 /* Schedule files at on-disk paths for addition in the next commit. */
159 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
160 struct got_pathlist_head *, got_worktree_status_cb, void *,
161 struct got_repository *);
163 /*
164 * Remove files from disk and schedule them to be deleted in the next commit.
165 * Don't allow deleting files with uncommitted modifications, unless the
166 * parameter 'delete_local_mods' is set.
167 */
168 const struct got_error *
169 got_worktree_schedule_delete(struct got_worktree *,
170 struct got_pathlist_head *, int, got_worktree_status_cb, void *,
171 struct got_repository *);
173 /* A callback function which is used to select or reject a patch. */
174 typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
175 unsigned char, const char *, FILE *, int, int);
177 /* Values for result output parameter of got_wortree_patch_cb. */
178 #define GOT_PATCH_CHOICE_NONE 0
179 #define GOT_PATCH_CHOICE_YES 1
180 #define GOT_PATCH_CHOICE_NO 2
181 #define GOT_PATCH_CHOICE_QUIT 3
183 /*
184 * Revert a file at the specified path such that it matches its
185 * original state in the worktree's base commit.
186 * If the patch callback is not NULL, call it to select patch hunks to
187 * revert. Otherwise, revert the whole file found at each path.
188 */
189 const struct got_error *got_worktree_revert(struct got_worktree *,
190 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
191 got_worktree_patch_cb patch_cb, void *patch_arg,
192 struct got_repository *);
194 /*
195 * A callback function which is invoked when a commit message is requested.
196 * Passes a pathlist with a struct got_commitable * in the data pointer of
197 * each element, a pointer to the log message that must be set by the
198 * callback and will be freed after committing, and an argument passed
199 * through to the callback.
200 */
201 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
202 struct got_pathlist_head *, char **, void *);
204 /*
205 * Create a new commit from changes in the work tree.
206 * Return the ID of the newly created commit.
207 * The worktree's base commit will be set to this new commit.
208 * Files unaffected by this commit operation will retain their
209 * current base commit.
210 * An author and a non-empty log message must be specified.
211 * The name of the committer is optional (may be NULL).
212 */
213 const struct got_error *got_worktree_commit(struct got_object_id **,
214 struct got_worktree *, struct got_pathlist_head *, const char *,
215 const char *, got_worktree_commit_msg_cb, void *,
216 got_worktree_status_cb, void *, struct got_repository *);
218 /* Get the path of a commitable worktree item. */
219 const char *got_commitable_get_path(struct got_commitable *);
221 /* Get the status of a commitable worktree item. */
222 unsigned int got_commitable_get_status(struct got_commitable *);
224 /*
225 * Prepare for rebasing a branch onto the work tree's current branch.
226 * This function creates references to a temporary branch, the branch
227 * being rebased, and the work tree's current branch, under the
228 * "got/worktree/rebase/" namespace. These references are used to
229 * keep track of rebase operation state and are used as input and/or
230 * output arguments with other rebase-related functions.
231 * The function also returns a pointer to a fileindex which must be
232 * passed back to other rebase-related functions.
233 */
234 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
235 struct got_reference **, struct got_fileindex **, struct got_worktree *,
236 struct got_reference *, struct got_repository *);
238 /*
239 * Continue an interrupted rebase operation.
240 * This function returns existing references created when rebase was prepared,
241 * and the ID of the commit currently being rebased. This should be called
242 * before either resuming or aborting a rebase operation.
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_continue(struct got_object_id **,
247 struct got_reference **, struct got_reference **, struct got_reference **,
248 struct got_fileindex **, struct got_worktree *, struct got_repository *);
250 /* Check whether a, potentially interrupted, rebase operation is in progress. */
251 const struct got_error *got_worktree_rebase_in_progress(int *,
252 struct got_worktree *);
254 /*
255 * Merge changes from the commit currently being rebased into the work tree.
256 * Report affected files, including merge conflicts, via the specified
257 * progress callback. Also populate a list of affected paths which should
258 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
259 * This list must be initialized with TAILQ_INIT() and disposed of with
260 * got_worktree_rebase_pathlist_free().
261 */
262 const struct got_error *got_worktree_rebase_merge_files(
263 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
264 struct got_object_id *, struct got_object_id *, struct got_repository *,
265 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
267 /*
268 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
269 * branch and return the ID of the newly created commit. An optional list of
270 * merged paths can be provided; otherwise this function will perform a status
271 * crawl across the entire work tree to find paths to commit.
272 */
273 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
274 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
275 struct got_reference *, struct got_commit_object *,
276 struct got_object_id *, struct got_repository *);
278 /* Free a list of merged paths from got_worktree_merge_files. */
279 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
281 /* Postpone the rebase operation. Should be called after a merge conflict. */
282 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
283 struct got_fileindex *);
285 /*
286 * Complete the current rebase operation. This should be called once all
287 * commits have been rebased successfully.
288 */
289 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
290 struct got_fileindex *, struct got_reference *, struct got_reference *,
291 struct got_reference *, struct got_repository *);
293 /*
294 * Abort the current rebase operation.
295 * Report reverted files via the specified progress callback.
296 */
297 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
298 struct got_fileindex *, struct got_repository *, struct got_reference *,
299 got_worktree_checkout_cb, void *);
301 /*
302 * Prepare for editing the history of the work tree's current branch.
303 * This function creates references to a temporary branch, and the
304 * work tree's current branch, under the "got/worktree/histedit/" namespace.
305 * These references are used to keep track of histedit operation state and
306 * are used as input and/or output arguments with other histedit-related
307 * functions.
308 */
309 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
310 struct got_reference **, struct got_object_id **, struct got_fileindex **,
311 struct got_worktree *, struct got_repository *);
313 /*
314 * Continue an interrupted histedit operation.
315 * This function returns existing references created when histedit was
316 * prepared and the ID of the commit currently being edited.
317 * It should be called before resuming or aborting a histedit operation.
318 */
319 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
320 struct got_reference **, struct got_reference **, struct got_object_id **,
321 struct got_fileindex **, struct got_worktree *, struct got_repository *);
323 /* Check whether a histedit operation is in progress. */
324 const struct got_error *got_worktree_histedit_in_progress(int *,
325 struct got_worktree *);
327 /*
328 * Merge changes from the commit currently being edited into the work tree.
329 * Report affected files, including merge conflicts, via the specified
330 * progress callback. Also populate a list of affected paths which should
331 * be passed to got_worktree_histedit_commit() after a conflict-free merge.
332 * This list must be initialized with TAILQ_INIT() and disposed of with
333 * got_worktree_rebase_pathlist_free().
334 */
335 const struct got_error *got_worktree_histedit_merge_files(
336 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
337 struct got_object_id *, struct got_object_id *, struct got_repository *,
338 got_worktree_checkout_cb, void *, got_cancel_cb, void *);
340 /*
341 * Commit changes merged by got_worktree_histedit_merge_files() to a temporary
342 * branch and return the ID of the newly created commit. An optional list of
343 * merged paths can be provided; otherwise this function will perform a status
344 * crawl across the entire work tree to find paths to commit.
345 * An optional log message can be provided which will be used instead of the
346 * commit's original message.
347 */
348 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
349 struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
350 struct got_reference *, struct got_commit_object *,
351 struct got_object_id *, const char *, struct got_repository *);
353 /*
354 * Record the specified commit as skipped during histedit.
355 * This should be called for commits which get dropped or get folded into
356 * a subsequent commit.
357 */
358 const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
359 struct got_object_id *, struct got_repository *);
361 /* Postpone the histedit operation. */
362 const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
363 struct got_fileindex *);
365 /*
366 * Complete the current histedit operation. This should be called once all
367 * commits have been edited successfully.
368 */
369 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
370 struct got_fileindex *, struct got_reference *, struct got_reference *,
371 struct got_repository *);
373 /*
374 * Abort the current histedit operation.
375 * Report reverted files via the specified progress callback.
376 */
377 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
378 struct got_fileindex *, struct got_repository *, struct got_reference *,
379 struct got_object_id *, got_worktree_checkout_cb, void *);
381 /* Get the path to this work tree's histedit script file. */
382 const struct got_error *got_worktree_get_histedit_script_path(char **,
383 struct got_worktree *);
386 /*
387 * Stage the specified paths for commit.
388 * If the patch callback is not NULL, call it to select patch hunks for
389 * staging. Otherwise, stage the full file content found at each path.
390 */
391 const struct got_error *got_worktree_stage(struct got_worktree *,
392 struct got_pathlist_head *, got_worktree_status_cb, void *,
393 got_worktree_patch_cb, void *, struct got_repository *);
395 /*
396 * Merge staged changes for the specified paths back into the work tree
397 * and mark the paths as non-staged again.
398 */
399 const struct got_error *got_worktree_unstage(struct got_worktree *,
400 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
401 got_worktree_patch_cb, void *, struct got_repository *);