]>
Commit | Line | Data |
---|---|---|
d00d5464 ET |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
8 | #include "refs.h" | |
9 | #include "hash.h" | |
10 | #include "repository.h" | |
11 | #include "fileops.h" | |
114f5a6c | 12 | #include "filebuf.h" |
d00d5464 ET |
13 | #include "pack.h" |
14 | #include "reflog.h" | |
d00d5464 ET |
15 | #include "refdb.h" |
16 | #include "refdb_fs.h" | |
4def7035 | 17 | #include "iterator.h" |
fe372740 | 18 | #include "sortedcache.h" |
b976f3c2 | 19 | #include "signature.h" |
d00d5464 ET |
20 | |
21 | #include <git2/tag.h> | |
22 | #include <git2/object.h> | |
23 | #include <git2/refdb.h> | |
4dcd8780 | 24 | #include <git2/sys/refdb_backend.h> |
21ca0451 | 25 | #include <git2/sys/refs.h> |
b976f3c2 | 26 | #include <git2/sys/reflog.h> |
d00d5464 ET |
27 | |
28 | GIT__USE_STRMAP; | |
29 | ||
30 | #define DEFAULT_NESTING_LEVEL 5 | |
31 | #define MAX_NESTING_LEVEL 10 | |
32 | ||
33 | enum { | |
f69db390 VM |
34 | PACKREF_HAS_PEEL = 1, |
35 | PACKREF_WAS_LOOSE = 2, | |
2638a03a VM |
36 | PACKREF_CANNOT_PEEL = 4, |
37 | PACKREF_SHADOWED = 8, | |
f69db390 VM |
38 | }; |
39 | ||
40 | enum { | |
41 | PEELING_NONE = 0, | |
42 | PEELING_STANDARD, | |
43 | PEELING_FULL | |
d00d5464 ET |
44 | }; |
45 | ||
46 | struct packref { | |
47 | git_oid oid; | |
48 | git_oid peel; | |
49 | char flags; | |
50 | char name[GIT_FLEX_ARRAY]; | |
51 | }; | |
52 | ||
53 | typedef struct refdb_fs_backend { | |
54 | git_refdb_backend parent; | |
55 | ||
56 | git_repository *repo; | |
bade5194 | 57 | char *path; |
d00d5464 | 58 | |
fe372740 | 59 | git_sortedcache *refcache; |
f69db390 | 60 | int peeling_mode; |
219d3457 RB |
61 | git_iterator_flag_t iterator_flags; |
62 | uint32_t direach_flags; | |
d00d5464 ET |
63 | } refdb_fs_backend; |
64 | ||
fe372740 | 65 | static int packref_cmp(const void *a_, const void *b_) |
d00d5464 | 66 | { |
fe372740 RB |
67 | const struct packref *a = a_, *b = b_; |
68 | return strcmp(a->name, b->name); | |
d00d5464 ET |
69 | } |
70 | ||
fe372740 | 71 | static int packed_reload(refdb_fs_backend *backend) |
d00d5464 | 72 | { |
fe372740 RB |
73 | int error; |
74 | git_buf packedrefs = GIT_BUF_INIT; | |
75 | char *scan, *eof, *eol; | |
4dcd8780 | 76 | |
fe372740 | 77 | if (!backend->path) |
69a3c766 CMN |
78 | return 0; |
79 | ||
fe372740 | 80 | error = git_sortedcache_lockandload(backend->refcache, &packedrefs); |
d00d5464 ET |
81 | |
82 | /* | |
fe372740 RB |
83 | * If we can't find the packed-refs, clear table and return. |
84 | * Any other error just gets passed through. | |
85 | * If no error, and file wasn't changed, just return. | |
86 | * Anything else means we need to refresh the packed refs. | |
d00d5464 | 87 | */ |
fe372740 RB |
88 | if (error <= 0) { |
89 | if (error == GIT_ENOTFOUND) { | |
90 | git_sortedcache_clear(backend->refcache, true); | |
91 | giterr_clear(); | |
92 | error = 0; | |
93 | } | |
94 | return error; | |
d00d5464 ET |
95 | } |
96 | ||
fe372740 | 97 | /* At this point, refresh the packed refs from the loaded buffer. */ |
d00d5464 | 98 | |
fe372740 | 99 | git_sortedcache_clear(backend->refcache, false); |
d00d5464 | 100 | |
fe372740 RB |
101 | scan = (char *)packedrefs.ptr; |
102 | eof = scan + packedrefs.size; | |
d00d5464 | 103 | |
f69db390 VM |
104 | backend->peeling_mode = PEELING_NONE; |
105 | ||
fe372740 | 106 | if (*scan == '#') { |
1fed6b07 | 107 | static const char *traits_header = "# pack-refs with: "; |
f69db390 | 108 | |
fe372740 RB |
109 | if (git__prefixcmp(scan, traits_header) == 0) { |
110 | scan += strlen(traits_header); | |
111 | eol = strchr(scan, '\n'); | |
822645f6 | 112 | |
fe372740 | 113 | if (!eol) |
822645f6 | 114 | goto parse_failed; |
fe372740 | 115 | *eol = '\0'; |
822645f6 | 116 | |
fe372740 | 117 | if (strstr(scan, " fully-peeled ") != NULL) { |
f69db390 | 118 | backend->peeling_mode = PEELING_FULL; |
fe372740 | 119 | } else if (strstr(scan, " peeled ") != NULL) { |
f69db390 VM |
120 | backend->peeling_mode = PEELING_STANDARD; |
121 | } | |
122 | ||
fe372740 | 123 | scan = eol + 1; |
f69db390 VM |
124 | } |
125 | } | |
126 | ||
fe372740 RB |
127 | while (scan < eof && *scan == '#') { |
128 | if (!(eol = strchr(scan, '\n'))) | |
d00d5464 | 129 | goto parse_failed; |
fe372740 | 130 | scan = eol + 1; |
d00d5464 ET |
131 | } |
132 | ||
fe372740 RB |
133 | while (scan < eof) { |
134 | struct packref *ref; | |
135 | git_oid oid; | |
136 | ||
137 | /* parse "<OID> <refname>\n" */ | |
d00d5464 | 138 | |
fe372740 | 139 | if (git_oid_fromstr(&oid, scan) < 0) |
d00d5464 | 140 | goto parse_failed; |
fe372740 | 141 | scan += GIT_OID_HEXSZ; |
d00d5464 | 142 | |
fe372740 RB |
143 | if (*scan++ != ' ') |
144 | goto parse_failed; | |
145 | if (!(eol = strchr(scan, '\n'))) | |
146 | goto parse_failed; | |
147 | *eol = '\0'; | |
148 | if (eol[-1] == '\r') | |
149 | eol[-1] = '\0'; | |
d00d5464 | 150 | |
fe372740 | 151 | if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0) |
d00d5464 | 152 | goto parse_failed; |
fe372740 RB |
153 | scan = eol + 1; |
154 | ||
155 | git_oid_cpy(&ref->oid, &oid); | |
156 | ||
157 | /* look for optional "^<OID>\n" */ | |
158 | ||
159 | if (*scan == '^') { | |
160 | if (git_oid_fromstr(&oid, scan + 1) < 0) | |
161 | goto parse_failed; | |
162 | scan += GIT_OID_HEXSZ + 1; | |
163 | ||
164 | if (scan < eof) { | |
165 | if (!(eol = strchr(scan, '\n'))) | |
166 | goto parse_failed; | |
167 | scan = eol + 1; | |
168 | } | |
169 | ||
170 | git_oid_cpy(&ref->peel, &oid); | |
171 | ref->flags |= PACKREF_HAS_PEEL; | |
172 | } | |
173 | else if (backend->peeling_mode == PEELING_FULL || | |
174 | (backend->peeling_mode == PEELING_STANDARD && | |
175 | git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0)) | |
176 | ref->flags |= PACKREF_CANNOT_PEEL; | |
d00d5464 ET |
177 | } |
178 | ||
8d9a85d4 | 179 | git_sortedcache_wunlock(backend->refcache); |
fe372740 RB |
180 | git_buf_free(&packedrefs); |
181 | ||
d00d5464 ET |
182 | return 0; |
183 | ||
184 | parse_failed: | |
fe372740 RB |
185 | giterr_set(GITERR_REFERENCE, "Corrupted packed references file"); |
186 | ||
187 | git_sortedcache_clear(backend->refcache, false); | |
8d9a85d4 | 188 | git_sortedcache_wunlock(backend->refcache); |
fe372740 RB |
189 | git_buf_free(&packedrefs); |
190 | ||
d00d5464 ET |
191 | return -1; |
192 | } | |
193 | ||
fe372740 RB |
194 | static int loose_parse_oid( |
195 | git_oid *oid, const char *filename, git_buf *file_content) | |
d00d5464 | 196 | { |
fe372740 | 197 | const char *str = git_buf_cstr(file_content); |
d00d5464 | 198 | |
fe372740 | 199 | if (git_buf_len(file_content) < GIT_OID_HEXSZ) |
d00d5464 ET |
200 | goto corrupted; |
201 | ||
d00d5464 | 202 | /* we need to get 40 OID characters from the file */ |
fe372740 | 203 | if (git_oid_fromstr(oid, str) < 0) |
d00d5464 ET |
204 | goto corrupted; |
205 | ||
206 | /* If the file is longer than 40 chars, the 41st must be a space */ | |
207 | str += GIT_OID_HEXSZ; | |
208 | if (*str == '\0' || git__isspace(*str)) | |
209 | return 0; | |
210 | ||
211 | corrupted: | |
a5de9044 | 212 | giterr_set(GITERR_REFERENCE, "Corrupted loose reference file: %s", filename); |
d00d5464 ET |
213 | return -1; |
214 | } | |
215 | ||
fe372740 RB |
216 | static int loose_readbuffer(git_buf *buf, const char *base, const char *path) |
217 | { | |
218 | int error; | |
219 | ||
220 | /* build full path to file */ | |
221 | if ((error = git_buf_joinpath(buf, base, path)) < 0 || | |
222 | (error = git_futils_readbuffer(buf, buf->ptr)) < 0) | |
223 | git_buf_free(buf); | |
224 | ||
225 | return error; | |
226 | } | |
227 | ||
228 | static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name) | |
d00d5464 | 229 | { |
fe372740 | 230 | int error = 0; |
d00d5464 ET |
231 | git_buf ref_file = GIT_BUF_INIT; |
232 | struct packref *ref = NULL; | |
fe372740 | 233 | git_oid oid; |
d00d5464 | 234 | |
fe372740 RB |
235 | /* if we fail to load the loose reference, assume someone changed |
236 | * the filesystem under us and skip it... | |
237 | */ | |
238 | if (loose_readbuffer(&ref_file, backend->path, name) < 0) { | |
239 | giterr_clear(); | |
240 | goto done; | |
241 | } | |
d00d5464 | 242 | |
0f0f5655 | 243 | /* skip symbolic refs */ |
fe372740 RB |
244 | if (!git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF)) |
245 | goto done; | |
0f0f5655 | 246 | |
fe372740 RB |
247 | /* parse OID from file */ |
248 | if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0) | |
249 | goto done; | |
d00d5464 | 250 | |
8d9a85d4 | 251 | git_sortedcache_wlock(backend->refcache); |
d00d5464 | 252 | |
fe372740 RB |
253 | if (!(error = git_sortedcache_upsert( |
254 | (void **)&ref, backend->refcache, name))) { | |
d00d5464 | 255 | |
fe372740 RB |
256 | git_oid_cpy(&ref->oid, &oid); |
257 | ref->flags = PACKREF_WAS_LOOSE; | |
d00d5464 ET |
258 | } |
259 | ||
8d9a85d4 | 260 | git_sortedcache_wunlock(backend->refcache); |
d00d5464 | 261 | |
fe372740 | 262 | done: |
d00d5464 | 263 | git_buf_free(&ref_file); |
fe372740 | 264 | return error; |
d00d5464 ET |
265 | } |
266 | ||
96869a4e RB |
267 | struct packed_loadloose_data { |
268 | refdb_fs_backend *backend; | |
269 | git_error_state error; | |
270 | }; | |
271 | ||
272 | static int _dirent_loose_load(void *data_, git_buf *full_path) | |
d00d5464 | 273 | { |
96869a4e | 274 | struct packed_loadloose_data *data = data_; |
d00d5464 | 275 | const char *file_path; |
d00d5464 | 276 | |
fe372740 RB |
277 | if (git__suffixcmp(full_path->ptr, ".lock") == 0) |
278 | return 0; | |
279 | ||
280 | if (git_path_isdir(full_path->ptr)) | |
219d3457 | 281 | return git_path_direach( |
96869a4e | 282 | full_path, data->backend->direach_flags, _dirent_loose_load, data); |
d00d5464 | 283 | |
96869a4e | 284 | file_path = full_path->ptr + strlen(data->backend->path); |
d00d5464 | 285 | |
96869a4e RB |
286 | return giterr_capture( |
287 | &data->error, loose_lookup_to_packfile(data->backend, file_path)); | |
d00d5464 ET |
288 | } |
289 | ||
290 | /* | |
291 | * Load all the loose references from the repository | |
292 | * into the in-memory Packfile, and build a vector with | |
293 | * all the references so it can be written back to | |
294 | * disk. | |
295 | */ | |
296 | static int packed_loadloose(refdb_fs_backend *backend) | |
297 | { | |
fe372740 | 298 | int error; |
d00d5464 | 299 | git_buf refs_path = GIT_BUF_INIT; |
96869a4e | 300 | struct packed_loadloose_data data; |
d00d5464 ET |
301 | |
302 | if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0) | |
303 | return -1; | |
304 | ||
96869a4e RB |
305 | memset(&data, 0, sizeof(data)); |
306 | data.backend = backend; | |
307 | ||
d00d5464 ET |
308 | /* |
309 | * Load all the loose files from disk into the Packfile table. | |
310 | * This will overwrite any old packed entries with their | |
311 | * updated loose versions | |
312 | */ | |
219d3457 | 313 | error = git_path_direach( |
96869a4e | 314 | &refs_path, backend->direach_flags, _dirent_loose_load, &data); |
fe372740 | 315 | |
d00d5464 ET |
316 | git_buf_free(&refs_path); |
317 | ||
96869a4e RB |
318 | if (error == GIT_EUSER) |
319 | error = giterr_restore(&data.error); | |
320 | ||
321 | return error; | |
d00d5464 ET |
322 | } |
323 | ||
324 | static int refdb_fs_backend__exists( | |
325 | int *exists, | |
326 | git_refdb_backend *_backend, | |
327 | const char *ref_name) | |
328 | { | |
fe372740 | 329 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 ET |
330 | git_buf ref_path = GIT_BUF_INIT; |
331 | ||
fe372740 | 332 | assert(backend); |
d00d5464 | 333 | |
fe372740 RB |
334 | if (packed_reload(backend) < 0 || |
335 | git_buf_joinpath(&ref_path, backend->path, ref_name) < 0) | |
d00d5464 ET |
336 | return -1; |
337 | ||
fe372740 | 338 | *exists = git_path_isfile(ref_path.ptr) || |
8d9a85d4 | 339 | (git_sortedcache_lookup(backend->refcache, ref_name) != NULL); |
d00d5464 ET |
340 | |
341 | git_buf_free(&ref_path); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static const char *loose_parse_symbolic(git_buf *file_content) | |
346 | { | |
347 | const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF); | |
348 | const char *refname_start; | |
349 | ||
350 | refname_start = (const char *)file_content->ptr; | |
351 | ||
352 | if (git_buf_len(file_content) < header_len + 1) { | |
353 | giterr_set(GITERR_REFERENCE, "Corrupted loose reference file"); | |
354 | return NULL; | |
355 | } | |
356 | ||
357 | /* | |
358 | * Assume we have already checked for the header | |
359 | * before calling this function | |
360 | */ | |
361 | refname_start += header_len; | |
362 | ||
363 | return refname_start; | |
364 | } | |
365 | ||
366 | static int loose_lookup( | |
367 | git_reference **out, | |
368 | refdb_fs_backend *backend, | |
369 | const char *ref_name) | |
370 | { | |
d00d5464 ET |
371 | git_buf ref_file = GIT_BUF_INIT; |
372 | int error = 0; | |
373 | ||
b7107131 RB |
374 | if (out) |
375 | *out = NULL; | |
376 | ||
fe372740 RB |
377 | if ((error = loose_readbuffer(&ref_file, backend->path, ref_name)) < 0) |
378 | /* cannot read loose ref file - gah */; | |
379 | else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) { | |
380 | const char *target; | |
d00d5464 | 381 | |
d00d5464 ET |
382 | git_buf_rtrim(&ref_file); |
383 | ||
fe372740 | 384 | if (!(target = loose_parse_symbolic(&ref_file))) |
d00d5464 | 385 | error = -1; |
fe372740 | 386 | else if (out != NULL) |
b7107131 | 387 | *out = git_reference__alloc_symbolic(ref_name, target); |
d00d5464 | 388 | } else { |
fe372740 | 389 | git_oid oid; |
4dcd8780 | 390 | |
fe372740 RB |
391 | if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) && |
392 | out != NULL) | |
b7107131 | 393 | *out = git_reference__alloc(ref_name, &oid, NULL); |
d00d5464 ET |
394 | } |
395 | ||
d00d5464 ET |
396 | git_buf_free(&ref_file); |
397 | return error; | |
398 | } | |
399 | ||
fe372740 | 400 | static int ref_error_notfound(const char *name) |
d00d5464 | 401 | { |
fe372740 RB |
402 | giterr_set(GITERR_REFERENCE, "Reference '%s' not found", name); |
403 | return GIT_ENOTFOUND; | |
d00d5464 ET |
404 | } |
405 | ||
406 | static int packed_lookup( | |
407 | git_reference **out, | |
408 | refdb_fs_backend *backend, | |
409 | const char *ref_name) | |
410 | { | |
d00d5464 | 411 | int error = 0; |
fe372740 | 412 | struct packref *entry; |
4dcd8780 | 413 | |
fe372740 | 414 | if (packed_reload(backend) < 0) |
d00d5464 | 415 | return -1; |
4dcd8780 | 416 | |
8d9a85d4 RB |
417 | if (git_sortedcache_rlock(backend->refcache) < 0) |
418 | return -1; | |
fe372740 RB |
419 | |
420 | entry = git_sortedcache_lookup(backend->refcache, ref_name); | |
421 | if (!entry) { | |
422 | error = ref_error_notfound(ref_name); | |
423 | } else { | |
424 | *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel); | |
425 | if (!*out) | |
426 | error = -1; | |
427 | } | |
428 | ||
8d9a85d4 RB |
429 | git_sortedcache_runlock(backend->refcache); |
430 | ||
fe372740 | 431 | return error; |
d00d5464 ET |
432 | } |
433 | ||
434 | static int refdb_fs_backend__lookup( | |
435 | git_reference **out, | |
436 | git_refdb_backend *_backend, | |
437 | const char *ref_name) | |
438 | { | |
fe372740 RB |
439 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
440 | int error; | |
d00d5464 | 441 | |
fe372740 | 442 | assert(backend); |
d00d5464 | 443 | |
fe372740 | 444 | if (!(error = loose_lookup(out, backend, ref_name))) |
d00d5464 ET |
445 | return 0; |
446 | ||
447 | /* only try to lookup this reference on the packfile if it | |
448 | * wasn't found on the loose refs; not if there was a critical error */ | |
fe372740 | 449 | if (error == GIT_ENOTFOUND) { |
d00d5464 | 450 | giterr_clear(); |
fe372740 | 451 | error = packed_lookup(out, backend, ref_name); |
d00d5464 ET |
452 | } |
453 | ||
fe372740 | 454 | return error; |
d00d5464 ET |
455 | } |
456 | ||
4def7035 CMN |
457 | typedef struct { |
458 | git_reference_iterator parent; | |
2638a03a | 459 | |
ec24e542 | 460 | char *glob; |
c77342ef RB |
461 | |
462 | git_pool pool; | |
2638a03a | 463 | git_vector loose; |
c77342ef | 464 | |
fe372740 RB |
465 | size_t loose_pos; |
466 | size_t packed_pos; | |
4def7035 CMN |
467 | } refdb_fs_iter; |
468 | ||
4def7035 | 469 | static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter) |
d00d5464 | 470 | { |
4def7035 | 471 | refdb_fs_iter *iter = (refdb_fs_iter *) _iter; |
2638a03a VM |
472 | |
473 | git_vector_free(&iter->loose); | |
c77342ef | 474 | git_pool_clear(&iter->pool); |
4def7035 CMN |
475 | git__free(iter); |
476 | } | |
d00d5464 | 477 | |
ec24e542 | 478 | static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter) |
4def7035 | 479 | { |
219d3457 | 480 | int error = 0; |
2638a03a | 481 | git_buf path = GIT_BUF_INIT; |
c77342ef | 482 | git_iterator *fsit = NULL; |
2638a03a | 483 | const git_index_entry *entry = NULL; |
d00d5464 | 484 | |
cee695ae RB |
485 | if (!backend->path) /* do nothing if no path for loose refs */ |
486 | return 0; | |
487 | ||
fe372740 RB |
488 | if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 || |
489 | (error = git_iterator_for_filesystem( | |
219d3457 | 490 | &fsit, path.ptr, backend->iterator_flags, NULL, NULL)) < 0) { |
fe372740 RB |
491 | git_buf_free(&path); |
492 | return error; | |
493 | } | |
d00d5464 | 494 | |
fe372740 | 495 | error = git_buf_sets(&path, GIT_REFS_DIR); |
d00d5464 | 496 | |
fe372740 | 497 | while (!error && !git_iterator_advance(&entry, fsit)) { |
2638a03a | 498 | const char *ref_name; |
fe372740 | 499 | struct packref *ref; |
c77342ef | 500 | char *ref_dup; |
d00d5464 | 501 | |
2638a03a VM |
502 | git_buf_truncate(&path, strlen(GIT_REFS_DIR)); |
503 | git_buf_puts(&path, entry->path); | |
504 | ref_name = git_buf_cstr(&path); | |
4dcd8780 | 505 | |
ec24e542 | 506 | if (git__suffixcmp(ref_name, ".lock") == 0 || |
cee695ae | 507 | (iter->glob && p_fnmatch(iter->glob, ref_name, 0) != 0)) |
2638a03a | 508 | continue; |
d00d5464 | 509 | |
8d9a85d4 | 510 | git_sortedcache_rlock(backend->refcache); |
fe372740 RB |
511 | ref = git_sortedcache_lookup(backend->refcache, ref_name); |
512 | if (ref) | |
2638a03a | 513 | ref->flags |= PACKREF_SHADOWED; |
8d9a85d4 | 514 | git_sortedcache_runlock(backend->refcache); |
d00d5464 | 515 | |
fe372740 RB |
516 | ref_dup = git_pool_strdup(&iter->pool, ref_name); |
517 | if (!ref_dup) | |
c77342ef | 518 | error = -1; |
fe372740 RB |
519 | else |
520 | error = git_vector_insert(&iter->loose, ref_dup); | |
2638a03a | 521 | } |
d00d5464 | 522 | |
2638a03a VM |
523 | git_iterator_free(fsit); |
524 | git_buf_free(&path); | |
4def7035 | 525 | |
fe372740 | 526 | return error; |
4def7035 CMN |
527 | } |
528 | ||
ec24e542 VM |
529 | static int refdb_fs_backend__iterator_next( |
530 | git_reference **out, git_reference_iterator *_iter) | |
4def7035 | 531 | { |
8d9a85d4 | 532 | int error = GIT_ITEROVER; |
2638a03a | 533 | refdb_fs_iter *iter = (refdb_fs_iter *)_iter; |
ec24e542 | 534 | refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend; |
fe372740 | 535 | struct packref *ref; |
4def7035 | 536 | |
56960b83 | 537 | while (iter->loose_pos < iter->loose.length) { |
2638a03a | 538 | const char *path = git_vector_get(&iter->loose, iter->loose_pos++); |
56960b83 VM |
539 | |
540 | if (loose_lookup(out, backend, path) == 0) | |
541 | return 0; | |
542 | ||
543 | giterr_clear(); | |
2638a03a | 544 | } |
d00d5464 | 545 | |
8d9a85d4 | 546 | git_sortedcache_rlock(backend->refcache); |
d00d5464 | 547 | |
fe372740 RB |
548 | while (iter->packed_pos < git_sortedcache_entrycount(backend->refcache)) { |
549 | ref = git_sortedcache_entry(backend->refcache, iter->packed_pos++); | |
550 | if (!ref) /* stop now if another thread deleted refs and we past end */ | |
551 | break; | |
ec24e542 VM |
552 | |
553 | if (ref->flags & PACKREF_SHADOWED) | |
554 | continue; | |
ec24e542 VM |
555 | if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0) |
556 | continue; | |
99d32707 | 557 | |
56960b83 | 558 | *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel); |
8d9a85d4 RB |
559 | error = (*out != NULL) ? 0 : -1; |
560 | break; | |
4def7035 CMN |
561 | } |
562 | ||
8d9a85d4 RB |
563 | git_sortedcache_runlock(backend->refcache); |
564 | return error; | |
d00d5464 ET |
565 | } |
566 | ||
ec24e542 VM |
567 | static int refdb_fs_backend__iterator_next_name( |
568 | const char **out, git_reference_iterator *_iter) | |
569 | { | |
8d9a85d4 | 570 | int error = GIT_ITEROVER; |
ec24e542 VM |
571 | refdb_fs_iter *iter = (refdb_fs_iter *)_iter; |
572 | refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend; | |
8d9a85d4 | 573 | struct packref *ref; |
ec24e542 VM |
574 | |
575 | while (iter->loose_pos < iter->loose.length) { | |
576 | const char *path = git_vector_get(&iter->loose, iter->loose_pos++); | |
577 | ||
8d9a85d4 RB |
578 | if (loose_lookup(NULL, backend, path) == 0) { |
579 | *out = path; | |
580 | return 0; | |
b7107131 RB |
581 | } |
582 | ||
8d9a85d4 | 583 | giterr_clear(); |
ec24e542 VM |
584 | } |
585 | ||
8d9a85d4 | 586 | git_sortedcache_rlock(backend->refcache); |
fe372740 RB |
587 | |
588 | while (iter->packed_pos < git_sortedcache_entrycount(backend->refcache)) { | |
8d9a85d4 RB |
589 | ref = git_sortedcache_entry(backend->refcache, iter->packed_pos++); |
590 | if (!ref) /* stop now if another thread deleted refs and we past end */ | |
591 | break; | |
fe372740 RB |
592 | |
593 | if (ref->flags & PACKREF_SHADOWED) | |
594 | continue; | |
8d9a85d4 | 595 | if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0) |
ec24e542 VM |
596 | continue; |
597 | ||
8d9a85d4 RB |
598 | *out = ref->name; |
599 | error = 0; | |
600 | break; | |
ec24e542 VM |
601 | } |
602 | ||
8d9a85d4 RB |
603 | git_sortedcache_runlock(backend->refcache); |
604 | return error; | |
ec24e542 VM |
605 | } |
606 | ||
607 | static int refdb_fs_backend__iterator( | |
608 | git_reference_iterator **out, git_refdb_backend *_backend, const char *glob) | |
609 | { | |
610 | refdb_fs_iter *iter; | |
fe372740 | 611 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
ec24e542 | 612 | |
fe372740 | 613 | assert(backend); |
ec24e542 | 614 | |
fe372740 | 615 | if (packed_reload(backend) < 0) |
ec24e542 VM |
616 | return -1; |
617 | ||
618 | iter = git__calloc(1, sizeof(refdb_fs_iter)); | |
619 | GITERR_CHECK_ALLOC(iter); | |
620 | ||
fe372740 RB |
621 | if (git_pool_init(&iter->pool, 1, 0) < 0 || |
622 | git_vector_init(&iter->loose, 8, NULL) < 0) | |
c77342ef RB |
623 | goto fail; |
624 | ||
625 | if (glob != NULL && | |
626 | (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) | |
627 | goto fail; | |
ec24e542 VM |
628 | |
629 | iter->parent.next = refdb_fs_backend__iterator_next; | |
630 | iter->parent.next_name = refdb_fs_backend__iterator_next_name; | |
631 | iter->parent.free = refdb_fs_backend__iterator_free; | |
632 | ||
c77342ef RB |
633 | if (iter_load_loose_paths(backend, iter) < 0) |
634 | goto fail; | |
ec24e542 VM |
635 | |
636 | *out = (git_reference_iterator *)iter; | |
637 | return 0; | |
c77342ef RB |
638 | |
639 | fail: | |
640 | refdb_fs_backend__iterator_free((git_reference_iterator *)iter); | |
641 | return -1; | |
ec24e542 VM |
642 | } |
643 | ||
4e6e2ff2 VM |
644 | static bool ref_is_available( |
645 | const char *old_ref, const char *new_ref, const char *this_ref) | |
646 | { | |
647 | if (old_ref == NULL || strcmp(old_ref, this_ref)) { | |
648 | size_t reflen = strlen(this_ref); | |
649 | size_t newlen = strlen(new_ref); | |
650 | size_t cmplen = reflen < newlen ? reflen : newlen; | |
651 | const char *lead = reflen < newlen ? new_ref : this_ref; | |
652 | ||
653 | if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') { | |
654 | return false; | |
655 | } | |
656 | } | |
657 | ||
658 | return true; | |
659 | } | |
660 | ||
661 | static int reference_path_available( | |
662 | refdb_fs_backend *backend, | |
663 | const char *new_ref, | |
664 | const char* old_ref, | |
665 | int force) | |
666 | { | |
fe372740 | 667 | size_t i; |
4e6e2ff2 | 668 | |
fe372740 | 669 | if (packed_reload(backend) < 0) |
4e6e2ff2 VM |
670 | return -1; |
671 | ||
672 | if (!force) { | |
673 | int exists; | |
674 | ||
fe372740 RB |
675 | if (refdb_fs_backend__exists( |
676 | &exists, (git_refdb_backend *)backend, new_ref) < 0) | |
4e6e2ff2 VM |
677 | return -1; |
678 | ||
679 | if (exists) { | |
680 | giterr_set(GITERR_REFERENCE, | |
681 | "Failed to write reference '%s': a reference with " | |
8d9a85d4 | 682 | "that name already exists.", new_ref); |
4e6e2ff2 VM |
683 | return GIT_EEXISTS; |
684 | } | |
685 | } | |
686 | ||
8d9a85d4 | 687 | git_sortedcache_rlock(backend->refcache); |
fe372740 RB |
688 | |
689 | for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { | |
8d9a85d4 | 690 | struct packref *ref = git_sortedcache_entry(backend->refcache, i); |
fe372740 | 691 | |
8d9a85d4 RB |
692 | if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { |
693 | git_sortedcache_runlock(backend->refcache); | |
4e6e2ff2 | 694 | giterr_set(GITERR_REFERENCE, |
8d9a85d4 | 695 | "Path to reference '%s' collides with existing one", new_ref); |
4e6e2ff2 VM |
696 | return -1; |
697 | } | |
fe372740 | 698 | } |
c77342ef | 699 | |
8d9a85d4 | 700 | git_sortedcache_runlock(backend->refcache); |
4e6e2ff2 VM |
701 | return 0; |
702 | } | |
ec24e542 | 703 | |
d00d5464 ET |
704 | static int loose_write(refdb_fs_backend *backend, const git_reference *ref) |
705 | { | |
706 | git_filebuf file = GIT_FILEBUF_INIT; | |
707 | git_buf ref_path = GIT_BUF_INIT; | |
708 | ||
709 | /* Remove a possibly existing empty directory hierarchy | |
710 | * which name would collide with the reference name | |
711 | */ | |
4e6e2ff2 | 712 | if (git_futils_rmdir_r(ref->name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0) |
d00d5464 ET |
713 | return -1; |
714 | ||
715 | if (git_buf_joinpath(&ref_path, backend->path, ref->name) < 0) | |
716 | return -1; | |
717 | ||
1d3a8aeb | 718 | if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) { |
d00d5464 ET |
719 | git_buf_free(&ref_path); |
720 | return -1; | |
721 | } | |
722 | ||
723 | git_buf_free(&ref_path); | |
724 | ||
725 | if (ref->type == GIT_REF_OID) { | |
726 | char oid[GIT_OID_HEXSZ + 1]; | |
fe372740 | 727 | git_oid_nfmt(oid, sizeof(oid), &ref->target.oid); |
d00d5464 ET |
728 | |
729 | git_filebuf_printf(&file, "%s\n", oid); | |
d00d5464 ET |
730 | } else if (ref->type == GIT_REF_SYMBOLIC) { |
731 | git_filebuf_printf(&file, GIT_SYMREF "%s\n", ref->target.symbolic); | |
732 | } else { | |
733 | assert(0); /* don't let this happen */ | |
734 | } | |
735 | ||
1d3a8aeb | 736 | return git_filebuf_commit(&file); |
d00d5464 ET |
737 | } |
738 | ||
d00d5464 ET |
739 | /* |
740 | * Find out what object this reference resolves to. | |
741 | * | |
742 | * For references that point to a 'big' tag (e.g. an | |
743 | * actual tag object on the repository), we need to | |
744 | * cache on the packfile the OID of the object to | |
745 | * which that 'big tag' is pointing to. | |
746 | */ | |
747 | static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref) | |
748 | { | |
749 | git_object *object; | |
750 | ||
f69db390 | 751 | if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL) |
d00d5464 ET |
752 | return 0; |
753 | ||
d00d5464 ET |
754 | /* |
755 | * Find the tagged object in the repository | |
756 | */ | |
757 | if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJ_ANY) < 0) | |
758 | return -1; | |
759 | ||
760 | /* | |
761 | * If the tagged object is a Tag object, we need to resolve it; | |
762 | * if the ref is actually a 'weak' ref, we don't need to resolve | |
763 | * anything. | |
764 | */ | |
765 | if (git_object_type(object) == GIT_OBJ_TAG) { | |
766 | git_tag *tag = (git_tag *)object; | |
767 | ||
768 | /* | |
769 | * Find the object pointed at by this tag | |
770 | */ | |
771 | git_oid_cpy(&ref->peel, git_tag_target_id(tag)); | |
f69db390 | 772 | ref->flags |= PACKREF_HAS_PEEL; |
d00d5464 ET |
773 | |
774 | /* | |
775 | * The reference has now cached the resolved OID, and is | |
776 | * marked at such. When written to the packfile, it'll be | |
777 | * accompanied by this resolved oid | |
778 | */ | |
779 | } | |
780 | ||
781 | git_object_free(object); | |
782 | return 0; | |
783 | } | |
784 | ||
785 | /* | |
786 | * Write a single reference into a packfile | |
787 | */ | |
788 | static int packed_write_ref(struct packref *ref, git_filebuf *file) | |
789 | { | |
790 | char oid[GIT_OID_HEXSZ + 1]; | |
fe372740 | 791 | git_oid_nfmt(oid, sizeof(oid), &ref->oid); |
d00d5464 ET |
792 | |
793 | /* | |
794 | * For references that peel to an object in the repo, we must | |
795 | * write the resulting peel on a separate line, e.g. | |
796 | * | |
797 | * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4 | |
798 | * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100 | |
799 | * | |
800 | * This obviously only applies to tags. | |
801 | * The required peels have already been loaded into `ref->peel_target`. | |
802 | */ | |
f69db390 | 803 | if (ref->flags & PACKREF_HAS_PEEL) { |
d00d5464 | 804 | char peel[GIT_OID_HEXSZ + 1]; |
fe372740 | 805 | git_oid_nfmt(peel, sizeof(peel), &ref->peel); |
d00d5464 ET |
806 | |
807 | if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0) | |
808 | return -1; | |
809 | } else { | |
810 | if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0) | |
811 | return -1; | |
812 | } | |
813 | ||
814 | return 0; | |
815 | } | |
816 | ||
817 | /* | |
818 | * Remove all loose references | |
819 | * | |
820 | * Once we have successfully written a packfile, | |
821 | * all the loose references that were packed must be | |
822 | * removed from disk. | |
823 | * | |
824 | * This is a dangerous method; make sure the packfile | |
825 | * is well-written, because we are destructing references | |
826 | * here otherwise. | |
827 | */ | |
fe372740 | 828 | static int packed_remove_loose(refdb_fs_backend *backend) |
d00d5464 | 829 | { |
10c06114 | 830 | size_t i; |
d00d5464 ET |
831 | git_buf full_path = GIT_BUF_INIT; |
832 | int failed = 0; | |
833 | ||
fe372740 RB |
834 | /* backend->refcache is already locked when this is called */ |
835 | ||
836 | for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { | |
837 | struct packref *ref = git_sortedcache_entry(backend->refcache, i); | |
d00d5464 | 838 | |
8d9a85d4 | 839 | if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) |
d00d5464 ET |
840 | continue; |
841 | ||
842 | if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0) | |
843 | return -1; /* critical; do not try to recover on oom */ | |
844 | ||
fe372740 | 845 | if (git_path_exists(full_path.ptr) && p_unlink(full_path.ptr) < 0) { |
d00d5464 ET |
846 | if (failed) |
847 | continue; | |
848 | ||
849 | giterr_set(GITERR_REFERENCE, | |
850 | "Failed to remove loose reference '%s' after packing: %s", | |
851 | full_path.ptr, strerror(errno)); | |
d00d5464 ET |
852 | failed = 1; |
853 | } | |
854 | ||
855 | /* | |
856 | * if we fail to remove a single file, this is *not* good, | |
857 | * but we should keep going and remove as many as possible. | |
858 | * After we've removed as many files as possible, we return | |
859 | * the error code anyway. | |
860 | */ | |
861 | } | |
862 | ||
863 | git_buf_free(&full_path); | |
864 | return failed ? -1 : 0; | |
865 | } | |
866 | ||
867 | /* | |
868 | * Write all the contents in the in-memory packfile to disk. | |
869 | */ | |
870 | static int packed_write(refdb_fs_backend *backend) | |
871 | { | |
8d9a85d4 | 872 | git_sortedcache *refcache = backend->refcache; |
d00d5464 | 873 | git_filebuf pack_file = GIT_FILEBUF_INIT; |
10c06114 | 874 | size_t i; |
d00d5464 | 875 | |
fe372740 | 876 | /* lock the cache to updates while we do this */ |
8d9a85d4 | 877 | if (git_sortedcache_wlock(refcache) < 0) |
d00d5464 ET |
878 | return -1; |
879 | ||
fe372740 | 880 | /* Open the file! */ |
1d3a8aeb | 881 | if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0) |
8d9a85d4 | 882 | goto fail; |
d00d5464 ET |
883 | |
884 | /* Packfiles have a header... apparently | |
885 | * This is in fact not required, but we might as well print it | |
886 | * just for kicks */ | |
887 | if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0) | |
8d9a85d4 | 888 | goto fail; |
d00d5464 | 889 | |
8d9a85d4 RB |
890 | for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) { |
891 | struct packref *ref = git_sortedcache_entry(refcache, i); | |
d00d5464 ET |
892 | |
893 | if (packed_find_peel(backend, ref) < 0) | |
8d9a85d4 | 894 | goto fail; |
d00d5464 ET |
895 | |
896 | if (packed_write_ref(ref, &pack_file) < 0) | |
8d9a85d4 | 897 | goto fail; |
d00d5464 ET |
898 | } |
899 | ||
900 | /* if we've written all the references properly, we can commit | |
901 | * the packfile to make the changes effective */ | |
1d3a8aeb | 902 | if (git_filebuf_commit(&pack_file) < 0) |
8d9a85d4 | 903 | goto fail; |
d00d5464 ET |
904 | |
905 | /* when and only when the packfile has been properly written, | |
906 | * we can go ahead and remove the loose refs */ | |
fe372740 | 907 | if (packed_remove_loose(backend) < 0) |
8d9a85d4 | 908 | goto fail; |
d00d5464 | 909 | |
8d9a85d4 RB |
910 | git_sortedcache_updated(refcache); |
911 | git_sortedcache_wunlock(refcache); | |
d00d5464 ET |
912 | |
913 | /* we're good now */ | |
914 | return 0; | |
915 | ||
8d9a85d4 | 916 | fail: |
d00d5464 | 917 | git_filebuf_cleanup(&pack_file); |
8d9a85d4 | 918 | git_sortedcache_wunlock(refcache); |
d00d5464 ET |
919 | |
920 | return -1; | |
921 | } | |
922 | ||
923 | static int refdb_fs_backend__write( | |
924 | git_refdb_backend *_backend, | |
4e6e2ff2 VM |
925 | const git_reference *ref, |
926 | int force) | |
d00d5464 | 927 | { |
fe372740 | 928 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
4e6e2ff2 | 929 | int error; |
d00d5464 | 930 | |
fe372740 | 931 | assert(backend); |
d00d5464 | 932 | |
4e6e2ff2 VM |
933 | error = reference_path_available(backend, ref->name, NULL, force); |
934 | if (error < 0) | |
935 | return error; | |
936 | ||
d00d5464 ET |
937 | return loose_write(backend, ref); |
938 | } | |
939 | ||
940 | static int refdb_fs_backend__delete( | |
941 | git_refdb_backend *_backend, | |
4e6e2ff2 | 942 | const char *ref_name) |
d00d5464 | 943 | { |
fe372740 | 944 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 | 945 | git_buf loose_path = GIT_BUF_INIT; |
fe372740 | 946 | size_t pack_pos; |
4e6e2ff2 | 947 | int error = 0; |
038c1654 | 948 | bool loose_deleted = 0; |
d00d5464 | 949 | |
fe372740 | 950 | assert(backend && ref_name); |
d00d5464 ET |
951 | |
952 | /* If a loose reference exists, remove it from the filesystem */ | |
4e6e2ff2 | 953 | if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0) |
d00d5464 ET |
954 | return -1; |
955 | ||
956 | if (git_path_isfile(loose_path.ptr)) { | |
957 | error = p_unlink(loose_path.ptr); | |
958 | loose_deleted = 1; | |
959 | } | |
4dcd8780 | 960 | |
d00d5464 ET |
961 | git_buf_free(&loose_path); |
962 | ||
963 | if (error != 0) | |
964 | return error; | |
965 | ||
fe372740 RB |
966 | if (packed_reload(backend) < 0) |
967 | return -1; | |
968 | ||
d00d5464 | 969 | /* If a packed reference exists, remove it from the packfile and repack */ |
8d9a85d4 | 970 | if (git_sortedcache_wlock(backend->refcache) < 0) |
fe372740 | 971 | return -1; |
4e6e2ff2 | 972 | |
fe372740 RB |
973 | if (!(error = git_sortedcache_lookup_index( |
974 | &pack_pos, backend->refcache, ref_name))) | |
8d9a85d4 | 975 | error = git_sortedcache_remove(backend->refcache, pack_pos); |
d00d5464 | 976 | |
8d9a85d4 | 977 | git_sortedcache_wunlock(backend->refcache); |
4dcd8780 | 978 | |
fe372740 RB |
979 | if (error == GIT_ENOTFOUND) |
980 | return loose_deleted ? 0 : ref_error_notfound(ref_name); | |
981 | ||
982 | return packed_write(backend); | |
d00d5464 ET |
983 | } |
984 | ||
4e6e2ff2 VM |
985 | static int refdb_fs_backend__rename( |
986 | git_reference **out, | |
987 | git_refdb_backend *_backend, | |
988 | const char *old_name, | |
989 | const char *new_name, | |
990 | int force) | |
991 | { | |
fe372740 | 992 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
4e6e2ff2 VM |
993 | git_reference *old, *new; |
994 | int error; | |
995 | ||
fe372740 | 996 | assert(backend); |
4e6e2ff2 | 997 | |
fe372740 RB |
998 | if ((error = reference_path_available( |
999 | backend, new_name, old_name, force)) < 0 || | |
1000 | (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0) | |
4e6e2ff2 VM |
1001 | return error; |
1002 | ||
fe372740 | 1003 | if ((error = refdb_fs_backend__delete(_backend, old_name)) < 0) { |
4e6e2ff2 VM |
1004 | git_reference_free(old); |
1005 | return error; | |
1006 | } | |
1007 | ||
fe372740 RB |
1008 | new = git_reference__set_name(old, new_name); |
1009 | if (!new) { | |
1010 | git_reference_free(old); | |
1011 | return -1; | |
4e6e2ff2 VM |
1012 | } |
1013 | ||
fe372740 | 1014 | if ((error = loose_write(backend, new)) < 0 || out == NULL) { |
4e6e2ff2 | 1015 | git_reference_free(new); |
fe372740 | 1016 | return error; |
4e6e2ff2 VM |
1017 | } |
1018 | ||
fe372740 | 1019 | *out = new; |
4e6e2ff2 VM |
1020 | return 0; |
1021 | } | |
1022 | ||
d00d5464 ET |
1023 | static int refdb_fs_backend__compress(git_refdb_backend *_backend) |
1024 | { | |
fe372740 | 1025 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 | 1026 | |
fe372740 | 1027 | assert(backend); |
d00d5464 | 1028 | |
fe372740 | 1029 | if (packed_reload(backend) < 0 || /* load the existing packfile */ |
d00d5464 ET |
1030 | packed_loadloose(backend) < 0 || /* add all the loose refs */ |
1031 | packed_write(backend) < 0) /* write back to disk */ | |
1032 | return -1; | |
1033 | ||
1034 | return 0; | |
1035 | } | |
1036 | ||
d00d5464 ET |
1037 | static void refdb_fs_backend__free(git_refdb_backend *_backend) |
1038 | { | |
fe372740 | 1039 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 | 1040 | |
fe372740 | 1041 | assert(backend); |
d00d5464 | 1042 | |
fe372740 | 1043 | git_sortedcache_free(backend->refcache); |
bade5194 | 1044 | git__free(backend->path); |
d00d5464 ET |
1045 | git__free(backend); |
1046 | } | |
1047 | ||
8cddf9b8 VM |
1048 | static int setup_namespace(git_buf *path, git_repository *repo) |
1049 | { | |
1fed6b07 | 1050 | char *parts, *start, *end; |
8cddf9b8 | 1051 | |
69a3c766 CMN |
1052 | /* Not all repositories have a path */ |
1053 | if (repo->path_repository == NULL) | |
1054 | return 0; | |
1055 | ||
8cddf9b8 VM |
1056 | /* Load the path to the repo first */ |
1057 | git_buf_puts(path, repo->path_repository); | |
1058 | ||
1059 | /* if the repo is not namespaced, nothing else to do */ | |
1060 | if (repo->namespace == NULL) | |
1061 | return 0; | |
1062 | ||
1063 | parts = end = git__strdup(repo->namespace); | |
1064 | if (parts == NULL) | |
1065 | return -1; | |
1066 | ||
fe372740 | 1067 | /* |
8cddf9b8 VM |
1068 | * From `man gitnamespaces`: |
1069 | * namespaces which include a / will expand to a hierarchy | |
1070 | * of namespaces; for example, GIT_NAMESPACE=foo/bar will store | |
1071 | * refs under refs/namespaces/foo/refs/namespaces/bar/ | |
1072 | */ | |
1073 | while ((start = git__strsep(&end, "/")) != NULL) { | |
1074 | git_buf_printf(path, "refs/namespaces/%s/", start); | |
1075 | } | |
1076 | ||
1077 | git_buf_printf(path, "refs/namespaces/%s/refs", end); | |
1ed356dc | 1078 | git__free(parts); |
8cddf9b8 VM |
1079 | |
1080 | /* Make sure that the folder with the namespace exists */ | |
1fed6b07 | 1081 | if (git_futils_mkdir_r(git_buf_cstr(path), repo->path_repository, 0777) < 0) |
8cddf9b8 VM |
1082 | return -1; |
1083 | ||
fe372740 | 1084 | /* Return root of the namespaced path, i.e. without the trailing '/refs' */ |
8cddf9b8 VM |
1085 | git_buf_rtruncate_at_char(path, '/'); |
1086 | return 0; | |
1087 | } | |
1088 | ||
b976f3c2 CMN |
1089 | static int reflog_alloc(git_reflog **reflog, const char *name) |
1090 | { | |
1091 | git_reflog *log; | |
1092 | ||
1093 | *reflog = NULL; | |
1094 | ||
1095 | log = git__calloc(1, sizeof(git_reflog)); | |
1096 | GITERR_CHECK_ALLOC(log); | |
1097 | ||
1098 | log->ref_name = git__strdup(name); | |
1099 | GITERR_CHECK_ALLOC(log->ref_name); | |
1100 | ||
1101 | if (git_vector_init(&log->entries, 0, NULL) < 0) { | |
1102 | git__free(log->ref_name); | |
1103 | git__free(log); | |
1104 | return -1; | |
1105 | } | |
1106 | ||
1107 | *reflog = log; | |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) | |
1113 | { | |
1114 | const char *ptr; | |
1115 | git_reflog_entry *entry; | |
1116 | ||
1117 | #define seek_forward(_increase) do { \ | |
1118 | if (_increase >= buf_size) { \ | |
1119 | giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \ | |
1120 | goto fail; \ | |
1121 | } \ | |
1122 | buf += _increase; \ | |
1123 | buf_size -= _increase; \ | |
1124 | } while (0) | |
1125 | ||
1126 | while (buf_size > GIT_REFLOG_SIZE_MIN) { | |
1127 | entry = git__calloc(1, sizeof(git_reflog_entry)); | |
1128 | GITERR_CHECK_ALLOC(entry); | |
1129 | ||
1130 | entry->committer = git__malloc(sizeof(git_signature)); | |
1131 | GITERR_CHECK_ALLOC(entry->committer); | |
1132 | ||
1133 | if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0) | |
1134 | goto fail; | |
1135 | seek_forward(GIT_OID_HEXSZ + 1); | |
1136 | ||
1137 | if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0) | |
1138 | goto fail; | |
1139 | seek_forward(GIT_OID_HEXSZ + 1); | |
1140 | ||
1141 | ptr = buf; | |
1142 | ||
1143 | /* Seek forward to the end of the signature. */ | |
1144 | while (*buf && *buf != '\t' && *buf != '\n') | |
1145 | seek_forward(1); | |
1146 | ||
1147 | if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0) | |
1148 | goto fail; | |
1149 | ||
1150 | if (*buf == '\t') { | |
1151 | /* We got a message. Read everything till we reach LF. */ | |
1152 | seek_forward(1); | |
1153 | ptr = buf; | |
1154 | ||
1155 | while (*buf && *buf != '\n') | |
1156 | seek_forward(1); | |
1157 | ||
1158 | entry->msg = git__strndup(ptr, buf - ptr); | |
1159 | GITERR_CHECK_ALLOC(entry->msg); | |
1160 | } else | |
1161 | entry->msg = NULL; | |
1162 | ||
1163 | while (*buf && *buf == '\n' && buf_size > 1) | |
1164 | seek_forward(1); | |
1165 | ||
1166 | if (git_vector_insert(&log->entries, entry) < 0) | |
1167 | goto fail; | |
1168 | } | |
1169 | ||
1170 | return 0; | |
1171 | ||
1172 | #undef seek_forward | |
1173 | ||
1174 | fail: | |
1175 | if (entry) | |
1176 | git_reflog_entry__free(entry); | |
1177 | ||
1178 | return -1; | |
1179 | } | |
1180 | ||
1181 | static int create_new_reflog_file(const char *filepath) | |
1182 | { | |
1183 | int fd, error; | |
1184 | ||
1185 | if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0) | |
1186 | return error; | |
1187 | ||
1188 | if ((fd = p_open(filepath, | |
1189 | O_WRONLY | O_CREAT | O_TRUNC, | |
1190 | GIT_REFLOG_FILE_MODE)) < 0) | |
1191 | return -1; | |
1192 | ||
1193 | return p_close(fd); | |
1194 | } | |
1195 | ||
1196 | GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name) | |
1197 | { | |
1198 | return git_buf_join_n(path, '/', 3, repo->path_repository, GIT_REFLOG_DIR, name); | |
1199 | } | |
1200 | ||
1201 | static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name) | |
1202 | { | |
1203 | int error = -1; | |
1204 | git_buf log_path = GIT_BUF_INIT; | |
1205 | git_buf log_file = GIT_BUF_INIT; | |
1206 | git_reflog *log = NULL; | |
1207 | git_repository *repo; | |
1208 | refdb_fs_backend *backend; | |
1209 | ||
1210 | assert(out && _backend && name); | |
1211 | ||
1212 | backend = (refdb_fs_backend *) _backend; | |
1213 | repo = backend->repo; | |
1214 | ||
1215 | if (reflog_alloc(&log, name) < 0) | |
1216 | return -1; | |
1217 | ||
1218 | if (retrieve_reflog_path(&log_path, repo, name) < 0) | |
1219 | goto cleanup; | |
1220 | ||
1221 | error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path)); | |
1222 | if (error < 0 && error != GIT_ENOTFOUND) | |
1223 | goto cleanup; | |
1224 | ||
1225 | if ((error == GIT_ENOTFOUND) && | |
1226 | ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0)) | |
1227 | goto cleanup; | |
1228 | ||
1229 | if ((error = reflog_parse(log, | |
1230 | git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0) | |
1231 | goto cleanup; | |
1232 | ||
1233 | *out = log; | |
1234 | goto success; | |
1235 | ||
1236 | cleanup: | |
1237 | git_reflog_free(log); | |
1238 | ||
1239 | success: | |
1240 | git_buf_free(&log_file); | |
1241 | git_buf_free(&log_path); | |
1242 | ||
1243 | return error; | |
1244 | } | |
1245 | ||
1246 | static int serialize_reflog_entry( | |
1247 | git_buf *buf, | |
1248 | const git_oid *oid_old, | |
1249 | const git_oid *oid_new, | |
1250 | const git_signature *committer, | |
1251 | const char *msg) | |
1252 | { | |
1253 | char raw_old[GIT_OID_HEXSZ+1]; | |
1254 | char raw_new[GIT_OID_HEXSZ+1]; | |
1255 | ||
1256 | git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old); | |
1257 | git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new); | |
1258 | ||
1259 | git_buf_clear(buf); | |
1260 | ||
1261 | git_buf_puts(buf, raw_old); | |
1262 | git_buf_putc(buf, ' '); | |
1263 | git_buf_puts(buf, raw_new); | |
1264 | ||
1265 | git_signature__writebuf(buf, " ", committer); | |
1266 | ||
1267 | /* drop trailing LF */ | |
1268 | git_buf_rtrim(buf); | |
1269 | ||
1270 | if (msg) { | |
1271 | git_buf_putc(buf, '\t'); | |
1272 | git_buf_puts(buf, msg); | |
1273 | } | |
1274 | ||
1275 | git_buf_putc(buf, '\n'); | |
1276 | ||
1277 | return git_buf_oom(buf); | |
1278 | } | |
1279 | ||
1280 | static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog) | |
1281 | { | |
1282 | int error = -1; | |
1283 | unsigned int i; | |
1284 | git_reflog_entry *entry; | |
1285 | git_repository *repo; | |
1286 | refdb_fs_backend *backend; | |
1287 | git_buf log_path = GIT_BUF_INIT; | |
1288 | git_buf log = GIT_BUF_INIT; | |
1289 | git_filebuf fbuf = GIT_FILEBUF_INIT; | |
1290 | ||
1291 | assert(_backend && reflog); | |
1292 | ||
1293 | backend = (refdb_fs_backend *) _backend; | |
1294 | repo = backend->repo; | |
1295 | ||
1296 | if (retrieve_reflog_path(&log_path, repo, reflog->ref_name) < 0) | |
1297 | return -1; | |
1298 | ||
1299 | if (!git_path_isfile(git_buf_cstr(&log_path))) { | |
1300 | giterr_set(GITERR_INVALID, | |
1301 | "Log file for reference '%s' doesn't exist.", reflog->ref_name); | |
1302 | goto cleanup; | |
1303 | } | |
1304 | ||
1d3a8aeb | 1305 | if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE)) < 0) |
b976f3c2 CMN |
1306 | goto cleanup; |
1307 | ||
1308 | git_vector_foreach(&reflog->entries, i, entry) { | |
1309 | if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0) | |
1310 | goto cleanup; | |
1311 | ||
1312 | if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0) | |
1313 | goto cleanup; | |
1314 | } | |
1315 | ||
1d3a8aeb | 1316 | error = git_filebuf_commit(&fbuf); |
b976f3c2 CMN |
1317 | goto success; |
1318 | ||
1319 | cleanup: | |
1320 | git_filebuf_cleanup(&fbuf); | |
1321 | ||
1322 | success: | |
1323 | git_buf_free(&log); | |
1324 | git_buf_free(&log_path); | |
1325 | return error; | |
1326 | } | |
1327 | ||
b976f3c2 CMN |
1328 | static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name) |
1329 | { | |
1330 | int error = 0, fd; | |
1331 | git_buf old_path = GIT_BUF_INIT; | |
1332 | git_buf new_path = GIT_BUF_INIT; | |
1333 | git_buf temp_path = GIT_BUF_INIT; | |
1334 | git_buf normalized = GIT_BUF_INIT; | |
1335 | git_repository *repo; | |
1336 | refdb_fs_backend *backend; | |
1337 | ||
1338 | assert(_backend && old_name && new_name); | |
1339 | ||
1340 | backend = (refdb_fs_backend *) _backend; | |
1341 | repo = backend->repo; | |
1342 | ||
1343 | if ((error = git_reference__normalize_name( | |
1344 | &normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0) | |
1345 | return error; | |
1346 | ||
1347 | if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0) | |
1348 | return -1; | |
1349 | ||
1350 | if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0) | |
1351 | return -1; | |
1352 | ||
1353 | if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0) | |
1354 | return -1; | |
1355 | ||
1356 | /* | |
1357 | * Move the reflog to a temporary place. This two-phase renaming is required | |
1358 | * in order to cope with funny renaming use cases when one tries to move a reference | |
1359 | * to a partially colliding namespace: | |
1360 | * - a/b -> a/b/c | |
1361 | * - a/b/c/d -> a/b/c | |
1362 | */ | |
1363 | if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0) | |
1364 | return -1; | |
1365 | ||
1d3a8aeb | 1366 | if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) { |
b976f3c2 CMN |
1367 | error = -1; |
1368 | goto cleanup; | |
1369 | } | |
1370 | ||
1371 | p_close(fd); | |
1372 | ||
1373 | if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) { | |
1374 | giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); | |
1375 | error = -1; | |
1376 | goto cleanup; | |
1377 | } | |
1378 | ||
1379 | if (git_path_isdir(git_buf_cstr(&new_path)) && | |
1380 | (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) { | |
1381 | error = -1; | |
1382 | goto cleanup; | |
1383 | } | |
1384 | ||
1385 | if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) { | |
1386 | error = -1; | |
1387 | goto cleanup; | |
1388 | } | |
1389 | ||
1390 | if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) { | |
1391 | giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); | |
1392 | error = -1; | |
1393 | } | |
1394 | ||
1395 | cleanup: | |
1396 | git_buf_free(&temp_path); | |
1397 | git_buf_free(&old_path); | |
1398 | git_buf_free(&new_path); | |
1399 | git_buf_free(&normalized); | |
1400 | ||
1401 | return error; | |
1402 | } | |
1403 | ||
b976f3c2 CMN |
1404 | static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name) |
1405 | { | |
1406 | int error; | |
1407 | git_buf path = GIT_BUF_INIT; | |
1408 | ||
1409 | git_repository *repo; | |
1410 | refdb_fs_backend *backend; | |
1411 | ||
1412 | assert(_backend && name); | |
1413 | ||
1414 | backend = (refdb_fs_backend *) _backend; | |
1415 | repo = backend->repo; | |
1416 | ||
1417 | error = retrieve_reflog_path(&path, repo, name); | |
1418 | ||
1419 | if (!error && git_path_exists(path.ptr)) | |
1420 | error = p_unlink(path.ptr); | |
1421 | ||
1422 | git_buf_free(&path); | |
1423 | ||
1424 | return error; | |
1425 | ||
1426 | } | |
1427 | ||
d00d5464 ET |
1428 | int git_refdb_backend_fs( |
1429 | git_refdb_backend **backend_out, | |
4e4eab52 | 1430 | git_repository *repository) |
d00d5464 | 1431 | { |
219d3457 | 1432 | int t = 0; |
bade5194 | 1433 | git_buf path = GIT_BUF_INIT; |
d00d5464 ET |
1434 | refdb_fs_backend *backend; |
1435 | ||
1436 | backend = git__calloc(1, sizeof(refdb_fs_backend)); | |
1437 | GITERR_CHECK_ALLOC(backend); | |
1438 | ||
1439 | backend->repo = repository; | |
bade5194 | 1440 | |
fe372740 RB |
1441 | if (setup_namespace(&path, repository) < 0) |
1442 | goto fail; | |
bade5194 VM |
1443 | |
1444 | backend->path = git_buf_detach(&path); | |
d00d5464 | 1445 | |
fe372740 RB |
1446 | if (git_buf_joinpath(&path, backend->path, GIT_PACKEDREFS_FILE) < 0 || |
1447 | git_sortedcache_new( | |
1448 | &backend->refcache, offsetof(struct packref, name), | |
1449 | NULL, NULL, packref_cmp, git_buf_cstr(&path)) < 0) | |
1450 | goto fail; | |
1451 | ||
1452 | git_buf_free(&path); | |
1453 | ||
219d3457 RB |
1454 | if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) { |
1455 | backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE; | |
1456 | backend->direach_flags |= GIT_PATH_DIR_IGNORE_CASE; | |
1457 | } | |
1458 | if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_PRECOMPOSE) && t) { | |
1459 | backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE; | |
1460 | backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE; | |
1461 | } | |
1462 | ||
d00d5464 ET |
1463 | backend->parent.exists = &refdb_fs_backend__exists; |
1464 | backend->parent.lookup = &refdb_fs_backend__lookup; | |
4def7035 | 1465 | backend->parent.iterator = &refdb_fs_backend__iterator; |
d00d5464 | 1466 | backend->parent.write = &refdb_fs_backend__write; |
e3f3868a | 1467 | backend->parent.del = &refdb_fs_backend__delete; |
4e6e2ff2 | 1468 | backend->parent.rename = &refdb_fs_backend__rename; |
d00d5464 ET |
1469 | backend->parent.compress = &refdb_fs_backend__compress; |
1470 | backend->parent.free = &refdb_fs_backend__free; | |
b976f3c2 CMN |
1471 | backend->parent.reflog_read = &refdb_reflog_fs__read; |
1472 | backend->parent.reflog_write = &refdb_reflog_fs__write; | |
b976f3c2 | 1473 | backend->parent.reflog_rename = &refdb_reflog_fs__rename; |
b976f3c2 | 1474 | backend->parent.reflog_delete = &refdb_reflog_fs__delete; |
d00d5464 ET |
1475 | |
1476 | *backend_out = (git_refdb_backend *)backend; | |
1477 | return 0; | |
fe372740 RB |
1478 | |
1479 | fail: | |
1480 | git_buf_free(&path); | |
1481 | git__free(backend->path); | |
1482 | git__free(backend); | |
1483 | return -1; | |
d00d5464 | 1484 | } |