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