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 void (*got_worktree_checkout_cb)(void *, unsigned char, const char *);
103 /* A callback function which is invoked at cancellation points.
104 * May return GOT_ERR_CANCELLED to abort the runing operation. */
105 typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
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 a specific path in
116 * the work tree, in which case all files outside this path 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 * The specified path is relative to the work tree's root. Pass "" to check
122 * 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 const char *, struct got_repository *, got_worktree_checkout_cb, void *,
129 got_worktree_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_worktree_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, const char *, struct got_object_id *,
141 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 const char *, struct got_repository *, got_worktree_status_cb, void *,
150 got_worktree_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 /*
175 * Revert a file at the specified path such that it matches its
176 * original state in the worktree's base commit.
177 */
178 const struct got_error *got_worktree_revert(struct got_worktree *,
179 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
180 struct got_repository *);
182 /*
183 * A callback function which is invoked when a commit message is requested.
184 * Passes a pathlist with a struct got_commitable * in the data pointer of
185 * each element, a pointer to the log message that must be set by the
186 * callback and will be freed after committing, and an argument passed
187 * through to the callback.
188 */
189 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
190 struct got_pathlist_head *, char **, void *);
192 /*
193 * Create a new commit from changes in the work tree.
194 * Return the ID of the newly created commit.
195 * The worktree's base commit will be set to this new commit.
196 * Files unaffected by this commit operation will retain their
197 * current base commit.
198 * An author and a non-empty log message must be specified.
199 * The name of the committer is optional (may be NULL).
200 * If an on-disk path is specified, only commit changes at or within this path.
201 */
202 const struct got_error *got_worktree_commit(struct got_object_id **,
203 struct got_worktree *, const char *, const char *, const char *,
204 got_worktree_commit_msg_cb, void *,
205 got_worktree_status_cb, void *, struct got_repository *);
207 /* Get the path of a commitable worktree item. */
208 const char *got_commitable_get_path(struct got_commitable *);
210 /* Get the status of a commitable worktree item. */
211 unsigned int got_commitable_get_status(struct got_commitable *);
213 /*
214 * Prepare for rebasing a branch onto the work tree's current branch.
215 * This function creates references to a temporary branch, the branch
216 * being rebased, and the work tree's current branch, under the
217 * "got/worktree/rebase/" namespace. These references are used to
218 * keep track of rebase operation state and are used as input and/or
219 * output arguments with other rebase-related functions.
220 */
221 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
222 struct got_reference **, struct got_worktree *, struct got_reference *,
223 struct got_repository *);
225 /*
226 * Continue an interrupted rebase operation.
227 * This function returns existing references created when rebase was prepared,
228 * and the ID of the commit currently being rebased. This should be called
229 * before either resuming or aborting a rebase operation.
230 */
231 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
232 struct got_reference **, struct got_reference **, struct got_reference **,
233 struct got_worktree *, struct got_repository *);
235 /* Check whether a, potentially interrupted, rebase operation is in progress. */
236 const struct got_error *got_worktree_rebase_in_progress(int *,
237 struct got_worktree *);
239 /*
240 * Merge changes from the commit currently being rebased into the work tree.
241 * Report affected files, including merge conflicts, via the specified
242 * progress callback.
243 */
244 const struct got_error *got_worktree_rebase_merge_files(
245 struct got_worktree *, struct got_object_id *, struct got_object_id *,
246 struct got_repository *, got_worktree_checkout_cb, void *,
247 got_worktree_cancel_cb, void *);
249 /*
250 * Commit merged rebased changes to a temporary branch and return the
251 * ID of the newly created commit.
252 */
253 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
254 struct got_worktree *, struct got_reference *, struct got_commit_object *,
255 struct got_object_id *, struct got_repository *);
257 /* Postpone the rebase operation. Should be called after a merge conflict. */
258 const struct got_error *got_worktree_rebase_postpone(struct got_worktree *);
260 /*
261 * Complete the current rebase operation. This should be called once all
262 * commits have been rebased successfully.
263 */
264 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
265 struct got_reference *, struct got_reference *, struct got_reference *,
266 struct got_repository *);
268 /*
269 * Abort the current rebase operation.
270 * Report reverted files via the specified progress callback.
271 */
272 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
273 struct got_repository *, struct got_reference *,
274 got_worktree_checkout_cb, void *);