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;
20 /* status codes */
21 #define GOT_STATUS_NO_CHANGE ' '
22 #define GOT_STATUS_ADD 'A'
23 #define GOT_STATUS_EXISTS 'E'
24 #define GOT_STATUS_UPDATE 'U'
25 #define GOT_STATUS_DELETE 'D'
26 #define GOT_STATUS_MODIFY 'M'
27 #define GOT_STATUS_CONFLICT 'C'
28 #define GOT_STATUS_MERGE 'G'
29 #define GOT_STATUS_MISSING '!'
30 #define GOT_STATUS_UNVERSIONED '?'
31 #define GOT_STATUS_OBSTRUCTED '~'
32 #define GOT_STATUS_REVERT 'R'
33 #define GOT_STATUS_CANNOT_DELETE 'd'
34 #define GOT_STATUS_BUMP_BASE 'b'
36 /*
37 * Attempt to initialize a new work tree on disk.
38 * The first argument is the path to a directory where the work tree
39 * will be created. The path itself must not yet exist, but the dirname(3)
40 * of the path must already exist.
41 * The reference provided will be used to determine the new worktree's
42 * base commit. The third argument speficies the work tree's path prefix.
43 */
44 const struct got_error *got_worktree_init(const char *, struct got_reference *,
45 const char *, struct got_repository *);
47 /*
48 * Attempt to open a worktree at or above the specified path.
49 * The caller must dispose of it with got_worktree_close().
50 */
51 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
53 /* Dispose of an open work tree. */
54 const struct got_error *got_worktree_close(struct got_worktree *);
56 /*
57 * Get the path to the root directory of a worktree.
58 */
59 const char *got_worktree_get_root_path(struct got_worktree *);
61 /*
62 * Get the path to the repository associated with a worktree.
63 */
64 const char *got_worktree_get_repo_path(struct got_worktree *);
66 /*
67 * Get the path prefix associated with a worktree.
68 */
69 const char *got_worktree_get_path_prefix(struct got_worktree *);
71 /*
72 * Check if a user-provided path prefix matches that of the worktree.
73 */
74 const struct got_error *got_worktree_match_path_prefix(int *,
75 struct got_worktree *, const char *);
77 /*
78 * Get the name of a work tree's HEAD reference.
79 */
80 const char *got_worktree_get_head_ref_name(struct got_worktree *);
82 /*
83 * Set the branch head reference of the work tree.
84 */
85 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
86 struct got_reference *);
88 /*
89 * Get the current base commit ID of a worktree.
90 */
91 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
93 /*
94 * Set the base commit Id of a worktree.
95 */
96 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
97 struct got_repository *, struct got_object_id *);
99 /* A callback function which is invoked when a path is checked out. */
100 typedef void (*got_worktree_checkout_cb)(void *, unsigned char, const char *);
102 /* A callback function which is invoked at cancellation points.
103 * May return GOT_ERR_CANCELLED to abort the runing operation. */
104 typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
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 a specific path in
115 * the work tree, in which case all files outside this path 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 * The specified path is relative to the work tree's root. Pass "" to check
121 * 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 const char *, struct got_repository *, got_worktree_checkout_cb, void *,
128 got_worktree_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_worktree_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, const char *, struct got_object_id *,
140 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 const char *, struct got_repository *, got_worktree_status_cb, void *,
149 got_worktree_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 /*
174 * Revert a file at the specified path such that it matches its
175 * original state in the worktree's base commit.
176 */
177 const struct got_error *got_worktree_revert(struct got_worktree *,
178 struct got_pathlist_head *, got_worktree_checkout_cb, void *,
179 struct got_repository *);
181 /*
182 * A callback function which is invoked when a commit message is requested.
183 * Passes a pathlist with a struct got_commitable * in the data pointer of
184 * each element, a pointer to the log message that must be set by the
185 * callback and will be freed after committing, and an argument passed
186 * through to the callback.
187 */
188 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
189 struct got_pathlist_head *, char **, void *);
191 /*
192 * Create a new commit from changes in the work tree.
193 * Return the ID of the newly created commit.
194 * The worktree's base commit will be set to this new commit.
195 * Files unaffected by this commit operation will retain their
196 * current base commit.
197 * An author and a non-empty log message must be specified.
198 * The name of the committer is optional (may be NULL).
199 * If an on-disk path is specified, only commit changes at or within this path.
200 */
201 const struct got_error *got_worktree_commit(struct got_object_id **,
202 struct got_worktree *, const char *, const char *, const char *,
203 got_worktree_commit_msg_cb, void *,
204 got_worktree_status_cb, void *, struct got_repository *);
206 /* Get the path of a commitable worktree item. */
207 const char *got_commitable_get_path(struct got_commitable *);
209 /* Get the status of a commitable worktree item. */
210 unsigned int got_commitable_get_status(struct got_commitable *);