2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
26 #include "got_error.h"
27 #include "got_repository.h"
29 #include "got_worktree.h"
31 #include "got_worktree_priv.h"
32 #include "got_path_priv.h"
34 static const struct got_error *
35 create_meta_file(const char *gotpath, const char *name, const char *content)
37 const struct got_error *err = NULL;
43 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
44 err = got_error(GOT_ERR_NO_MEM);
49 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
50 GOT_DEFAULT_FILE_MODE);
52 err = got_error_from_errno();
56 /* The file should be empty. */
57 n = read(fd, buf, sizeof(buf));
59 err = (n == -1 ? got_error_from_errno() :
60 got_error(GOT_ERR_WORKTREE_EXISTS));
65 int len = dprintf(fd, "%s\n", content);
66 if (len != strlen(content) + 1) {
67 err = got_error_from_errno();
73 if (fd != -1 && close(fd) == -1 && err == NULL)
74 err = got_error_from_errno();
79 const struct got_error *
80 got_worktree_init(const char *path, struct got_reference *head_ref,
81 struct got_repository *repo)
83 const struct got_error *err = NULL;
85 char *normpath = NULL;
88 char *path_repos = NULL;
89 char *formatstr = NULL;
91 if (got_path_is_absolute(path)) {
92 abspath = strdup(path);
94 return got_error(GOT_ERR_NO_MEM);
96 abspath = got_path_get_absolute(path);
98 return got_error(GOT_ERR_BAD_PATH);
101 /* Create top-level directory (may already exist). */
102 normpath = got_path_normalize(abspath);
103 if (normpath == NULL) {
104 err = got_error(GOT_ERR_BAD_PATH);
107 if (mkdir(normpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
108 err = got_error_from_errno();
112 /* Create .got directory (may already exist). */
113 if (asprintf(&gotpath, "%s/%s", normpath, GOT_WORKTREE_GOT_DIR) == -1) {
114 err = got_error(GOT_ERR_NO_MEM);
117 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
118 err = got_error_from_errno();
122 /* Create an empty file index. */
123 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
127 /* Write the HEAD reference. */
128 refstr = got_ref_to_str(head_ref);
129 if (refstr == NULL) {
130 err = got_error(GOT_ERR_NO_MEM);
133 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
137 /* Store path to repository. */
138 path_repos = got_repo_get_path(repo);
139 if (path_repos == NULL) {
140 err = got_error(GOT_ERR_NO_MEM);
143 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
147 /* Stamp repository with format file. */
148 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
149 err = got_error(GOT_ERR_NO_MEM);
152 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
166 const struct got_error *
167 got_worktree_open(struct got_worktree **worktree, const char *path)
173 got_worktree_close(struct got_worktree *worktree)
178 got_worktree_get_repo_path(struct got_worktree *worktree)
180 return strdup(worktree->path_repo);
183 struct got_reference *
184 got_worktree_get_head(struct got_worktree *worktree)
189 const struct got_error *
190 got_worktree_set_head(struct got_worktree *worktree, struct got_reference *head,
191 struct got_repository *repo)
196 const struct got_error *
197 got_worktree_update_fileindex(struct got_worktree *worktree,
198 struct got_repository *repo)
203 const struct got_error *
204 got_worktree_checkout_files(struct got_worktree *worktree,
205 struct got_repository *repo)