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;
19 /* status codes */
20 #define GOT_STATUS_NO_CHANGE ' '
21 #define GOT_STATUS_ADD 'A'
22 #define GOT_STATUS_EXISTS 'E'
23 #define GOT_STATUS_UPDATE 'U'
24 #define GOT_STATUS_DELETE 'D'
25 #define GOT_STATUS_MODIFY 'M'
26 #define GOT_STATUS_CONFLICT 'C'
27 #define GOT_STATUS_MERGE 'G'
28 #define GOT_STATUS_MISSING '!'
29 #define GOT_STATUS_UNVERSIONED '?'
30 #define GOT_STATUS_OBSTRUCTED '~'
31 #define GOT_STATUS_REVERT 'R'
33 struct got_commitable {
34 char *path;
35 char *in_repo_path;
36 char *ondisk_path;
37 unsigned char status;
38 struct got_object_id *blob_id;
39 struct got_object_id *base_blob_id;
40 struct got_object_id *base_commit_id;
41 mode_t mode;
42 };
44 /*
45 * Attempt to initialize a new work tree on disk.
46 * The first argument is the path to a directory where the work tree
47 * will be created. The path itself must not yet exist, but the dirname(3)
48 * of the path must already exist.
49 * The reference provided will be used to determine the new worktree's
50 * base commit. The third argument speficies the work tree's path prefix.
51 */
52 const struct got_error *got_worktree_init(const char *, struct got_reference *,
53 const char *, struct got_repository *);
55 /*
56 * Attempt to open a worktree at or above the specified path.
57 * The caller must dispose of it with got_worktree_close().
58 */
59 const struct got_error *got_worktree_open(struct got_worktree **, const char *);
61 /* Dispose of an open work tree. */
62 const struct got_error *got_worktree_close(struct got_worktree *);
64 /*
65 * Get the path to the root directory of a worktree.
66 */
67 const char *got_worktree_get_root_path(struct got_worktree *);
69 /*
70 * Get the path to the repository associated with a worktree.
71 */
72 const char *got_worktree_get_repo_path(struct got_worktree *);
74 /*
75 * Get the path prefix associated with a worktree.
76 */
77 const char *got_worktree_get_path_prefix(struct got_worktree *);
79 /*
80 * Check if a user-provided path prefix matches that of the worktree.
81 */
82 const struct got_error *got_worktree_match_path_prefix(int *,
83 struct got_worktree *, const char *);
85 /*
86 * Get the name of a work tree's HEAD reference.
87 */
88 const char *got_worktree_get_head_ref_name(struct got_worktree *);
90 /*
91 * Set the branch head reference of the work tree.
92 */
93 const struct got_error *got_worktree_set_head_ref(struct got_worktree *,
94 struct got_reference *);
96 /*
97 * Get the current base commit ID of a worktree.
98 */
99 struct got_object_id *got_worktree_get_base_commit_id(struct got_worktree *);
101 /*
102 * Set the base commit Id of a worktree.
103 */
104 const struct got_error *got_worktree_set_base_commit_id(struct got_worktree *,
105 struct got_repository *, struct got_object_id *);
107 /* A callback function which is invoked when a path is checked out. */
108 typedef void (*got_worktree_checkout_cb)(void *, unsigned char, const char *);
110 /* A callback function which is invoked at cancellation points.
111 * May return GOT_ERR_CANCELLED to abort the runing operation. */
112 typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
114 /*
115 * Attempt to check out files into a work tree from its associated repository
116 * and path prefix, and update the work tree's file index accordingly.
117 * File content is obtained from blobs within the work tree's path prefix
118 * inside the tree corresponding to the work tree's base commit.
119 * The checkout progress callback will be invoked with the provided
120 * void * argument, and the path of each checked out file.
122 * It is possible to restrict the checkout operation to a specific path in
123 * the work tree, in which case all files outside this path will remain at
124 * their currently recorded base commit. Inconsistent base commits can be
125 * repaired later by running another update operation across the entire work
126 * tree. Inconsistent base-commits may also occur if this function runs into
127 * an error or if the checkout operation is cancelled by the cancel callback.
128 * The specified path is relative to the work tree's root. Pass "" to check
129 * out files across the entire work tree.
131 * Some operations may refuse to run while the work tree contains files from
132 * multiple base commits.
133 */
134 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
135 const char *, 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 a file from disk and schedule it 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 *, const char *, int,
171 got_worktree_status_cb, void *, 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 const char *, got_worktree_checkout_cb, void *, struct got_repository *);
180 /*
181 * A callback function which is invoked when a commit message is requested.
182 * Passes a list of modified paths being committed to, a pointer to the log
183 * message that must be set by the callback and will be freed after committing,
184 * and an argument passed through to the callback.
185 */
186 typedef const struct got_error *(*got_worktree_commit_msg_cb)(
187 struct got_pathlist_head *, char **, void *);
189 /*
190 * Create a new commit from changes in the work tree.
191 * Return the ID of the newly created commit.
192 * The worktree's base commit will be set to this new commit.
193 * Files unaffected by this commit operation will retain their
194 * current base commit.
195 * An author and a non-empty log message must be specified.
196 * The name of the committer is optional (may be NULL).
197 * If an on-disk path is specified, only commit changes at or within this path.
198 */
199 const struct got_error *got_worktree_commit(struct got_object_id **,
200 struct got_worktree *, const char *, const char *, const char *,
201 got_worktree_commit_msg_cb, void *,
202 got_worktree_status_cb, void *, struct got_repository *);