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;
21 /* status codes */
22 #define GOT_STATUS_NO_CHANGE ' '
23 #define GOT_STATUS_ADD 'A'
24 #define GOT_STATUS_EXISTS 'E'
25 #define GOT_STATUS_UPDATE 'U'
26 #define GOT_STATUS_DELETE 'D'
27 #define GOT_STATUS_MODIFY 'M'
28 #define GOT_STATUS_CONFLICT 'C'
29 #define GOT_STATUS_MERGE 'G'
30 #define GOT_STATUS_MISSING '!'
31 #define GOT_STATUS_UNVERSIONED '?'
32 #define GOT_STATUS_OBSTRUCTED '~'
33 #define GOT_STATUS_REVERT 'R'
34 #define GOT_STATUS_CANNOT_DELETE 'd'
35 #define GOT_STATUS_BUMP_BASE 'b'
37 /*
38 * Attempt to initialize a new work tree on disk.
39 * The first argument is the path to a directory where the work tree
40 * will be created. The path itself must not yet exist, but the dirname(3)
41 * of the path must already exist.
42 * The reference provided will be used to determine the new worktree's
43 * base commit. The third argument speficies the work tree's path prefix.
44 */
45 const struct got_error *got_worktree_init(const char *, struct got_reference *,
46 const char *, struct got_repository *);
48 /*
49 * Attempt to open a worktree at or above the specified path.
50 * The caller must dispose of it with got_worktree_close().
51 */
52 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
54 /* Dispose of an open work tree. */
55 const struct got_error *got_worktree_close(struct got_worktree *);
57 /*
58 * Get the path to the root directory of a worktree.
59 */
60 const char *got_worktree_get_root_path(struct got_worktree *);
62 /*
63 * Get the path to the repository associated with a worktree.
64 */
65 const char *got_worktree_get_repo_path(struct got_worktree *);
67 /*
68 * Get the path prefix associated with a worktree.
69 */
70 const char *got_worktree_get_path_prefix(struct got_worktree *);
72 /*
73 * Check if a user-provided path prefix matches that of the worktree.
74 */
75 const struct got_error *got_worktree_match_path_prefix(int *,
76 struct got_worktree *, const char *);
78 /*
79 * Get the name of a work tree's HEAD reference.
80 */
81 const char *got_worktree_get_head_ref_name(struct got_worktree *);
83 /*
84 * Set the branch head reference of the work tree.
85 */
86 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
87 struct got_reference *);
89 /*
90 * Get the current base commit ID of a worktree.
91 */
92 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
94 /*
95 * Set the base commit Id of a worktree.
96 */
97 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
98 struct got_repository *, struct got_object_id *);
100 /* A callback function which is invoked when a path is checked out. */
101 typedef const struct got_error *(*got_worktree_checkout_cb)(void *,
102 unsigned char, const char *);
104 /* A callback function which is invoked at cancellation points.
105 * May return GOT_ERR_CANCELLED to abort the runing operation. */
106 typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
108 /*
109 * Attempt to check out files into a work tree from its associated repository
110 * and path prefix, and update the work tree's file index accordingly.
111 * File content is obtained from blobs within the work tree's path prefix
112 * inside the tree corresponding to the work tree's base commit.
113 * The checkout progress callback will be invoked with the provided
114 * void * argument, and the path of each checked out file.
116 * It is possible to restrict the checkout operation to a specific path in
117 * the work tree, in which case all files outside this path will remain at
118 * their currently recorded base commit. Inconsistent base commits can be
119 * repaired later by running another update operation across the entire work
120 * tree. Inconsistent base-commits may also occur if this function runs into
121 * an error or if the checkout operation is cancelled by the cancel callback.
122 * The specified path is relative to the work tree's root. Pass "" to check
123 * out files across the entire work tree.
125 * Some operations may refuse to run while the work tree contains files from
126 * multiple base commits.
127 */
128 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
129 const char *, struct got_repository *, got_worktree_checkout_cb, void *,
130 got_worktree_cancel_cb, void *);
132 /* Merge the differences between two commits into a work tree. */
133 const struct got_error *
134 got_worktree_merge_files(struct got_worktree *,
135 struct got_object_id *, struct got_object_id *,
136 struct got_repository *, got_worktree_checkout_cb, void *,
137 got_worktree_cancel_cb, void *);
139 /* A callback function which is invoked to report a path's status. */
140 typedef const struct got_error *(*got_worktree_status_cb)(void *,
141 unsigned char, const char *, struct got_object_id *,
142 struct got_object_id *);
144 /*
145 * Report the status of paths in the work tree.
146 * The status callback will be invoked with the provided void * argument,
147 * a path, and a corresponding status code.
148 */
149 const struct got_error *got_worktree_status(struct got_worktree *,
150 const char *, struct got_repository *, got_worktree_status_cb, void *,
151 got_worktree_cancel_cb cancel_cb, void *);
153 /*
154 * Try to resolve a user-provided path to an on-disk path in the work tree.
155 * The caller must dispose of the resolved path with free(3).
156 */
157 const struct got_error *got_worktree_resolve_path(char **,
158 struct got_worktree *, const char *);
160 /* Schedule files at on-disk paths for addition in the next commit. */
161 const struct got_error *got_worktree_schedule_add(struct got_worktree *,
162 struct got_pathlist_head *, got_worktree_status_cb, void *,
163 struct got_repository *);
165 /*
166 * Remove files from disk and schedule them to be deleted in the next commit.
167 * Don't allow deleting files with uncommitted modifications, unless the
168 * parameter 'delete_local_mods' is set.
169 */
170 const struct got_error *
171 got_worktree_schedule_delete(struct got_worktree *,
172 struct got_pathlist_head *, int, got_worktree_status_cb, void *,
173 struct got_repository *);
175 /*
176 * Revert a file at the specified path such that it matches its
177 * original state in the worktree's base commit.
178 */
179 const struct got_error *got_worktree_revert(struct got_worktree *,
180 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
181 struct got_repository *);
183 /*
184 * A callback function which is invoked when a commit message is requested.
185 * Passes a pathlist with a struct got_commitable * in the data pointer of
186 * each element, a pointer to the log message that must be set by the
187 * callback and will be freed after committing, and an argument passed
188 * through to the callback.
189 */
190 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
191 struct got_pathlist_head *, char **, void *);
193 /*
194 * Create a new commit from changes in the work tree.
195 * Return the ID of the newly created commit.
196 * The worktree's base commit will be set to this new commit.
197 * Files unaffected by this commit operation will retain their
198 * current base commit.
199 * An author and a non-empty log message must be specified.
200 * The name of the committer is optional (may be NULL).
201 * If an on-disk path is specified, only commit changes at or within this path.
202 */
203 const struct got_error *got_worktree_commit(struct got_object_id **,
204 struct got_worktree *, const char *, const char *, const char *,
205 got_worktree_commit_msg_cb, void *,
206 got_worktree_status_cb, void *, struct got_repository *);
208 /* Get the path of a commitable worktree item. */
209 const char *got_commitable_get_path(struct got_commitable *);
211 /* Get the status of a commitable worktree item. */
212 unsigned int got_commitable_get_status(struct got_commitable *);
214 /*
215 * Prepare for rebasing a branch onto the work tree's current branch.
216 * This function creates references to a temporary branch, the branch
217 * being rebased, and the work tree's current branch, under the
218 * "got/worktree/rebase/" namespace. These references are used to
219 * keep track of rebase operation state and are used as input and/or
220 * output arguments with other rebase-related functions.
221 */
222 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
223 struct got_reference **, struct got_worktree *, struct got_reference *,
224 struct got_repository *);
226 /*
227 * Continue an interrupted rebase operation.
228 * This function returns existing references created when rebase was prepared,
229 * and the ID of the commit currently being rebased. This should be called
230 * before either resuming or aborting a rebase operation.
231 */
232 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
233 struct got_reference **, struct got_reference **, struct got_reference **,
234 struct got_worktree *, struct got_repository *);
236 /* Check whether a, potentially interrupted, rebase operation is in progress. */
237 const struct got_error *got_worktree_rebase_in_progress(int *,
238 struct got_worktree *);
240 /*
241 * Merge changes from the commit currently being rebased into the work tree.
242 * Report affected files, including merge conflicts, via the specified
243 * progress callback. Also populate a list of affected paths which should
244 * be passed to got_worktree_rebase_commit() after a conflict-free merge.
245 * This list must be initialized with TAILQ_INIT() and disposed of with
246 * got_worktree_rebase_pathlist_free().
247 */
248 const struct got_error *got_worktree_rebase_merge_files(
249 struct got_pathlist_head *, struct got_worktree *,
250 struct got_object_id *, struct got_object_id *, struct got_repository *,
251 got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
253 /*
254 * Commit changes merged by got_worktree_rebase_merge_files() to a temporary
255 * branch and return the ID of the newly created commit. An optional list of
256 * merged paths can be provided; otherwise this function will perform a status
257 * crawl across the entire work tree to find paths to commit.
258 */
259 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
260 struct got_pathlist_head *, struct got_worktree *,
261 struct got_reference *, struct got_commit_object *,
262 struct got_object_id *, struct got_repository *);
264 /* Free a list of merged paths from got_worktree_merge_files. */
265 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
267 /* Postpone the rebase operation. Should be called after a merge conflict. */
268 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *);
270 /*
271 * Complete the current rebase operation. This should be called once all
272 * commits have been rebased successfully.
273 */
274 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
275 struct got_reference *, struct got_reference *, struct got_reference *,
276 struct got_repository *);
278 /*
279 * Abort the current rebase operation.
280 * Report reverted files via the specified progress callback.
281 */
282 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
283 struct got_repository *, struct got_reference *,
284 got_worktree_checkout_cb, void *);