2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5261c201 2018-04-01 stsp * Permission to use, copy, modify, and distribute this software for any
5 5261c201 2018-04-01 stsp * purpose with or without fee is hereby granted, provided that the above
6 5261c201 2018-04-01 stsp * copyright notice and this permission notice appear in all copies.
8 5261c201 2018-04-01 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5261c201 2018-04-01 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5261c201 2018-04-01 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5261c201 2018-04-01 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5261c201 2018-04-01 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5261c201 2018-04-01 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5261c201 2018-04-01 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
19 9e672c74 2019-03-11 stsp #include <sys/stat.h>
21 0fd469ce 2019-03-11 stsp #include <errno.h>
22 f77a24b0 2019-03-11 stsp #include <ctype.h>
23 a04f49d2 2019-02-04 stsp #include <dirent.h>
24 56e0773d 2019-11-28 stsp #include <limits.h>
25 5261c201 2018-04-01 stsp #include <sha1.h>
26 5261c201 2018-04-01 stsp #include <stdio.h>
27 5261c201 2018-04-01 stsp #include <stdlib.h>
28 5261c201 2018-04-01 stsp #include <string.h>
29 5261c201 2018-04-01 stsp #include <util.h>
30 5261c201 2018-04-01 stsp #include <zlib.h>
31 788c352e 2018-06-16 stsp #include <time.h>
32 0cd1c46a 2019-03-11 stsp #include <libgen.h>
34 5261c201 2018-04-01 stsp #include "got_error.h"
35 5261c201 2018-04-01 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
39 324d37e7 2019-05-11 stsp #include "got_path.h"
41 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
42 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 5261c201 2018-04-01 stsp #include "got_lib_object.h"
45 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
47 fb79db15 2019-02-01 stsp #ifndef nitems
48 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
52 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
53 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
56 598a8b91 2019-03-15 stsp * We do not resolve tags yet, and don't yet care about sorting refs either,
57 598a8b91 2019-03-15 stsp * so packed-refs files we write contain a minimal header which disables all
58 598a8b91 2019-03-15 stsp * packed-refs "traits" supported by Git.
60 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
62 5261c201 2018-04-01 stsp /* A symbolic reference. */
63 5261c201 2018-04-01 stsp struct got_symref {
68 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
70 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
71 5261c201 2018-04-01 stsp struct got_ref {
73 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
76 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
77 5261c201 2018-04-01 stsp struct got_reference {
78 5261c201 2018-04-01 stsp unsigned int flags;
79 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
80 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
83 5261c201 2018-04-01 stsp struct got_ref ref;
84 5261c201 2018-04-01 stsp struct got_symref symref;
87 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
90 5261c201 2018-04-01 stsp static const struct got_error *
91 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
92 f9267c9a 2019-03-15 stsp struct got_object_id *id, int flags)
94 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
96 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
97 f9267c9a 2019-03-15 stsp if (*ref == NULL)
98 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
100 80d5fc1f 2019-03-15 stsp memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
101 c980e470 2019-03-15 stsp (*ref)->flags = flags;
102 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
103 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
105 c980e470 2019-03-15 stsp got_ref_close(*ref);
106 f9267c9a 2019-03-15 stsp *ref = NULL;
108 f9267c9a 2019-03-15 stsp return err;
111 f9267c9a 2019-03-15 stsp static const struct got_error *
112 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
113 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
115 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
117 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
118 5261c201 2018-04-01 stsp if (*ref == NULL)
119 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
121 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
122 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
123 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
124 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
125 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
126 f9267c9a 2019-03-15 stsp *ref = NULL;
127 cdb8f1fa 2019-08-28 hiltjo return err;
129 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
130 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
132 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
133 f9267c9a 2019-03-15 stsp *ref = NULL;
135 f9267c9a 2019-03-15 stsp return err;
138 5261c201 2018-04-01 stsp static const struct got_error *
139 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
141 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
142 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
144 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
147 f9267c9a 2019-03-15 stsp static const struct got_error *
148 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
150 5892cdd6 2019-03-10 stsp struct got_object_id id;
152 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
154 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
157 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
158 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
160 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, 0);
163 5261c201 2018-04-01 stsp static const struct got_error *
164 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
165 2f17228e 2019-05-12 stsp const char *abspath, int lock)
167 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
169 c30018ad 2019-10-21 stsp char *line = NULL;
170 c30018ad 2019-10-21 stsp size_t linesize = 0;
171 c30018ad 2019-10-21 stsp ssize_t linelen;
172 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
174 2f17228e 2019-05-12 stsp if (lock) {
175 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, abspath);
177 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
178 9f142382 2020-03-21 stsp err = got_error(GOT_ERR_NOT_REF);
179 9f142382 2020-03-21 stsp return err;
183 c0a1c016 2019-03-15 stsp f = fopen(abspath, "rb");
184 f5c58ad1 2019-05-12 stsp if (f == NULL) {
186 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
187 1e37702e 2019-02-04 stsp return NULL;
190 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
191 c30018ad 2019-10-21 stsp if (linelen == -1) {
192 c30018ad 2019-10-21 stsp if (feof(f))
193 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
195 c30018ad 2019-10-21 stsp err = got_error_from_errno2("getline", abspath);
197 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
200 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
201 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
205 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
206 f5c58ad1 2019-05-12 stsp if (lock) {
208 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
211 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
213 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
217 5261c201 2018-04-01 stsp free(line);
218 2f17228e 2019-05-12 stsp if (fclose(f) != 0 && err == NULL) {
219 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
220 2f17228e 2019-05-12 stsp if (*ref) {
222 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
223 2f17228e 2019-05-12 stsp got_ref_close(*ref);
224 2f17228e 2019-05-12 stsp *ref = NULL;
227 5261c201 2018-04-01 stsp return err;
231 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
233 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
234 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
235 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
236 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
239 c5f754cc 2019-02-01 stsp static char *
240 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
242 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
243 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
245 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
249 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
251 7fa81f88 2020-02-21 stsp const char *s, *seg;
252 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
253 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
254 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
255 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
258 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
263 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
265 f77a24b0 2019-03-11 stsp while (*s) {
266 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
267 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
270 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
271 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
272 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
275 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
277 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
279 f77a24b0 2019-03-11 stsp if (*s == '/') {
280 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
281 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
282 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
284 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
285 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
287 f77a24b0 2019-03-11 stsp seg = nextseg;
292 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
293 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
299 5892cdd6 2019-03-10 stsp const struct got_error *
300 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
301 5892cdd6 2019-03-10 stsp struct got_object_id *id)
303 0f148cb7 2019-05-13 stsp if (!is_valid_ref_name(name))
304 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
306 0f148cb7 2019-05-13 stsp return alloc_ref(ref, name, id, 0);
309 aaf88317 2019-07-10 stsp const struct got_error *
310 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
311 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
313 aaf88317 2019-07-10 stsp if (!is_valid_ref_name(name))
314 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
319 d1f2edc9 2018-06-13 stsp static const struct got_error *
320 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
321 fb79db15 2019-02-01 stsp const char *line)
323 5892cdd6 2019-03-10 stsp struct got_object_id id;
324 5892cdd6 2019-03-10 stsp const char *name;
326 fb79db15 2019-02-01 stsp *ref = NULL;
328 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
329 fb79db15 2019-02-01 stsp return NULL;
331 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
332 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
334 199a4027 2019-02-02 stsp if (abs_refname) {
335 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
336 199a4027 2019-02-02 stsp return NULL;
337 5892cdd6 2019-03-10 stsp name = abs_refname;
339 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
341 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
344 fb79db15 2019-02-01 stsp static const struct got_error *
345 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
346 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
348 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
349 fb79db15 2019-02-01 stsp char *abs_refname;
350 fb79db15 2019-02-01 stsp char *line;
351 fb79db15 2019-02-01 stsp size_t len;
352 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
353 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
355 1e37702e 2019-02-04 stsp *ref = NULL;
357 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
358 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
360 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
361 7ab0422a 2019-03-15 stsp if (line == NULL) {
362 7ab0422a 2019-03-15 stsp if (feof(f))
364 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
367 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
368 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
369 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
370 0dec1cc0 2019-02-01 stsp refname) == -1)
371 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
372 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
373 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
374 0dec1cc0 2019-02-01 stsp free(abs_refname);
375 532920c8 2019-02-01 stsp if (err || *ref != NULL)
378 fb79db15 2019-02-01 stsp free(line);
381 fb79db15 2019-02-01 stsp } while (*ref == NULL);
383 fb79db15 2019-02-01 stsp return err;
386 fb79db15 2019-02-01 stsp static const struct got_error *
387 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
388 2f17228e 2019-05-12 stsp const char *name, int lock)
390 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
391 a04f49d2 2019-02-04 stsp char *path = NULL;
392 a04f49d2 2019-02-04 stsp char *absname = NULL;
393 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
394 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
396 1e37702e 2019-02-04 stsp *ref = NULL;
398 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
399 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
400 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
401 a04f49d2 2019-02-04 stsp absname = (char *)name;
403 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
404 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
405 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
407 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
408 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
409 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
414 6e472252 2019-07-22 stsp err = parse_ref_file(ref, absname, path, lock);
416 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
417 a04f49d2 2019-02-04 stsp free(absname);
418 a04f49d2 2019-02-04 stsp free(path);
419 d1f2edc9 2018-06-13 stsp return err;
422 5261c201 2018-04-01 stsp const struct got_error *
423 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 2f17228e 2019-05-12 stsp const char *refname, int lock)
426 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
427 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
428 fb79db15 2019-02-01 stsp const char *subdirs[] = {
429 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
431 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
432 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
434 1e37702e 2019-02-04 stsp *ref = NULL;
436 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
437 e135804e 2019-02-10 stsp if (path_refs == NULL) {
438 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
442 0885ce8f 2019-05-12 stsp if (well_known) {
443 0885ce8f 2019-05-12 stsp err = open_ref(ref, path_refs, "", refname, lock);
445 c5f754cc 2019-02-01 stsp char *packed_refs_path;
448 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
449 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
450 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
452 e135804e 2019-02-10 stsp if (err || *ref)
456 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
457 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
458 638f9024 2019-05-13 stsp err = got_error_from_errno(
459 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
463 2f17228e 2019-05-12 stsp if (lock) {
464 a875589a 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
468 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
469 c5f754cc 2019-02-01 stsp free(packed_refs_path);
470 c5f754cc 2019-02-01 stsp if (f != NULL) {
471 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
473 a875589a 2019-05-12 stsp if (!err) {
474 a875589a 2019-05-12 stsp if (fclose(f) != 0) {
475 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
476 a875589a 2019-05-12 stsp got_ref_close(*ref);
477 a875589a 2019-05-12 stsp *ref = NULL;
478 6e472abb 2019-05-13 stsp } else if (*ref)
479 a875589a 2019-05-12 stsp (*ref)->lf = lf;
484 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
485 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
486 a875589a 2019-05-12 stsp if (err && lf)
487 a875589a 2019-05-12 stsp got_lockfile_unlock(lf);
488 5261c201 2018-04-01 stsp free(path_refs);
489 5261c201 2018-04-01 stsp return err;
493 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
495 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
497 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
499 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
503 5261c201 2018-04-01 stsp struct got_reference *
504 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
506 5261c201 2018-04-01 stsp struct got_reference *ret;
508 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
509 5261c201 2018-04-01 stsp if (ret == NULL)
510 5261c201 2018-04-01 stsp return NULL;
512 5261c201 2018-04-01 stsp ret->flags = ref->flags;
513 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
515 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
517 5261c201 2018-04-01 stsp return NULL;
519 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
521 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
523 5261c201 2018-04-01 stsp return NULL;
526 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
527 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
529 5261c201 2018-04-01 stsp return NULL;
531 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 80d5fc1f 2019-03-15 stsp sizeof(ret->ref.ref.sha1));
535 5261c201 2018-04-01 stsp return ret;
538 b8bad2ba 2019-08-23 stsp const struct got_error *
539 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
540 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
542 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
543 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
545 b8bad2ba 2019-08-23 stsp *newp = NULL;
547 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
548 b8bad2ba 2019-08-23 stsp if (new == NULL)
549 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
551 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
552 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
553 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
555 b8bad2ba 2019-08-23 stsp return err;
558 b8bad2ba 2019-08-23 stsp new->id = got_object_id_dup(re->id);
559 b8bad2ba 2019-08-23 stsp if (new->id == NULL) {
560 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
561 b8bad2ba 2019-08-23 stsp free(new->id);
563 b8bad2ba 2019-08-23 stsp return err;
566 b8bad2ba 2019-08-23 stsp *newp = new;
567 b8bad2ba 2019-08-23 stsp return NULL;
570 5261c201 2018-04-01 stsp static const struct got_error *
571 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
572 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
574 5261c201 2018-04-01 stsp struct got_reference *nextref;
575 5261c201 2018-04-01 stsp const struct got_error *err;
577 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
579 5261c201 2018-04-01 stsp return err;
581 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
582 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
584 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
586 5261c201 2018-04-01 stsp got_ref_close(nextref);
587 5261c201 2018-04-01 stsp return err;
590 29e86f7a 2019-08-12 stsp static const struct got_error *
591 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
592 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
594 5261c201 2018-04-01 stsp const struct got_error *err;
596 29e86f7a 2019-08-12 stsp if (recursion <= 0)
597 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
598 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
600 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
601 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
602 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
603 5261c201 2018-04-01 stsp if (err == NULL)
604 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
605 57b6f99a 2019-04-13 stsp if (resolved)
606 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
607 5261c201 2018-04-01 stsp return err;
610 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
611 5261c201 2018-04-01 stsp if (*id == NULL)
612 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
613 80d5fc1f 2019-03-15 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
614 5261c201 2018-04-01 stsp return NULL;
617 29e86f7a 2019-08-12 stsp const struct got_error *
618 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 29e86f7a 2019-08-12 stsp struct got_reference *ref)
621 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
625 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
629 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
630 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
632 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
633 271d2a38 2018-12-25 stsp if (str == NULL)
634 271d2a38 2018-12-25 stsp return NULL;
636 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
637 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
639 271d2a38 2018-12-25 stsp return NULL;
642 5261c201 2018-04-01 stsp return str;
645 0bd18d37 2019-02-01 stsp const char *
646 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
648 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
649 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
651 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
654 aaf88317 2019-07-10 stsp const char *
655 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
657 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
660 b8bad2ba 2019-08-23 stsp return NULL;
663 b8bad2ba 2019-08-23 stsp const struct got_error *
664 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
665 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
667 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
668 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
670 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
671 aaf88317 2019-07-10 stsp return NULL;
674 d1f16636 2020-01-15 stsp const struct got_error *
675 d1f16636 2020-01-15 stsp got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
676 d1f16636 2020-01-15 stsp struct got_reference *ref2)
678 d1f16636 2020-01-15 stsp const struct got_error *err = NULL;
679 d1f16636 2020-01-15 stsp struct got_repository *repo = arg;
680 d1f16636 2020-01-15 stsp struct got_object_id *id1, *id2 = NULL;
681 d1f16636 2020-01-15 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
682 d1f16636 2020-01-15 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
683 d1f16636 2020-01-15 stsp time_t time1, time2;
687 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id1, repo, ref1);
689 d1f16636 2020-01-15 stsp return err;
690 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag1, repo, id1);
692 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
694 d1f16636 2020-01-15 stsp /* "lightweight" tag */
695 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit1, repo, id1);
698 d1f16636 2020-01-15 stsp time1 = got_object_commit_get_committer_time(commit1);
700 d1f16636 2020-01-15 stsp time1 = got_object_tag_get_tagger_time(tag1);
702 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id2, repo, ref2);
705 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag2, repo, id2);
707 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
709 d1f16636 2020-01-15 stsp /* "lightweight" tag */
710 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit2, repo, id2);
713 d1f16636 2020-01-15 stsp time2 = got_object_commit_get_committer_time(commit2);
715 d1f16636 2020-01-15 stsp time2 = got_object_tag_get_tagger_time(tag2);
717 d1f16636 2020-01-15 stsp /* Put latest tags first. */
718 d1f16636 2020-01-15 stsp if (time1 < time2)
720 d1f16636 2020-01-15 stsp else if (time1 > time2)
723 d1f16636 2020-01-15 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 d1f16636 2020-01-15 stsp got_object_tag_close(tag1);
730 d1f16636 2020-01-15 stsp got_object_tag_close(tag2);
731 d1f16636 2020-01-15 stsp if (commit1)
732 d1f16636 2020-01-15 stsp got_object_commit_close(commit1);
733 d1f16636 2020-01-15 stsp if (commit2)
734 d1f16636 2020-01-15 stsp got_object_commit_close(commit2);
735 d1f16636 2020-01-15 stsp return err;
738 199a4027 2019-02-02 stsp static const struct got_error *
739 505287be 2019-03-15 stsp insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
740 b8bad2ba 2019-08-23 stsp struct got_reference *ref, struct got_repository *repo,
741 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
743 199a4027 2019-02-02 stsp const struct got_error *err;
744 199a4027 2019-02-02 stsp struct got_object_id *id;
745 6249107b 2019-03-15 stsp struct got_reflist_entry *new, *re, *prev = NULL;
748 505287be 2019-03-15 stsp *newp = NULL;
750 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
752 7a3c76f5 2019-02-05 stsp return err;
754 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
755 7a3c76f5 2019-02-05 stsp if (new == NULL) {
757 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
759 7a3c76f5 2019-02-05 stsp new->ref = ref;
760 7a3c76f5 2019-02-05 stsp new->id = id;
761 505287be 2019-03-15 stsp *newp = new;
764 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
765 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
766 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
767 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
769 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
770 7a3c76f5 2019-02-05 stsp while (re) {
771 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
773 b8bad2ba 2019-08-23 stsp return err;
774 e397b6db 2019-02-04 stsp if (cmp == 0) {
775 27a1ed03 2019-03-15 stsp /* duplicate */
776 27a1ed03 2019-03-15 stsp free(new->id);
778 505287be 2019-03-15 stsp *newp = NULL;
779 7a3c76f5 2019-02-05 stsp return NULL;
780 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
782 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
784 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
785 7a3c76f5 2019-02-05 stsp return NULL;
788 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
792 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
793 199a4027 2019-02-02 stsp return NULL;
796 a04f49d2 2019-02-04 stsp static const struct got_error *
797 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
798 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
799 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
801 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
802 a04f49d2 2019-02-04 stsp DIR *d = NULL;
803 a04f49d2 2019-02-04 stsp char *path_subdir;
805 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
806 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
808 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
809 a04f49d2 2019-02-04 stsp if (d == NULL)
813 a04f49d2 2019-02-04 stsp struct dirent *dent;
814 a04f49d2 2019-02-04 stsp struct got_reference *ref;
815 a04f49d2 2019-02-04 stsp char *child;
817 a04f49d2 2019-02-04 stsp dent = readdir(d);
818 a04f49d2 2019-02-04 stsp if (dent == NULL)
821 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
822 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
825 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
826 a04f49d2 2019-02-04 stsp case DT_REG:
827 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
832 505287be 2019-03-15 stsp struct got_reflist_entry *new;
833 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
834 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
835 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
836 505287be 2019-03-15 stsp got_ref_close(ref);
841 a04f49d2 2019-02-04 stsp case DT_DIR:
842 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
843 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
844 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
847 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
848 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
849 a04f49d2 2019-02-04 stsp free(child);
857 a04f49d2 2019-02-04 stsp closedir(d);
858 a04f49d2 2019-02-04 stsp free(path_subdir);
859 a04f49d2 2019-02-04 stsp return err;
862 199a4027 2019-02-02 stsp const struct got_error *
863 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
864 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
866 199a4027 2019-02-02 stsp const struct got_error *err;
867 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
868 6aeab596 2019-08-28 stsp const char *ondisk_ref_namespace = NULL;
869 29b5c214 2019-02-04 stsp FILE *f = NULL;
870 199a4027 2019-02-02 stsp struct got_reference *ref;
871 505287be 2019-03-15 stsp struct got_reflist_entry *new;
873 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
874 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
875 29606af7 2019-08-23 stsp if (path_refs == NULL) {
876 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
879 29606af7 2019-08-23 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
882 f68a7890 2020-03-19 stsp err = insert_ref(&new, refs, ref, repo,
883 f68a7890 2020-03-19 stsp cmp_cb, cmp_arg);
884 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
885 29606af7 2019-08-23 stsp got_ref_close(ref);
886 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
890 6aeab596 2019-08-28 stsp ondisk_ref_namespace = ref_namespace;
891 29606af7 2019-08-23 stsp if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
892 6aeab596 2019-08-28 stsp ondisk_ref_namespace += 5;
894 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
895 29b5c214 2019-02-04 stsp free(path_refs);
896 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
897 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
898 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
901 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
902 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
903 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
908 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
909 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
911 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
912 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
913 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
917 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
918 199a4027 2019-02-02 stsp free(packed_refs_path);
920 199a4027 2019-02-02 stsp char *line;
921 199a4027 2019-02-02 stsp size_t len;
922 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
924 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
925 0bb4abae 2019-03-15 stsp if (line == NULL) {
926 0bb4abae 2019-03-15 stsp if (feof(f))
928 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
931 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
932 0bb4abae 2019-03-15 stsp free(line);
936 29606af7 2019-08-23 stsp if (ref_namespace) {
937 29606af7 2019-08-23 stsp const char *name;
938 29606af7 2019-08-23 stsp name = got_ref_get_name(ref);
939 29606af7 2019-08-23 stsp if (strncmp(name, ref_namespace,
940 29606af7 2019-08-23 stsp strlen(ref_namespace)) != 0) {
941 29606af7 2019-08-23 stsp got_ref_close(ref);
945 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
946 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
947 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
948 505287be 2019-03-15 stsp got_ref_close(ref);
955 a04f49d2 2019-02-04 stsp free(path_refs);
956 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
957 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
958 199a4027 2019-02-02 stsp return err;
962 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
964 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
966 e2e879a0 2019-03-11 stsp while (!SIMPLEQ_EMPTY(refs)) {
967 e2e879a0 2019-03-11 stsp re = SIMPLEQ_FIRST(refs);
968 e2e879a0 2019-03-11 stsp SIMPLEQ_REMOVE_HEAD(refs, entry);
969 e2e879a0 2019-03-11 stsp got_ref_close(re->ref);
970 e2e879a0 2019-03-11 stsp free(re->id);
977 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
979 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
982 b249b824 2019-05-09 stsp const struct got_error *
983 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
985 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
986 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
988 b249b824 2019-05-09 stsp memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
989 b249b824 2019-05-09 stsp return NULL;
992 9e672c74 2019-03-11 stsp const struct got_error *
993 b249b824 2019-05-09 stsp got_ref_change_symref(struct got_reference *ref, char *refname)
995 b249b824 2019-05-09 stsp char *new_name;
997 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
998 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1000 b249b824 2019-05-09 stsp new_name = strdup(refname);
1001 b249b824 2019-05-09 stsp if (new_name == NULL)
1002 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1004 b249b824 2019-05-09 stsp free(ref->ref.symref.name);
1005 b249b824 2019-05-09 stsp ref->ref.symref.name = new_name;
1006 b249b824 2019-05-09 stsp return NULL;
1009 b249b824 2019-05-09 stsp const struct got_error *
1010 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1011 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1013 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1014 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1016 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1017 e8a967e0 2020-03-21 stsp memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1018 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1019 e8a967e0 2020-03-21 stsp return NULL;
1022 e8a967e0 2020-03-21 stsp const struct got_error *
1023 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1025 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1026 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1027 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1028 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1029 9e672c74 2019-03-11 stsp FILE *f = NULL;
1031 9e672c74 2019-03-11 stsp struct stat sb;
1033 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1034 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1035 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1036 9e672c74 2019-03-11 stsp goto done;
1039 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1040 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1041 9e672c74 2019-03-11 stsp goto done;
1044 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1045 49c7094f 2019-03-11 stsp if (err) {
1046 d1667f0d 2019-03-11 stsp char *parent;
1047 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1048 5e1c9f23 2019-03-11 stsp goto done;
1049 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1051 0cd1c46a 2019-03-11 stsp goto done;
1052 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1053 5e1c9f23 2019-03-11 stsp free(parent);
1055 0cd1c46a 2019-03-11 stsp goto done;
1056 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1058 0cd1c46a 2019-03-11 stsp goto done;
1061 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1062 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1063 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1064 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1065 9e672c74 2019-03-11 stsp goto done;
1068 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1069 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1070 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
1071 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1072 9e672c74 2019-03-11 stsp goto done;
1074 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1075 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
1076 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1077 9e672c74 2019-03-11 stsp goto done;
1081 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1082 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1084 2f17228e 2019-05-12 stsp goto done;
1087 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1089 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1090 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1091 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1092 0cd1c46a 2019-03-11 stsp goto done;
1094 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1097 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1098 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1099 9e672c74 2019-03-11 stsp goto done;
1101 9e672c74 2019-03-11 stsp free(tmppath);
1102 9e672c74 2019-03-11 stsp tmppath = NULL;
1104 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
1105 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", path);
1106 9e672c74 2019-03-11 stsp goto done;
1109 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1110 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1112 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
1113 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1115 9e672c74 2019-03-11 stsp free(path_refs);
1116 9e672c74 2019-03-11 stsp free(path);
1117 9e672c74 2019-03-11 stsp if (tmppath) {
1118 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
1119 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1120 9e672c74 2019-03-11 stsp free(tmppath);
1122 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1125 598a8b91 2019-03-15 stsp static const struct got_error *
1126 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1128 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1129 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1130 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1131 598a8b91 2019-03-15 stsp char *packed_refs_path, *tmppath = NULL;
1132 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1133 598a8b91 2019-03-15 stsp int found_delref = 0;
1135 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1136 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1137 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1139 598a8b91 2019-03-15 stsp SIMPLEQ_INIT(&refs);
1141 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1142 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1143 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1145 598a8b91 2019-03-15 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1147 598a8b91 2019-03-15 stsp goto done;
1149 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1150 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
1152 2f17228e 2019-05-12 stsp goto done;
1155 598a8b91 2019-03-15 stsp f = fopen(packed_refs_path, "r");
1156 598a8b91 2019-03-15 stsp if (f == NULL) {
1157 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1158 598a8b91 2019-03-15 stsp goto done;
1161 598a8b91 2019-03-15 stsp char *line;
1162 598a8b91 2019-03-15 stsp size_t len;
1163 598a8b91 2019-03-15 stsp const char delim[3] = {'\0', '\0', '\0'};
1164 598a8b91 2019-03-15 stsp struct got_reference *ref;
1165 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1167 598a8b91 2019-03-15 stsp line = fparseln(f, &len, NULL, delim, 0);
1168 598a8b91 2019-03-15 stsp if (line == NULL) {
1169 598a8b91 2019-03-15 stsp if (feof(f))
1171 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1172 598a8b91 2019-03-15 stsp goto done;
1174 598a8b91 2019-03-15 stsp err = parse_packed_ref_line(&ref, NULL, line);
1175 598a8b91 2019-03-15 stsp free(line);
1177 598a8b91 2019-03-15 stsp goto done;
1178 598a8b91 2019-03-15 stsp if (ref == NULL)
1181 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1182 598a8b91 2019-03-15 stsp memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1183 598a8b91 2019-03-15 stsp sizeof(delref->ref.ref.sha1)) == 0) {
1184 598a8b91 2019-03-15 stsp found_delref = 1;
1185 598a8b91 2019-03-15 stsp got_ref_close(ref);
1189 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, &refs, ref, repo,
1190 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1191 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1192 598a8b91 2019-03-15 stsp got_ref_close(ref);
1194 598a8b91 2019-03-15 stsp goto done;
1197 598a8b91 2019-03-15 stsp if (found_delref) {
1198 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1200 598a8b91 2019-03-15 stsp struct stat sb;
1202 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1203 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1204 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1205 598a8b91 2019-03-15 stsp goto done;
1208 598a8b91 2019-03-15 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1209 598a8b91 2019-03-15 stsp uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1211 598a8b91 2019-03-15 stsp if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1212 598a8b91 2019-03-15 stsp sizeof(hex)) == NULL) {
1213 598a8b91 2019-03-15 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1214 598a8b91 2019-03-15 stsp goto done;
1216 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1217 598a8b91 2019-03-15 stsp if (n != sizeof(hex)) {
1218 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1219 598a8b91 2019-03-15 stsp goto done;
1221 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1222 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1223 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1224 598a8b91 2019-03-15 stsp goto done;
1228 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1230 598a8b91 2019-03-15 stsp goto done;
1233 598a8b91 2019-03-15 stsp if (stat(packed_refs_path, &sb) != 0) {
1234 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1235 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat",
1236 230a42bd 2019-05-11 jcs packed_refs_path);
1237 598a8b91 2019-03-15 stsp goto done;
1239 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1242 598a8b91 2019-03-15 stsp if (rename(tmppath, packed_refs_path) != 0) {
1243 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1244 230a42bd 2019-05-11 jcs packed_refs_path);
1245 598a8b91 2019-03-15 stsp goto done;
1248 598a8b91 2019-03-15 stsp if (chmod(packed_refs_path, sb.st_mode) != 0) {
1249 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod",
1250 230a42bd 2019-05-11 jcs packed_refs_path);
1251 598a8b91 2019-03-15 stsp goto done;
1255 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1256 598a8b91 2019-03-15 stsp unlock_err = got_lockfile_unlock(lf);
1258 598a8b91 2019-03-15 stsp if (fclose(f) != 0 && err == NULL)
1259 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1261 598a8b91 2019-03-15 stsp if (tmpf) {
1262 598a8b91 2019-03-15 stsp unlink(tmppath);
1263 598a8b91 2019-03-15 stsp if (fclose(tmpf) != 0 && err == NULL)
1264 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1266 598a8b91 2019-03-15 stsp free(tmppath);
1267 598a8b91 2019-03-15 stsp free(packed_refs_path);
1268 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1269 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1272 2d2e1378 2019-03-11 stsp const struct got_error *
1273 2d2e1378 2019-03-11 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1275 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1276 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1277 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1278 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1280 598a8b91 2019-03-15 stsp if (ref->flags & GOT_REF_IS_PACKED)
1281 598a8b91 2019-03-15 stsp return delete_packed_ref(ref, repo);
1283 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1284 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1285 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1286 2d2e1378 2019-03-11 stsp goto done;
1289 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1290 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1291 2d2e1378 2019-03-11 stsp goto done;
1294 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1295 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1297 2f17228e 2019-05-12 stsp goto done;
1300 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1302 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
1303 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1305 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1306 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1308 2d2e1378 2019-03-11 stsp free(path_refs);
1309 2d2e1378 2019-03-11 stsp free(path);
1310 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1313 2f17228e 2019-05-12 stsp const struct got_error *
1314 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1316 2f17228e 2019-05-12 stsp const struct got_error *err;
1317 2f17228e 2019-05-12 stsp err = got_lockfile_unlock(ref->lf);
1318 2f17228e 2019-05-12 stsp ref->lf = NULL;
1319 2f17228e 2019-05-12 stsp return err;