Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <string.h>
20 86c3caaf 2018-03-09 stsp #include <stdio.h>
21 86c3caaf 2018-03-09 stsp #include <stdlib.h>
22 86c3caaf 2018-03-09 stsp #include <fcntl.h>
23 86c3caaf 2018-03-09 stsp #include <errno.h>
24 86c3caaf 2018-03-09 stsp #include <unistd.h>
25 86c3caaf 2018-03-09 stsp
26 86c3caaf 2018-03-09 stsp #include "got_error.h"
27 86c3caaf 2018-03-09 stsp #include "got_repository.h"
28 86c3caaf 2018-03-09 stsp #include "got_refs.h"
29 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
30 86c3caaf 2018-03-09 stsp
31 86c3caaf 2018-03-09 stsp #include "got_worktree_priv.h"
32 86c3caaf 2018-03-09 stsp #include "got_path_priv.h"
33 86c3caaf 2018-03-09 stsp
34 99724ed4 2018-03-10 stsp static const struct got_error *
35 99724ed4 2018-03-10 stsp create_meta_file(const char *gotpath, const char *name, const char *content)
36 99724ed4 2018-03-10 stsp {
37 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
38 99724ed4 2018-03-10 stsp char *path;
39 99724ed4 2018-03-10 stsp int fd = -1;
40 99724ed4 2018-03-10 stsp char buf[4];
41 99724ed4 2018-03-10 stsp ssize_t n;
42 99724ed4 2018-03-10 stsp
43 99724ed4 2018-03-10 stsp if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
44 99724ed4 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
45 99724ed4 2018-03-10 stsp path = NULL;
46 99724ed4 2018-03-10 stsp goto done;
47 99724ed4 2018-03-10 stsp }
48 99724ed4 2018-03-10 stsp
49 99724ed4 2018-03-10 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
50 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
51 99724ed4 2018-03-10 stsp if (fd == -1) {
52 99724ed4 2018-03-10 stsp err = got_error_from_errno();
53 99724ed4 2018-03-10 stsp goto done;
54 99724ed4 2018-03-10 stsp }
55 99724ed4 2018-03-10 stsp
56 99724ed4 2018-03-10 stsp /* The file should be empty. */
57 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
58 99724ed4 2018-03-10 stsp if (n != 0) {
59 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
60 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
61 99724ed4 2018-03-10 stsp goto done;
62 99724ed4 2018-03-10 stsp }
63 99724ed4 2018-03-10 stsp
64 99724ed4 2018-03-10 stsp if (content) {
65 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
66 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
67 99724ed4 2018-03-10 stsp err = got_error_from_errno();
68 99724ed4 2018-03-10 stsp goto done;
69 99724ed4 2018-03-10 stsp }
70 99724ed4 2018-03-10 stsp }
71 99724ed4 2018-03-10 stsp
72 99724ed4 2018-03-10 stsp done:
73 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
74 99724ed4 2018-03-10 stsp err = got_error_from_errno();
75 99724ed4 2018-03-10 stsp free(path);
76 99724ed4 2018-03-10 stsp return err;
77 99724ed4 2018-03-10 stsp }
78 99724ed4 2018-03-10 stsp
79 86c3caaf 2018-03-09 stsp const struct got_error *
80 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
81 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
82 86c3caaf 2018-03-09 stsp {
83 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
84 86c3caaf 2018-03-09 stsp char *abspath = NULL;
85 86c3caaf 2018-03-09 stsp char *normpath = NULL;
86 86c3caaf 2018-03-09 stsp char *gotpath = NULL;
87 86c3caaf 2018-03-09 stsp char *refstr = NULL;
88 86c3caaf 2018-03-09 stsp char *path_repos = NULL;
89 1451e70d 2018-03-10 stsp char *formatstr = NULL;
90 86c3caaf 2018-03-09 stsp
91 577ec78f 2018-03-11 stsp if (!got_path_is_absolute(prefix))
92 577ec78f 2018-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
93 577ec78f 2018-03-11 stsp
94 86c3caaf 2018-03-09 stsp if (got_path_is_absolute(path)) {
95 86c3caaf 2018-03-09 stsp abspath = strdup(path);
96 86c3caaf 2018-03-09 stsp if (abspath == NULL)
97 86c3caaf 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
98 86c3caaf 2018-03-09 stsp } else {
99 86c3caaf 2018-03-09 stsp abspath = got_path_get_absolute(path);
100 86c3caaf 2018-03-09 stsp if (abspath == NULL)
101 86c3caaf 2018-03-09 stsp return got_error(GOT_ERR_BAD_PATH);
102 86c3caaf 2018-03-09 stsp }
103 86c3caaf 2018-03-09 stsp
104 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
105 86c3caaf 2018-03-09 stsp normpath = got_path_normalize(abspath);
106 86c3caaf 2018-03-09 stsp if (normpath == NULL) {
107 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_BAD_PATH);
108 86c3caaf 2018-03-09 stsp goto done;
109 86c3caaf 2018-03-09 stsp }
110 86c3caaf 2018-03-09 stsp if (mkdir(normpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
111 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
112 86c3caaf 2018-03-09 stsp goto done;
113 86c3caaf 2018-03-09 stsp }
114 86c3caaf 2018-03-09 stsp
115 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
116 86c3caaf 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", normpath, GOT_WORKTREE_GOT_DIR) == -1) {
117 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
118 86c3caaf 2018-03-09 stsp goto done;
119 86c3caaf 2018-03-09 stsp }
120 86c3caaf 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
121 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
122 86c3caaf 2018-03-09 stsp goto done;
123 86c3caaf 2018-03-09 stsp }
124 86c3caaf 2018-03-09 stsp
125 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
126 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
127 99724ed4 2018-03-10 stsp if (err)
128 86c3caaf 2018-03-09 stsp goto done;
129 86c3caaf 2018-03-09 stsp
130 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
131 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
132 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
133 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
134 86c3caaf 2018-03-09 stsp goto done;
135 86c3caaf 2018-03-09 stsp }
136 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
137 99724ed4 2018-03-10 stsp if (err)
138 86c3caaf 2018-03-09 stsp goto done;
139 86c3caaf 2018-03-09 stsp
140 1451e70d 2018-03-10 stsp /* Store path to repository. */
141 86c3caaf 2018-03-09 stsp path_repos = got_repo_get_path(repo);
142 86c3caaf 2018-03-09 stsp if (path_repos == NULL) {
143 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
144 86c3caaf 2018-03-09 stsp goto done;
145 86c3caaf 2018-03-09 stsp }
146 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
147 99724ed4 2018-03-10 stsp if (err)
148 86c3caaf 2018-03-09 stsp goto done;
149 86c3caaf 2018-03-09 stsp
150 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
151 577ec78f 2018-03-11 stsp err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
152 577ec78f 2018-03-11 stsp if (err)
153 577ec78f 2018-03-11 stsp goto done;
154 577ec78f 2018-03-11 stsp
155 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
156 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
157 1451e70d 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
158 1451e70d 2018-03-10 stsp goto done;
159 1451e70d 2018-03-10 stsp }
160 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
161 99724ed4 2018-03-10 stsp if (err)
162 1451e70d 2018-03-10 stsp goto done;
163 1451e70d 2018-03-10 stsp
164 86c3caaf 2018-03-09 stsp done:
165 86c3caaf 2018-03-09 stsp free(abspath);
166 86c3caaf 2018-03-09 stsp free(normpath);
167 86c3caaf 2018-03-09 stsp free(gotpath);
168 1451e70d 2018-03-10 stsp free(formatstr);
169 86c3caaf 2018-03-09 stsp free(refstr);
170 86c3caaf 2018-03-09 stsp free(path_repos);
171 86c3caaf 2018-03-09 stsp return err;
172 86c3caaf 2018-03-09 stsp }
173 86c3caaf 2018-03-09 stsp
174 86c3caaf 2018-03-09 stsp const struct got_error *
175 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
176 86c3caaf 2018-03-09 stsp {
177 86c3caaf 2018-03-09 stsp return NULL;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp void
181 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
182 86c3caaf 2018-03-09 stsp {
183 86c3caaf 2018-03-09 stsp }
184 86c3caaf 2018-03-09 stsp
185 86c3caaf 2018-03-09 stsp char *
186 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
187 86c3caaf 2018-03-09 stsp {
188 86c3caaf 2018-03-09 stsp return strdup(worktree->path_repo);
189 86c3caaf 2018-03-09 stsp }
190 86c3caaf 2018-03-09 stsp
191 86c3caaf 2018-03-09 stsp struct got_reference *
192 86c3caaf 2018-03-09 stsp got_worktree_get_head(struct got_worktree *worktree)
193 86c3caaf 2018-03-09 stsp {
194 86c3caaf 2018-03-09 stsp return NULL;
195 86c3caaf 2018-03-09 stsp }
196 86c3caaf 2018-03-09 stsp
197 86c3caaf 2018-03-09 stsp const struct got_error *
198 86c3caaf 2018-03-09 stsp got_worktree_set_head(struct got_worktree *worktree, struct got_reference *head,
199 86c3caaf 2018-03-09 stsp struct got_repository *repo)
200 86c3caaf 2018-03-09 stsp {
201 86c3caaf 2018-03-09 stsp return NULL;
202 86c3caaf 2018-03-09 stsp }
203 86c3caaf 2018-03-09 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_checkout_files(struct got_worktree *worktree,
206 86c3caaf 2018-03-09 stsp struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp return NULL;
209 86c3caaf 2018-03-09 stsp }