]>
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> | |
4b7e1b9e | 24 | #include <git2/branch.h> |
4dcd8780 | 25 | #include <git2/sys/refdb_backend.h> |
21ca0451 | 26 | #include <git2/sys/refs.h> |
b976f3c2 | 27 | #include <git2/sys/reflog.h> |
d00d5464 ET |
28 | |
29 | GIT__USE_STRMAP; | |
30 | ||
31 | #define DEFAULT_NESTING_LEVEL 5 | |
32 | #define MAX_NESTING_LEVEL 10 | |
33 | ||
34 | enum { | |
f69db390 VM |
35 | PACKREF_HAS_PEEL = 1, |
36 | PACKREF_WAS_LOOSE = 2, | |
2638a03a VM |
37 | PACKREF_CANNOT_PEEL = 4, |
38 | PACKREF_SHADOWED = 8, | |
f69db390 VM |
39 | }; |
40 | ||
41 | enum { | |
42 | PEELING_NONE = 0, | |
43 | PEELING_STANDARD, | |
44 | PEELING_FULL | |
d00d5464 ET |
45 | }; |
46 | ||
47 | struct packref { | |
48 | git_oid oid; | |
49 | git_oid peel; | |
50 | char flags; | |
51 | char name[GIT_FLEX_ARRAY]; | |
52 | }; | |
53 | ||
54 | typedef struct refdb_fs_backend { | |
55 | git_refdb_backend parent; | |
56 | ||
57 | git_repository *repo; | |
bade5194 | 58 | char *path; |
d00d5464 | 59 | |
fe372740 | 60 | git_sortedcache *refcache; |
f69db390 | 61 | int peeling_mode; |
219d3457 RB |
62 | git_iterator_flag_t iterator_flags; |
63 | uint32_t direach_flags; | |
d00d5464 ET |
64 | } refdb_fs_backend; |
65 | ||
fe372740 | 66 | static int packref_cmp(const void *a_, const void *b_) |
d00d5464 | 67 | { |
fe372740 RB |
68 | const struct packref *a = a_, *b = b_; |
69 | return strcmp(a->name, b->name); | |
d00d5464 ET |
70 | } |
71 | ||
fe372740 | 72 | static int packed_reload(refdb_fs_backend *backend) |
d00d5464 | 73 | { |
fe372740 RB |
74 | int error; |
75 | git_buf packedrefs = GIT_BUF_INIT; | |
76 | char *scan, *eof, *eol; | |
4dcd8780 | 77 | |
fe372740 | 78 | if (!backend->path) |
69a3c766 CMN |
79 | return 0; |
80 | ||
fe372740 | 81 | error = git_sortedcache_lockandload(backend->refcache, &packedrefs); |
d00d5464 ET |
82 | |
83 | /* | |
fe372740 RB |
84 | * If we can't find the packed-refs, clear table and return. |
85 | * Any other error just gets passed through. | |
86 | * If no error, and file wasn't changed, just return. | |
87 | * Anything else means we need to refresh the packed refs. | |
d00d5464 | 88 | */ |
fe372740 RB |
89 | if (error <= 0) { |
90 | if (error == GIT_ENOTFOUND) { | |
91 | git_sortedcache_clear(backend->refcache, true); | |
92 | giterr_clear(); | |
93 | error = 0; | |
94 | } | |
95 | return error; | |
d00d5464 ET |
96 | } |
97 | ||
fe372740 | 98 | /* At this point, refresh the packed refs from the loaded buffer. */ |
d00d5464 | 99 | |
fe372740 | 100 | git_sortedcache_clear(backend->refcache, false); |
d00d5464 | 101 | |
fe372740 RB |
102 | scan = (char *)packedrefs.ptr; |
103 | eof = scan + packedrefs.size; | |
d00d5464 | 104 | |
f69db390 VM |
105 | backend->peeling_mode = PEELING_NONE; |
106 | ||
fe372740 | 107 | if (*scan == '#') { |
1fed6b07 | 108 | static const char *traits_header = "# pack-refs with: "; |
f69db390 | 109 | |
fe372740 RB |
110 | if (git__prefixcmp(scan, traits_header) == 0) { |
111 | scan += strlen(traits_header); | |
112 | eol = strchr(scan, '\n'); | |
822645f6 | 113 | |
fe372740 | 114 | if (!eol) |
822645f6 | 115 | goto parse_failed; |
fe372740 | 116 | *eol = '\0'; |
822645f6 | 117 | |
fe372740 | 118 | if (strstr(scan, " fully-peeled ") != NULL) { |
f69db390 | 119 | backend->peeling_mode = PEELING_FULL; |
fe372740 | 120 | } else if (strstr(scan, " peeled ") != NULL) { |
f69db390 VM |
121 | backend->peeling_mode = PEELING_STANDARD; |
122 | } | |
123 | ||
fe372740 | 124 | scan = eol + 1; |
f69db390 VM |
125 | } |
126 | } | |
127 | ||
fe372740 RB |
128 | while (scan < eof && *scan == '#') { |
129 | if (!(eol = strchr(scan, '\n'))) | |
d00d5464 | 130 | goto parse_failed; |
fe372740 | 131 | scan = eol + 1; |
d00d5464 ET |
132 | } |
133 | ||
fe372740 RB |
134 | while (scan < eof) { |
135 | struct packref *ref; | |
136 | git_oid oid; | |
137 | ||
138 | /* parse "<OID> <refname>\n" */ | |
d00d5464 | 139 | |
fe372740 | 140 | if (git_oid_fromstr(&oid, scan) < 0) |
d00d5464 | 141 | goto parse_failed; |
fe372740 | 142 | scan += GIT_OID_HEXSZ; |
d00d5464 | 143 | |
fe372740 RB |
144 | if (*scan++ != ' ') |
145 | goto parse_failed; | |
146 | if (!(eol = strchr(scan, '\n'))) | |
147 | goto parse_failed; | |
148 | *eol = '\0'; | |
149 | if (eol[-1] == '\r') | |
150 | eol[-1] = '\0'; | |
d00d5464 | 151 | |
fe372740 | 152 | if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0) |
d00d5464 | 153 | goto parse_failed; |
fe372740 RB |
154 | scan = eol + 1; |
155 | ||
156 | git_oid_cpy(&ref->oid, &oid); | |
157 | ||
158 | /* look for optional "^<OID>\n" */ | |
159 | ||
160 | if (*scan == '^') { | |
161 | if (git_oid_fromstr(&oid, scan + 1) < 0) | |
162 | goto parse_failed; | |
163 | scan += GIT_OID_HEXSZ + 1; | |
164 | ||
165 | if (scan < eof) { | |
166 | if (!(eol = strchr(scan, '\n'))) | |
167 | goto parse_failed; | |
168 | scan = eol + 1; | |
169 | } | |
170 | ||
171 | git_oid_cpy(&ref->peel, &oid); | |
172 | ref->flags |= PACKREF_HAS_PEEL; | |
173 | } | |
174 | else if (backend->peeling_mode == PEELING_FULL || | |
175 | (backend->peeling_mode == PEELING_STANDARD && | |
176 | git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0)) | |
177 | ref->flags |= PACKREF_CANNOT_PEEL; | |
d00d5464 ET |
178 | } |
179 | ||
8d9a85d4 | 180 | git_sortedcache_wunlock(backend->refcache); |
fe372740 RB |
181 | git_buf_free(&packedrefs); |
182 | ||
d00d5464 ET |
183 | return 0; |
184 | ||
185 | parse_failed: | |
fe372740 RB |
186 | giterr_set(GITERR_REFERENCE, "Corrupted packed references file"); |
187 | ||
188 | git_sortedcache_clear(backend->refcache, false); | |
8d9a85d4 | 189 | git_sortedcache_wunlock(backend->refcache); |
fe372740 RB |
190 | git_buf_free(&packedrefs); |
191 | ||
d00d5464 ET |
192 | return -1; |
193 | } | |
194 | ||
fe372740 RB |
195 | static int loose_parse_oid( |
196 | git_oid *oid, const char *filename, git_buf *file_content) | |
d00d5464 | 197 | { |
fe372740 | 198 | const char *str = git_buf_cstr(file_content); |
d00d5464 | 199 | |
fe372740 | 200 | if (git_buf_len(file_content) < GIT_OID_HEXSZ) |
d00d5464 ET |
201 | goto corrupted; |
202 | ||
d00d5464 | 203 | /* we need to get 40 OID characters from the file */ |
fe372740 | 204 | if (git_oid_fromstr(oid, str) < 0) |
d00d5464 ET |
205 | goto corrupted; |
206 | ||
207 | /* If the file is longer than 40 chars, the 41st must be a space */ | |
208 | str += GIT_OID_HEXSZ; | |
209 | if (*str == '\0' || git__isspace(*str)) | |
210 | return 0; | |
211 | ||
212 | corrupted: | |
a5de9044 | 213 | giterr_set(GITERR_REFERENCE, "Corrupted loose reference file: %s", filename); |
d00d5464 ET |
214 | return -1; |
215 | } | |
216 | ||
fe372740 RB |
217 | static int loose_readbuffer(git_buf *buf, const char *base, const char *path) |
218 | { | |
219 | int error; | |
220 | ||
221 | /* build full path to file */ | |
222 | if ((error = git_buf_joinpath(buf, base, path)) < 0 || | |
223 | (error = git_futils_readbuffer(buf, buf->ptr)) < 0) | |
224 | git_buf_free(buf); | |
225 | ||
226 | return error; | |
227 | } | |
228 | ||
229 | static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name) | |
d00d5464 | 230 | { |
fe372740 | 231 | int error = 0; |
d00d5464 ET |
232 | git_buf ref_file = GIT_BUF_INIT; |
233 | struct packref *ref = NULL; | |
fe372740 | 234 | git_oid oid; |
d00d5464 | 235 | |
fe372740 RB |
236 | /* if we fail to load the loose reference, assume someone changed |
237 | * the filesystem under us and skip it... | |
238 | */ | |
239 | if (loose_readbuffer(&ref_file, backend->path, name) < 0) { | |
240 | giterr_clear(); | |
241 | goto done; | |
242 | } | |
d00d5464 | 243 | |
0f0f5655 | 244 | /* skip symbolic refs */ |
fe372740 RB |
245 | if (!git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF)) |
246 | goto done; | |
0f0f5655 | 247 | |
fe372740 RB |
248 | /* parse OID from file */ |
249 | if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0) | |
250 | goto done; | |
d00d5464 | 251 | |
8d9a85d4 | 252 | git_sortedcache_wlock(backend->refcache); |
d00d5464 | 253 | |
fe372740 RB |
254 | if (!(error = git_sortedcache_upsert( |
255 | (void **)&ref, backend->refcache, name))) { | |
d00d5464 | 256 | |
fe372740 RB |
257 | git_oid_cpy(&ref->oid, &oid); |
258 | ref->flags = PACKREF_WAS_LOOSE; | |
d00d5464 ET |
259 | } |
260 | ||
8d9a85d4 | 261 | git_sortedcache_wunlock(backend->refcache); |
d00d5464 | 262 | |
fe372740 | 263 | done: |
d00d5464 | 264 | git_buf_free(&ref_file); |
fe372740 | 265 | return error; |
d00d5464 ET |
266 | } |
267 | ||
25e0b157 | 268 | static int _dirent_loose_load(void *payload, git_buf *full_path) |
d00d5464 | 269 | { |
25e0b157 | 270 | refdb_fs_backend *backend = payload; |
d00d5464 | 271 | const char *file_path; |
d00d5464 | 272 | |
fe372740 RB |
273 | if (git__suffixcmp(full_path->ptr, ".lock") == 0) |
274 | return 0; | |
275 | ||
a213a7bf CMN |
276 | if (git_path_isdir(full_path->ptr)) { |
277 | int error = git_path_direach( | |
25e0b157 | 278 | full_path, backend->direach_flags, _dirent_loose_load, backend); |
a213a7bf CMN |
279 | /* Race with the filesystem, ignore it */ |
280 | if (error == GIT_ENOTFOUND) { | |
281 | giterr_clear(); | |
282 | return 0; | |
283 | } | |
284 | ||
285 | return error; | |
286 | } | |
d00d5464 | 287 | |
25e0b157 | 288 | file_path = full_path->ptr + strlen(backend->path); |
d00d5464 | 289 | |
25e0b157 | 290 | return loose_lookup_to_packfile(backend, file_path); |
d00d5464 ET |
291 | } |
292 | ||
293 | /* | |
294 | * Load all the loose references from the repository | |
295 | * into the in-memory Packfile, and build a vector with | |
296 | * all the references so it can be written back to | |
297 | * disk. | |
298 | */ | |
299 | static int packed_loadloose(refdb_fs_backend *backend) | |
300 | { | |
fe372740 | 301 | int error; |
d00d5464 | 302 | git_buf refs_path = GIT_BUF_INIT; |
d00d5464 ET |
303 | |
304 | if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0) | |
305 | return -1; | |
306 | ||
307 | /* | |
308 | * Load all the loose files from disk into the Packfile table. | |
309 | * This will overwrite any old packed entries with their | |
310 | * updated loose versions | |
311 | */ | |
219d3457 | 312 | error = git_path_direach( |
25e0b157 | 313 | &refs_path, backend->direach_flags, _dirent_loose_load, backend); |
fe372740 | 314 | |
d00d5464 ET |
315 | git_buf_free(&refs_path); |
316 | ||
96869a4e | 317 | return error; |
d00d5464 ET |
318 | } |
319 | ||
320 | static int refdb_fs_backend__exists( | |
321 | int *exists, | |
322 | git_refdb_backend *_backend, | |
323 | const char *ref_name) | |
324 | { | |
fe372740 | 325 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 ET |
326 | git_buf ref_path = GIT_BUF_INIT; |
327 | ||
fe372740 | 328 | assert(backend); |
d00d5464 | 329 | |
fe372740 RB |
330 | if (packed_reload(backend) < 0 || |
331 | git_buf_joinpath(&ref_path, backend->path, ref_name) < 0) | |
d00d5464 ET |
332 | return -1; |
333 | ||
fe372740 | 334 | *exists = git_path_isfile(ref_path.ptr) || |
8d9a85d4 | 335 | (git_sortedcache_lookup(backend->refcache, ref_name) != NULL); |
d00d5464 ET |
336 | |
337 | git_buf_free(&ref_path); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | static const char *loose_parse_symbolic(git_buf *file_content) | |
342 | { | |
343 | const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF); | |
344 | const char *refname_start; | |
345 | ||
346 | refname_start = (const char *)file_content->ptr; | |
347 | ||
348 | if (git_buf_len(file_content) < header_len + 1) { | |
349 | giterr_set(GITERR_REFERENCE, "Corrupted loose reference file"); | |
350 | return NULL; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Assume we have already checked for the header | |
355 | * before calling this function | |
356 | */ | |
357 | refname_start += header_len; | |
358 | ||
359 | return refname_start; | |
360 | } | |
361 | ||
362 | static int loose_lookup( | |
363 | git_reference **out, | |
364 | refdb_fs_backend *backend, | |
365 | const char *ref_name) | |
366 | { | |
d00d5464 ET |
367 | git_buf ref_file = GIT_BUF_INIT; |
368 | int error = 0; | |
369 | ||
b7107131 RB |
370 | if (out) |
371 | *out = NULL; | |
372 | ||
fe372740 RB |
373 | if ((error = loose_readbuffer(&ref_file, backend->path, ref_name)) < 0) |
374 | /* cannot read loose ref file - gah */; | |
375 | else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) { | |
376 | const char *target; | |
d00d5464 | 377 | |
d00d5464 ET |
378 | git_buf_rtrim(&ref_file); |
379 | ||
fe372740 | 380 | if (!(target = loose_parse_symbolic(&ref_file))) |
d00d5464 | 381 | error = -1; |
fe372740 | 382 | else if (out != NULL) |
b7107131 | 383 | *out = git_reference__alloc_symbolic(ref_name, target); |
d00d5464 | 384 | } else { |
fe372740 | 385 | git_oid oid; |
4dcd8780 | 386 | |
fe372740 RB |
387 | if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) && |
388 | out != NULL) | |
b7107131 | 389 | *out = git_reference__alloc(ref_name, &oid, NULL); |
d00d5464 ET |
390 | } |
391 | ||
d00d5464 ET |
392 | git_buf_free(&ref_file); |
393 | return error; | |
394 | } | |
395 | ||
fe372740 | 396 | static int ref_error_notfound(const char *name) |
d00d5464 | 397 | { |
fe372740 RB |
398 | giterr_set(GITERR_REFERENCE, "Reference '%s' not found", name); |
399 | return GIT_ENOTFOUND; | |
d00d5464 ET |
400 | } |
401 | ||
402 | static int packed_lookup( | |
403 | git_reference **out, | |
404 | refdb_fs_backend *backend, | |
405 | const char *ref_name) | |
406 | { | |
d00d5464 | 407 | int error = 0; |
fe372740 | 408 | struct packref *entry; |
4dcd8780 | 409 | |
fe372740 | 410 | if (packed_reload(backend) < 0) |
d00d5464 | 411 | return -1; |
4dcd8780 | 412 | |
8d9a85d4 RB |
413 | if (git_sortedcache_rlock(backend->refcache) < 0) |
414 | return -1; | |
fe372740 RB |
415 | |
416 | entry = git_sortedcache_lookup(backend->refcache, ref_name); | |
417 | if (!entry) { | |
418 | error = ref_error_notfound(ref_name); | |
419 | } else { | |
420 | *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel); | |
421 | if (!*out) | |
422 | error = -1; | |
423 | } | |
424 | ||
8d9a85d4 RB |
425 | git_sortedcache_runlock(backend->refcache); |
426 | ||
fe372740 | 427 | return error; |
d00d5464 ET |
428 | } |
429 | ||
430 | static int refdb_fs_backend__lookup( | |
431 | git_reference **out, | |
432 | git_refdb_backend *_backend, | |
433 | const char *ref_name) | |
434 | { | |
fe372740 RB |
435 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
436 | int error; | |
d00d5464 | 437 | |
fe372740 | 438 | assert(backend); |
d00d5464 | 439 | |
fe372740 | 440 | if (!(error = loose_lookup(out, backend, ref_name))) |
d00d5464 ET |
441 | return 0; |
442 | ||
443 | /* only try to lookup this reference on the packfile if it | |
444 | * wasn't found on the loose refs; not if there was a critical error */ | |
fe372740 | 445 | if (error == GIT_ENOTFOUND) { |
d00d5464 | 446 | giterr_clear(); |
fe372740 | 447 | error = packed_lookup(out, backend, ref_name); |
d00d5464 ET |
448 | } |
449 | ||
fe372740 | 450 | return error; |
d00d5464 ET |
451 | } |
452 | ||
4def7035 CMN |
453 | typedef struct { |
454 | git_reference_iterator parent; | |
2638a03a | 455 | |
ec24e542 | 456 | char *glob; |
c77342ef RB |
457 | |
458 | git_pool pool; | |
2638a03a | 459 | git_vector loose; |
c77342ef | 460 | |
2d945f82 | 461 | git_sortedcache *cache; |
fe372740 RB |
462 | size_t loose_pos; |
463 | size_t packed_pos; | |
4def7035 CMN |
464 | } refdb_fs_iter; |
465 | ||
4def7035 | 466 | static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter) |
d00d5464 | 467 | { |
4def7035 | 468 | refdb_fs_iter *iter = (refdb_fs_iter *) _iter; |
2638a03a VM |
469 | |
470 | git_vector_free(&iter->loose); | |
c77342ef | 471 | git_pool_clear(&iter->pool); |
2d945f82 | 472 | git_sortedcache_free(iter->cache); |
4def7035 CMN |
473 | git__free(iter); |
474 | } | |
d00d5464 | 475 | |
ec24e542 | 476 | static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter) |
4def7035 | 477 | { |
219d3457 | 478 | int error = 0; |
2638a03a | 479 | git_buf path = GIT_BUF_INIT; |
c77342ef | 480 | git_iterator *fsit = NULL; |
2638a03a | 481 | const git_index_entry *entry = NULL; |
d00d5464 | 482 | |
cee695ae RB |
483 | if (!backend->path) /* do nothing if no path for loose refs */ |
484 | return 0; | |
485 | ||
fe372740 RB |
486 | if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 || |
487 | (error = git_iterator_for_filesystem( | |
219d3457 | 488 | &fsit, path.ptr, backend->iterator_flags, NULL, NULL)) < 0) { |
fe372740 RB |
489 | git_buf_free(&path); |
490 | return error; | |
491 | } | |
d00d5464 | 492 | |
fe372740 | 493 | error = git_buf_sets(&path, GIT_REFS_DIR); |
d00d5464 | 494 | |
fe372740 | 495 | while (!error && !git_iterator_advance(&entry, fsit)) { |
2638a03a | 496 | const char *ref_name; |
fe372740 | 497 | struct packref *ref; |
c77342ef | 498 | char *ref_dup; |
d00d5464 | 499 | |
2638a03a VM |
500 | git_buf_truncate(&path, strlen(GIT_REFS_DIR)); |
501 | git_buf_puts(&path, entry->path); | |
502 | ref_name = git_buf_cstr(&path); | |
4dcd8780 | 503 | |
ec24e542 | 504 | if (git__suffixcmp(ref_name, ".lock") == 0 || |
cee695ae | 505 | (iter->glob && p_fnmatch(iter->glob, ref_name, 0) != 0)) |
2638a03a | 506 | continue; |
d00d5464 | 507 | |
8d9a85d4 | 508 | git_sortedcache_rlock(backend->refcache); |
fe372740 RB |
509 | ref = git_sortedcache_lookup(backend->refcache, ref_name); |
510 | if (ref) | |
2638a03a | 511 | ref->flags |= PACKREF_SHADOWED; |
8d9a85d4 | 512 | git_sortedcache_runlock(backend->refcache); |
d00d5464 | 513 | |
fe372740 RB |
514 | ref_dup = git_pool_strdup(&iter->pool, ref_name); |
515 | if (!ref_dup) | |
c77342ef | 516 | error = -1; |
fe372740 RB |
517 | else |
518 | error = git_vector_insert(&iter->loose, ref_dup); | |
2638a03a | 519 | } |
d00d5464 | 520 | |
2638a03a VM |
521 | git_iterator_free(fsit); |
522 | git_buf_free(&path); | |
4def7035 | 523 | |
fe372740 | 524 | return error; |
4def7035 CMN |
525 | } |
526 | ||
ec24e542 VM |
527 | static int refdb_fs_backend__iterator_next( |
528 | git_reference **out, git_reference_iterator *_iter) | |
4def7035 | 529 | { |
8d9a85d4 | 530 | int error = GIT_ITEROVER; |
2638a03a | 531 | refdb_fs_iter *iter = (refdb_fs_iter *)_iter; |
ec24e542 | 532 | refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend; |
fe372740 | 533 | struct packref *ref; |
4def7035 | 534 | |
56960b83 | 535 | while (iter->loose_pos < iter->loose.length) { |
2638a03a | 536 | const char *path = git_vector_get(&iter->loose, iter->loose_pos++); |
56960b83 VM |
537 | |
538 | if (loose_lookup(out, backend, path) == 0) | |
539 | return 0; | |
540 | ||
541 | giterr_clear(); | |
2638a03a | 542 | } |
d00d5464 | 543 | |
2d945f82 CMN |
544 | if (!iter->cache) { |
545 | if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0) | |
546 | return error; | |
547 | } | |
d00d5464 | 548 | |
2d945f82 CMN |
549 | error = GIT_ITEROVER; |
550 | while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { | |
551 | ref = git_sortedcache_entry(iter->cache, iter->packed_pos++); | |
fe372740 RB |
552 | if (!ref) /* stop now if another thread deleted refs and we past end */ |
553 | break; | |
ec24e542 VM |
554 | |
555 | if (ref->flags & PACKREF_SHADOWED) | |
556 | continue; | |
ec24e542 VM |
557 | if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0) |
558 | continue; | |
99d32707 | 559 | |
56960b83 | 560 | *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel); |
8d9a85d4 RB |
561 | error = (*out != NULL) ? 0 : -1; |
562 | break; | |
4def7035 CMN |
563 | } |
564 | ||
8d9a85d4 | 565 | return error; |
d00d5464 ET |
566 | } |
567 | ||
ec24e542 VM |
568 | static int refdb_fs_backend__iterator_next_name( |
569 | const char **out, git_reference_iterator *_iter) | |
570 | { | |
8d9a85d4 | 571 | int error = GIT_ITEROVER; |
ec24e542 VM |
572 | refdb_fs_iter *iter = (refdb_fs_iter *)_iter; |
573 | refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend; | |
8d9a85d4 | 574 | struct packref *ref; |
ec24e542 VM |
575 | |
576 | while (iter->loose_pos < iter->loose.length) { | |
577 | const char *path = git_vector_get(&iter->loose, iter->loose_pos++); | |
578 | ||
8d9a85d4 RB |
579 | if (loose_lookup(NULL, backend, path) == 0) { |
580 | *out = path; | |
581 | return 0; | |
b7107131 RB |
582 | } |
583 | ||
8d9a85d4 | 584 | giterr_clear(); |
ec24e542 VM |
585 | } |
586 | ||
2d945f82 CMN |
587 | if (!iter->cache) { |
588 | if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0) | |
589 | return error; | |
590 | } | |
fe372740 | 591 | |
2d945f82 CMN |
592 | error = GIT_ITEROVER; |
593 | while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { | |
594 | ref = git_sortedcache_entry(iter->cache, iter->packed_pos++); | |
8d9a85d4 RB |
595 | if (!ref) /* stop now if another thread deleted refs and we past end */ |
596 | break; | |
fe372740 RB |
597 | |
598 | if (ref->flags & PACKREF_SHADOWED) | |
599 | continue; | |
8d9a85d4 | 600 | if (iter->glob && p_fnmatch(iter->glob, ref->name, 0) != 0) |
ec24e542 VM |
601 | continue; |
602 | ||
8d9a85d4 RB |
603 | *out = ref->name; |
604 | error = 0; | |
605 | break; | |
ec24e542 VM |
606 | } |
607 | ||
8d9a85d4 | 608 | return error; |
ec24e542 VM |
609 | } |
610 | ||
611 | static int refdb_fs_backend__iterator( | |
612 | git_reference_iterator **out, git_refdb_backend *_backend, const char *glob) | |
613 | { | |
614 | refdb_fs_iter *iter; | |
fe372740 | 615 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
ec24e542 | 616 | |
fe372740 | 617 | assert(backend); |
ec24e542 | 618 | |
fe372740 | 619 | if (packed_reload(backend) < 0) |
ec24e542 VM |
620 | return -1; |
621 | ||
622 | iter = git__calloc(1, sizeof(refdb_fs_iter)); | |
623 | GITERR_CHECK_ALLOC(iter); | |
624 | ||
fe372740 RB |
625 | if (git_pool_init(&iter->pool, 1, 0) < 0 || |
626 | git_vector_init(&iter->loose, 8, NULL) < 0) | |
c77342ef RB |
627 | goto fail; |
628 | ||
629 | if (glob != NULL && | |
630 | (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) | |
631 | goto fail; | |
ec24e542 VM |
632 | |
633 | iter->parent.next = refdb_fs_backend__iterator_next; | |
634 | iter->parent.next_name = refdb_fs_backend__iterator_next_name; | |
635 | iter->parent.free = refdb_fs_backend__iterator_free; | |
636 | ||
c77342ef RB |
637 | if (iter_load_loose_paths(backend, iter) < 0) |
638 | goto fail; | |
ec24e542 VM |
639 | |
640 | *out = (git_reference_iterator *)iter; | |
641 | return 0; | |
c77342ef RB |
642 | |
643 | fail: | |
644 | refdb_fs_backend__iterator_free((git_reference_iterator *)iter); | |
645 | return -1; | |
ec24e542 VM |
646 | } |
647 | ||
4e6e2ff2 VM |
648 | static bool ref_is_available( |
649 | const char *old_ref, const char *new_ref, const char *this_ref) | |
650 | { | |
651 | if (old_ref == NULL || strcmp(old_ref, this_ref)) { | |
652 | size_t reflen = strlen(this_ref); | |
653 | size_t newlen = strlen(new_ref); | |
654 | size_t cmplen = reflen < newlen ? reflen : newlen; | |
655 | const char *lead = reflen < newlen ? new_ref : this_ref; | |
656 | ||
657 | if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') { | |
658 | return false; | |
659 | } | |
660 | } | |
661 | ||
662 | return true; | |
663 | } | |
664 | ||
665 | static int reference_path_available( | |
666 | refdb_fs_backend *backend, | |
667 | const char *new_ref, | |
668 | const char* old_ref, | |
669 | int force) | |
670 | { | |
fe372740 | 671 | size_t i; |
4e6e2ff2 | 672 | |
fe372740 | 673 | if (packed_reload(backend) < 0) |
4e6e2ff2 VM |
674 | return -1; |
675 | ||
676 | if (!force) { | |
677 | int exists; | |
678 | ||
fe372740 RB |
679 | if (refdb_fs_backend__exists( |
680 | &exists, (git_refdb_backend *)backend, new_ref) < 0) | |
4e6e2ff2 VM |
681 | return -1; |
682 | ||
683 | if (exists) { | |
684 | giterr_set(GITERR_REFERENCE, | |
685 | "Failed to write reference '%s': a reference with " | |
8d9a85d4 | 686 | "that name already exists.", new_ref); |
4e6e2ff2 VM |
687 | return GIT_EEXISTS; |
688 | } | |
689 | } | |
690 | ||
8d9a85d4 | 691 | git_sortedcache_rlock(backend->refcache); |
fe372740 RB |
692 | |
693 | for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { | |
8d9a85d4 | 694 | struct packref *ref = git_sortedcache_entry(backend->refcache, i); |
fe372740 | 695 | |
8d9a85d4 RB |
696 | if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { |
697 | git_sortedcache_runlock(backend->refcache); | |
4e6e2ff2 | 698 | giterr_set(GITERR_REFERENCE, |
8d9a85d4 | 699 | "Path to reference '%s' collides with existing one", new_ref); |
4e6e2ff2 VM |
700 | return -1; |
701 | } | |
fe372740 | 702 | } |
c77342ef | 703 | |
8d9a85d4 | 704 | git_sortedcache_runlock(backend->refcache); |
4e6e2ff2 VM |
705 | return 0; |
706 | } | |
ec24e542 | 707 | |
7ee8c7e6 | 708 | static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name) |
d00d5464 | 709 | { |
9b148098 | 710 | int error; |
d00d5464 ET |
711 | git_buf ref_path = GIT_BUF_INIT; |
712 | ||
7ee8c7e6 | 713 | assert(file && backend && name); |
9b148098 | 714 | |
d00d5464 ET |
715 | /* Remove a possibly existing empty directory hierarchy |
716 | * which name would collide with the reference name | |
717 | */ | |
7ee8c7e6 | 718 | if (git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0) |
d00d5464 ET |
719 | return -1; |
720 | ||
7ee8c7e6 | 721 | if (git_buf_joinpath(&ref_path, backend->path, name) < 0) |
d00d5464 ET |
722 | return -1; |
723 | ||
9b148098 | 724 | error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE); |
d00d5464 ET |
725 | |
726 | git_buf_free(&ref_path); | |
9b148098 | 727 | return error; |
a57dd3b7 | 728 | } |
d00d5464 | 729 | |
a57dd3b7 CMN |
730 | static int loose_commit(git_filebuf *file, const git_reference *ref) |
731 | { | |
9b148098 CMN |
732 | assert(file && ref); |
733 | ||
d00d5464 ET |
734 | if (ref->type == GIT_REF_OID) { |
735 | char oid[GIT_OID_HEXSZ + 1]; | |
fe372740 | 736 | git_oid_nfmt(oid, sizeof(oid), &ref->target.oid); |
d00d5464 | 737 | |
a57dd3b7 | 738 | git_filebuf_printf(file, "%s\n", oid); |
d00d5464 | 739 | } else if (ref->type == GIT_REF_SYMBOLIC) { |
a57dd3b7 | 740 | git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic); |
d00d5464 ET |
741 | } else { |
742 | assert(0); /* don't let this happen */ | |
743 | } | |
744 | ||
a57dd3b7 | 745 | return git_filebuf_commit(file); |
d00d5464 ET |
746 | } |
747 | ||
ab8d9242 CMN |
748 | static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname) |
749 | { | |
750 | int error; | |
751 | git_filebuf *lock; | |
752 | refdb_fs_backend *backend = (refdb_fs_backend *) _backend; | |
753 | ||
754 | lock = git__calloc(1, sizeof(git_filebuf)); | |
755 | GITERR_CHECK_ALLOC(lock); | |
756 | ||
757 | if ((error = loose_lock(lock, backend, refname)) < 0) { | |
758 | git__free(lock); | |
759 | return error; | |
760 | } | |
761 | ||
762 | *out = lock; | |
763 | return 0; | |
764 | } | |
765 | ||
766 | static int refdb_fs_backend__write_tail( | |
767 | git_refdb_backend *_backend, | |
768 | const git_reference *ref, | |
769 | git_filebuf *file, | |
770 | int update_reflog, | |
771 | const git_signature *who, | |
772 | const char *message, | |
773 | const git_oid *old_id, | |
774 | const char *old_target); | |
775 | ||
776 | static int refdb_fs_backend__delete_tail( | |
777 | git_refdb_backend *_backend, | |
778 | git_filebuf *file, | |
779 | const char *ref_name, | |
780 | const git_oid *old_id, const char *old_target); | |
781 | ||
782 | static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog, | |
783 | const git_reference *ref, const git_signature *sig, const char *message) | |
784 | { | |
785 | git_filebuf *lock = (git_filebuf *) payload; | |
786 | int error = 0; | |
787 | ||
788 | if (success == 2) | |
789 | error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL); | |
790 | else if (success) | |
791 | error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, sig, message, NULL, NULL); | |
792 | else | |
793 | git_filebuf_cleanup(lock); | |
794 | ||
795 | git__free(lock); | |
796 | return error; | |
797 | } | |
798 | ||
d00d5464 ET |
799 | /* |
800 | * Find out what object this reference resolves to. | |
801 | * | |
802 | * For references that point to a 'big' tag (e.g. an | |
803 | * actual tag object on the repository), we need to | |
804 | * cache on the packfile the OID of the object to | |
805 | * which that 'big tag' is pointing to. | |
806 | */ | |
807 | static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref) | |
808 | { | |
809 | git_object *object; | |
810 | ||
f69db390 | 811 | if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL) |
d00d5464 ET |
812 | return 0; |
813 | ||
d00d5464 ET |
814 | /* |
815 | * Find the tagged object in the repository | |
816 | */ | |
817 | if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJ_ANY) < 0) | |
818 | return -1; | |
819 | ||
820 | /* | |
821 | * If the tagged object is a Tag object, we need to resolve it; | |
822 | * if the ref is actually a 'weak' ref, we don't need to resolve | |
823 | * anything. | |
824 | */ | |
825 | if (git_object_type(object) == GIT_OBJ_TAG) { | |
826 | git_tag *tag = (git_tag *)object; | |
827 | ||
828 | /* | |
829 | * Find the object pointed at by this tag | |
830 | */ | |
831 | git_oid_cpy(&ref->peel, git_tag_target_id(tag)); | |
f69db390 | 832 | ref->flags |= PACKREF_HAS_PEEL; |
d00d5464 ET |
833 | |
834 | /* | |
835 | * The reference has now cached the resolved OID, and is | |
836 | * marked at such. When written to the packfile, it'll be | |
837 | * accompanied by this resolved oid | |
838 | */ | |
839 | } | |
840 | ||
841 | git_object_free(object); | |
842 | return 0; | |
843 | } | |
844 | ||
845 | /* | |
846 | * Write a single reference into a packfile | |
847 | */ | |
848 | static int packed_write_ref(struct packref *ref, git_filebuf *file) | |
849 | { | |
850 | char oid[GIT_OID_HEXSZ + 1]; | |
fe372740 | 851 | git_oid_nfmt(oid, sizeof(oid), &ref->oid); |
d00d5464 ET |
852 | |
853 | /* | |
854 | * For references that peel to an object in the repo, we must | |
855 | * write the resulting peel on a separate line, e.g. | |
856 | * | |
857 | * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4 | |
858 | * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100 | |
859 | * | |
860 | * This obviously only applies to tags. | |
861 | * The required peels have already been loaded into `ref->peel_target`. | |
862 | */ | |
f69db390 | 863 | if (ref->flags & PACKREF_HAS_PEEL) { |
d00d5464 | 864 | char peel[GIT_OID_HEXSZ + 1]; |
fe372740 | 865 | git_oid_nfmt(peel, sizeof(peel), &ref->peel); |
d00d5464 ET |
866 | |
867 | if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0) | |
868 | return -1; | |
869 | } else { | |
870 | if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0) | |
871 | return -1; | |
872 | } | |
873 | ||
874 | return 0; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Remove all loose references | |
879 | * | |
880 | * Once we have successfully written a packfile, | |
881 | * all the loose references that were packed must be | |
882 | * removed from disk. | |
883 | * | |
884 | * This is a dangerous method; make sure the packfile | |
885 | * is well-written, because we are destructing references | |
886 | * here otherwise. | |
887 | */ | |
fe372740 | 888 | static int packed_remove_loose(refdb_fs_backend *backend) |
d00d5464 | 889 | { |
10c06114 | 890 | size_t i; |
d00d5464 ET |
891 | git_buf full_path = GIT_BUF_INIT; |
892 | int failed = 0; | |
893 | ||
fe372740 RB |
894 | /* backend->refcache is already locked when this is called */ |
895 | ||
896 | for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { | |
897 | struct packref *ref = git_sortedcache_entry(backend->refcache, i); | |
d00d5464 | 898 | |
8d9a85d4 | 899 | if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) |
d00d5464 ET |
900 | continue; |
901 | ||
902 | if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0) | |
903 | return -1; /* critical; do not try to recover on oom */ | |
904 | ||
fe372740 | 905 | if (git_path_exists(full_path.ptr) && p_unlink(full_path.ptr) < 0) { |
d00d5464 ET |
906 | if (failed) |
907 | continue; | |
908 | ||
909 | giterr_set(GITERR_REFERENCE, | |
910 | "Failed to remove loose reference '%s' after packing: %s", | |
911 | full_path.ptr, strerror(errno)); | |
d00d5464 ET |
912 | failed = 1; |
913 | } | |
914 | ||
915 | /* | |
916 | * if we fail to remove a single file, this is *not* good, | |
917 | * but we should keep going and remove as many as possible. | |
918 | * After we've removed as many files as possible, we return | |
919 | * the error code anyway. | |
920 | */ | |
921 | } | |
922 | ||
923 | git_buf_free(&full_path); | |
924 | return failed ? -1 : 0; | |
925 | } | |
926 | ||
927 | /* | |
928 | * Write all the contents in the in-memory packfile to disk. | |
929 | */ | |
930 | static int packed_write(refdb_fs_backend *backend) | |
931 | { | |
8d9a85d4 | 932 | git_sortedcache *refcache = backend->refcache; |
d00d5464 | 933 | git_filebuf pack_file = GIT_FILEBUF_INIT; |
10c06114 | 934 | size_t i; |
d00d5464 | 935 | |
fe372740 | 936 | /* lock the cache to updates while we do this */ |
8d9a85d4 | 937 | if (git_sortedcache_wlock(refcache) < 0) |
d00d5464 ET |
938 | return -1; |
939 | ||
fe372740 | 940 | /* Open the file! */ |
1d3a8aeb | 941 | if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0) |
8d9a85d4 | 942 | goto fail; |
d00d5464 ET |
943 | |
944 | /* Packfiles have a header... apparently | |
945 | * This is in fact not required, but we might as well print it | |
946 | * just for kicks */ | |
947 | if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0) | |
8d9a85d4 | 948 | goto fail; |
d00d5464 | 949 | |
8d9a85d4 RB |
950 | for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) { |
951 | struct packref *ref = git_sortedcache_entry(refcache, i); | |
d00d5464 ET |
952 | |
953 | if (packed_find_peel(backend, ref) < 0) | |
8d9a85d4 | 954 | goto fail; |
d00d5464 ET |
955 | |
956 | if (packed_write_ref(ref, &pack_file) < 0) | |
8d9a85d4 | 957 | goto fail; |
d00d5464 ET |
958 | } |
959 | ||
960 | /* if we've written all the references properly, we can commit | |
961 | * the packfile to make the changes effective */ | |
1d3a8aeb | 962 | if (git_filebuf_commit(&pack_file) < 0) |
8d9a85d4 | 963 | goto fail; |
d00d5464 ET |
964 | |
965 | /* when and only when the packfile has been properly written, | |
966 | * we can go ahead and remove the loose refs */ | |
fe372740 | 967 | if (packed_remove_loose(backend) < 0) |
8d9a85d4 | 968 | goto fail; |
d00d5464 | 969 | |
8d9a85d4 RB |
970 | git_sortedcache_updated(refcache); |
971 | git_sortedcache_wunlock(refcache); | |
d00d5464 ET |
972 | |
973 | /* we're good now */ | |
974 | return 0; | |
975 | ||
8d9a85d4 | 976 | fail: |
d00d5464 | 977 | git_filebuf_cleanup(&pack_file); |
8d9a85d4 | 978 | git_sortedcache_wunlock(refcache); |
d00d5464 ET |
979 | |
980 | return -1; | |
981 | } | |
982 | ||
4b7e1b9e | 983 | static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message); |
6f13a305 CMN |
984 | static int has_reflog(git_repository *repo, const char *name); |
985 | ||
986 | /* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */ | |
83504371 | 987 | static int should_write_reflog(int *write, git_repository *repo, const char *name) |
6f13a305 | 988 | { |
2b52a0bf | 989 | int error, logall; |
6f13a305 | 990 | |
2b52a0bf RB |
991 | error = git_repository__cvar(&logall, repo, GIT_CVAR_LOGALLREFUPDATES); |
992 | if (error < 0) | |
83504371 CMN |
993 | return error; |
994 | ||
2b52a0bf RB |
995 | /* Defaults to the opposite of the repo being bare */ |
996 | if (logall == GIT_LOGALLREFUPDATES_UNSET) | |
997 | logall = !git_repository_is_bare(repo); | |
83504371 CMN |
998 | |
999 | if (!logall) { | |
1000 | *write = 0; | |
1001 | } else if (has_reflog(repo, name)) { | |
1002 | *write = 1; | |
1003 | } else if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR) || | |
1004 | !git__strcmp(name, GIT_HEAD_FILE) || | |
1005 | !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) || | |
1006 | !git__prefixcmp(name, GIT_REFS_NOTES_DIR)) { | |
1007 | *write = 1; | |
1008 | } else { | |
1009 | *write = 0; | |
1010 | } | |
6f13a305 CMN |
1011 | |
1012 | return 0; | |
1013 | } | |
a57dd3b7 | 1014 | |
7ee8c7e6 | 1015 | static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name, |
f8621dde CMN |
1016 | const git_oid *old_id, const char *old_target) |
1017 | { | |
1018 | int error = 0; | |
1019 | git_reference *old_ref = NULL; | |
1020 | ||
1021 | *cmp = 0; | |
b7ae71ec CMN |
1022 | /* It "matches" if there is no old value to compare against */ |
1023 | if (!old_id && !old_target) | |
1024 | return 0; | |
1025 | ||
1026 | if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0) | |
1027 | goto out; | |
1028 | ||
1029 | /* If the types don't match, there's no way the values do */ | |
1030 | if (old_id && old_ref->type != GIT_REF_OID) { | |
1031 | *cmp = -1; | |
1032 | goto out; | |
1033 | } | |
1034 | if (old_target && old_ref->type != GIT_REF_SYMBOLIC) { | |
1035 | *cmp = 1; | |
1036 | goto out; | |
f8621dde CMN |
1037 | } |
1038 | ||
1039 | if (old_id && old_ref->type == GIT_REF_OID) | |
1040 | *cmp = git_oid_cmp(old_id, &old_ref->target.oid); | |
1041 | ||
1042 | if (old_target && old_ref->type == GIT_REF_SYMBOLIC) | |
1043 | *cmp = git__strcmp(old_target, old_ref->target.symbolic); | |
1044 | ||
1045 | out: | |
1046 | git_reference_free(old_ref); | |
1047 | ||
1048 | return error; | |
1049 | } | |
1050 | ||
4b7e1b9e CMN |
1051 | /* |
1052 | * The git.git comment regarding this, for your viewing pleasure: | |
1053 | * | |
1054 | * Special hack: If a branch is updated directly and HEAD | |
1055 | * points to it (may happen on the remote side of a push | |
1056 | * for example) then logically the HEAD reflog should be | |
1057 | * updated too. | |
1058 | * A generic solution implies reverse symref information, | |
1059 | * but finding all symrefs pointing to the given branch | |
1060 | * would be rather costly for this rare event (the direct | |
1061 | * update of a branch) to be worth it. So let's cheat and | |
1062 | * check with HEAD only which should cover 99% of all usage | |
1063 | * scenarios (even 100% of the default ones). | |
1064 | */ | |
1065 | static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message) | |
1066 | { | |
1067 | int error; | |
6aaae94a | 1068 | git_oid old_id = {{0}}; |
99797c96 CMN |
1069 | git_reference *tmp = NULL, *head = NULL, *peeled = NULL; |
1070 | const char *name; | |
1071 | ||
1072 | if (ref->type == GIT_REF_SYMBOLIC) | |
1073 | return 0; | |
4b7e1b9e | 1074 | |
6aaae94a CMN |
1075 | /* if we can't resolve, we use {0}*40 as old id */ |
1076 | git_reference_name_to_id(&old_id, backend->repo, ref->name); | |
4b7e1b9e | 1077 | |
99797c96 | 1078 | if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0) |
4b7e1b9e CMN |
1079 | return error; |
1080 | ||
6aaae94a CMN |
1081 | if (git_reference_type(head) == GIT_REF_OID) |
1082 | goto cleanup; | |
1083 | ||
99797c96 CMN |
1084 | if ((error = git_reference_lookup(&tmp, backend->repo, GIT_HEAD_FILE)) < 0) |
1085 | goto cleanup; | |
1086 | ||
1087 | /* Go down the symref chain until we find the branch */ | |
1088 | while (git_reference_type(tmp) == GIT_REF_SYMBOLIC) { | |
1089 | error = git_reference_lookup(&peeled, backend->repo, git_reference_symbolic_target(tmp)); | |
1090 | if (error < 0) | |
1091 | break; | |
1092 | ||
1093 | git_reference_free(tmp); | |
1094 | tmp = peeled; | |
1095 | } | |
1096 | ||
1097 | if (error == GIT_ENOTFOUND) { | |
1098 | error = 0; | |
1099 | name = git_reference_symbolic_target(tmp); | |
1100 | } else if (error < 0) { | |
1101 | goto cleanup; | |
1102 | } else { | |
1103 | name = git_reference_name(tmp); | |
1104 | } | |
1105 | ||
99797c96 | 1106 | if (strcmp(name, ref->name)) |
6aaae94a CMN |
1107 | goto cleanup; |
1108 | ||
4b7e1b9e CMN |
1109 | error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message); |
1110 | ||
6aaae94a | 1111 | cleanup: |
99797c96 | 1112 | git_reference_free(tmp); |
4b7e1b9e CMN |
1113 | git_reference_free(head); |
1114 | return error; | |
1115 | } | |
1116 | ||
d00d5464 ET |
1117 | static int refdb_fs_backend__write( |
1118 | git_refdb_backend *_backend, | |
4e6e2ff2 | 1119 | const git_reference *ref, |
110df893 | 1120 | int force, |
a57dd3b7 | 1121 | const git_signature *who, |
9b148098 | 1122 | const char *message, |
91123661 CMN |
1123 | const git_oid *old_id, |
1124 | const char *old_target) | |
d00d5464 | 1125 | { |
fe372740 | 1126 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
a57dd3b7 | 1127 | git_filebuf file = GIT_FILEBUF_INIT; |
ab8d9242 | 1128 | int error = 0; |
d00d5464 | 1129 | |
fe372740 | 1130 | assert(backend); |
d00d5464 | 1131 | |
4e6e2ff2 VM |
1132 | error = reference_path_available(backend, ref->name, NULL, force); |
1133 | if (error < 0) | |
1134 | return error; | |
1135 | ||
9b148098 | 1136 | /* We need to perform the reflog append and old value check under the ref's lock */ |
7ee8c7e6 | 1137 | if ((error = loose_lock(&file, backend, ref->name)) < 0) |
a57dd3b7 CMN |
1138 | return error; |
1139 | ||
ab8d9242 CMN |
1140 | return refdb_fs_backend__write_tail(_backend, ref, &file, true, who, message, old_id, old_target); |
1141 | } | |
1142 | ||
1143 | static int refdb_fs_backend__write_tail( | |
1144 | git_refdb_backend *_backend, | |
1145 | const git_reference *ref, | |
1146 | git_filebuf *file, | |
1147 | int update_reflog, | |
1148 | const git_signature *who, | |
1149 | const char *message, | |
1150 | const git_oid *old_id, | |
1151 | const char *old_target) | |
1152 | { | |
1153 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; | |
1154 | int error = 0, cmp = 0, should_write; | |
1155 | const char *new_target = NULL; | |
1156 | const git_oid *new_id = NULL; | |
1157 | ||
7ee8c7e6 | 1158 | if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0) |
f8621dde | 1159 | goto on_error; |
91123661 CMN |
1160 | |
1161 | if (cmp) { | |
1162 | giterr_set(GITERR_REFERENCE, "old reference value does not match"); | |
1163 | error = GIT_EMODIFIED; | |
1164 | goto on_error; | |
9b148098 CMN |
1165 | } |
1166 | ||
1afe1400 CMN |
1167 | if (ref->type == GIT_REF_SYMBOLIC) |
1168 | new_target = ref->target.symbolic; | |
1169 | else | |
1170 | new_id = &ref->target.oid; | |
1171 | ||
1172 | error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target); | |
1173 | if (error < 0 && error != GIT_ENOTFOUND) | |
1174 | goto on_error; | |
1175 | ||
1176 | /* Don't update if we have the same value */ | |
1177 | if (!error && !cmp) { | |
1178 | error = 0; | |
1179 | goto on_error; /* not really error */ | |
1180 | } | |
1181 | ||
ab8d9242 CMN |
1182 | if (update_reflog) { |
1183 | if ((error = should_write_reflog(&should_write, backend->repo, ref->name)) < 0) | |
4b7e1b9e | 1184 | goto on_error; |
ab8d9242 CMN |
1185 | |
1186 | if (should_write) { | |
1187 | if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0) | |
1188 | goto on_error; | |
1189 | if ((error = maybe_append_head(backend, ref, who, message)) < 0) | |
1190 | goto on_error; | |
1191 | } | |
a57dd3b7 CMN |
1192 | } |
1193 | ||
ab8d9242 | 1194 | return loose_commit(file, ref); |
9b148098 CMN |
1195 | |
1196 | on_error: | |
ab8d9242 | 1197 | git_filebuf_cleanup(file); |
fc4728e3 | 1198 | return error; |
d00d5464 ET |
1199 | } |
1200 | ||
1201 | static int refdb_fs_backend__delete( | |
1202 | git_refdb_backend *_backend, | |
7ee8c7e6 CMN |
1203 | const char *ref_name, |
1204 | const git_oid *old_id, const char *old_target) | |
d00d5464 | 1205 | { |
fe372740 | 1206 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
77ad6754 | 1207 | git_filebuf file = GIT_FILEBUF_INIT; |
ab8d9242 | 1208 | int error = 0; |
d00d5464 | 1209 | |
fe372740 | 1210 | assert(backend && ref_name); |
d00d5464 | 1211 | |
7ee8c7e6 CMN |
1212 | if ((error = loose_lock(&file, backend, ref_name)) < 0) |
1213 | return error; | |
1214 | ||
ab8d9242 CMN |
1215 | return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target); |
1216 | } | |
1217 | ||
1218 | static int refdb_fs_backend__delete_tail( | |
1219 | git_refdb_backend *_backend, | |
1220 | git_filebuf *file, | |
1221 | const char *ref_name, | |
1222 | const git_oid *old_id, const char *old_target) | |
1223 | { | |
1224 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; | |
1225 | git_buf loose_path = GIT_BUF_INIT; | |
1226 | size_t pack_pos; | |
1227 | int error = 0, cmp = 0; | |
1228 | bool loose_deleted = 0; | |
1229 | ||
7ee8c7e6 | 1230 | error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target); |
7ee8c7e6 CMN |
1231 | if (error < 0) |
1232 | goto cleanup; | |
1233 | ||
1234 | if (cmp) { | |
1235 | giterr_set(GITERR_REFERENCE, "old reference value does not match"); | |
1236 | error = GIT_EMODIFIED; | |
1237 | goto cleanup; | |
1238 | } | |
1239 | ||
d00d5464 | 1240 | /* If a loose reference exists, remove it from the filesystem */ |
4e6e2ff2 | 1241 | if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0) |
d00d5464 ET |
1242 | return -1; |
1243 | ||
1244 | if (git_path_isfile(loose_path.ptr)) { | |
1245 | error = p_unlink(loose_path.ptr); | |
1246 | loose_deleted = 1; | |
1247 | } | |
4dcd8780 | 1248 | |
d00d5464 ET |
1249 | git_buf_free(&loose_path); |
1250 | ||
1251 | if (error != 0) | |
7ee8c7e6 | 1252 | goto cleanup; |
d00d5464 | 1253 | |
7ee8c7e6 CMN |
1254 | if ((error = packed_reload(backend)) < 0) |
1255 | goto cleanup; | |
fe372740 | 1256 | |
d00d5464 | 1257 | /* If a packed reference exists, remove it from the packfile and repack */ |
7ee8c7e6 CMN |
1258 | if ((error = git_sortedcache_wlock(backend->refcache)) < 0) |
1259 | goto cleanup; | |
4e6e2ff2 | 1260 | |
fe372740 RB |
1261 | if (!(error = git_sortedcache_lookup_index( |
1262 | &pack_pos, backend->refcache, ref_name))) | |
8d9a85d4 | 1263 | error = git_sortedcache_remove(backend->refcache, pack_pos); |
d00d5464 | 1264 | |
8d9a85d4 | 1265 | git_sortedcache_wunlock(backend->refcache); |
4dcd8780 | 1266 | |
7ee8c7e6 CMN |
1267 | if (error == GIT_ENOTFOUND) { |
1268 | error = loose_deleted ? 0 : ref_error_notfound(ref_name); | |
1269 | goto cleanup; | |
1270 | } | |
1271 | ||
1272 | error = packed_write(backend); | |
fe372740 | 1273 | |
7ee8c7e6 | 1274 | cleanup: |
ab8d9242 | 1275 | git_filebuf_cleanup(file); |
7ee8c7e6 CMN |
1276 | |
1277 | return error; | |
d00d5464 ET |
1278 | } |
1279 | ||
a57dd3b7 CMN |
1280 | static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name); |
1281 | ||
4e6e2ff2 VM |
1282 | static int refdb_fs_backend__rename( |
1283 | git_reference **out, | |
1284 | git_refdb_backend *_backend, | |
1285 | const char *old_name, | |
1286 | const char *new_name, | |
110df893 | 1287 | int force, |
a57dd3b7 | 1288 | const git_signature *who, |
110df893 | 1289 | const char *message) |
4e6e2ff2 | 1290 | { |
fe372740 | 1291 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
4e6e2ff2 | 1292 | git_reference *old, *new; |
a57dd3b7 | 1293 | git_filebuf file = GIT_FILEBUF_INIT; |
4e6e2ff2 VM |
1294 | int error; |
1295 | ||
fe372740 | 1296 | assert(backend); |
4e6e2ff2 | 1297 | |
fe372740 RB |
1298 | if ((error = reference_path_available( |
1299 | backend, new_name, old_name, force)) < 0 || | |
1300 | (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0) | |
4e6e2ff2 VM |
1301 | return error; |
1302 | ||
7ee8c7e6 | 1303 | if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) { |
4e6e2ff2 VM |
1304 | git_reference_free(old); |
1305 | return error; | |
1306 | } | |
1307 | ||
fe372740 RB |
1308 | new = git_reference__set_name(old, new_name); |
1309 | if (!new) { | |
1310 | git_reference_free(old); | |
1311 | return -1; | |
4e6e2ff2 VM |
1312 | } |
1313 | ||
7ee8c7e6 | 1314 | if ((error = loose_lock(&file, backend, new->name)) < 0) { |
a57dd3b7 CMN |
1315 | git_reference_free(new); |
1316 | return error; | |
1317 | } | |
1318 | ||
1319 | /* Try to rename the refog; it's ok if the old doesn't exist */ | |
1320 | error = refdb_reflog_fs__rename(_backend, old_name, new_name); | |
1321 | if (((error == 0) || (error == GIT_ENOTFOUND)) && | |
4b7e1b9e | 1322 | ((error = reflog_append(backend, new, NULL, NULL, who, message)) < 0)) { |
a57dd3b7 CMN |
1323 | git_reference_free(new); |
1324 | git_filebuf_cleanup(&file); | |
1325 | return error; | |
1326 | } | |
1327 | ||
1328 | if (error < 0) { | |
1329 | git_reference_free(new); | |
1330 | git_filebuf_cleanup(&file); | |
1331 | return error; | |
1332 | } | |
1333 | ||
1334 | ||
1335 | if ((error = loose_commit(&file, new)) < 0 || out == NULL) { | |
4e6e2ff2 | 1336 | git_reference_free(new); |
fe372740 | 1337 | return error; |
4e6e2ff2 VM |
1338 | } |
1339 | ||
fe372740 | 1340 | *out = new; |
4e6e2ff2 VM |
1341 | return 0; |
1342 | } | |
1343 | ||
d00d5464 ET |
1344 | static int refdb_fs_backend__compress(git_refdb_backend *_backend) |
1345 | { | |
fe372740 | 1346 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 | 1347 | |
fe372740 | 1348 | assert(backend); |
d00d5464 | 1349 | |
fe372740 | 1350 | if (packed_reload(backend) < 0 || /* load the existing packfile */ |
d00d5464 ET |
1351 | packed_loadloose(backend) < 0 || /* add all the loose refs */ |
1352 | packed_write(backend) < 0) /* write back to disk */ | |
1353 | return -1; | |
1354 | ||
1355 | return 0; | |
1356 | } | |
1357 | ||
d00d5464 ET |
1358 | static void refdb_fs_backend__free(git_refdb_backend *_backend) |
1359 | { | |
fe372740 | 1360 | refdb_fs_backend *backend = (refdb_fs_backend *)_backend; |
d00d5464 | 1361 | |
fe372740 | 1362 | assert(backend); |
d00d5464 | 1363 | |
fe372740 | 1364 | git_sortedcache_free(backend->refcache); |
bade5194 | 1365 | git__free(backend->path); |
d00d5464 ET |
1366 | git__free(backend); |
1367 | } | |
1368 | ||
8cddf9b8 VM |
1369 | static int setup_namespace(git_buf *path, git_repository *repo) |
1370 | { | |
1fed6b07 | 1371 | char *parts, *start, *end; |
8cddf9b8 | 1372 | |
69a3c766 CMN |
1373 | /* Not all repositories have a path */ |
1374 | if (repo->path_repository == NULL) | |
1375 | return 0; | |
1376 | ||
8cddf9b8 VM |
1377 | /* Load the path to the repo first */ |
1378 | git_buf_puts(path, repo->path_repository); | |
1379 | ||
1380 | /* if the repo is not namespaced, nothing else to do */ | |
1381 | if (repo->namespace == NULL) | |
1382 | return 0; | |
1383 | ||
1384 | parts = end = git__strdup(repo->namespace); | |
1385 | if (parts == NULL) | |
1386 | return -1; | |
1387 | ||
fe372740 | 1388 | /* |
8cddf9b8 VM |
1389 | * From `man gitnamespaces`: |
1390 | * namespaces which include a / will expand to a hierarchy | |
1391 | * of namespaces; for example, GIT_NAMESPACE=foo/bar will store | |
1392 | * refs under refs/namespaces/foo/refs/namespaces/bar/ | |
1393 | */ | |
1394 | while ((start = git__strsep(&end, "/")) != NULL) { | |
1395 | git_buf_printf(path, "refs/namespaces/%s/", start); | |
1396 | } | |
1397 | ||
1398 | git_buf_printf(path, "refs/namespaces/%s/refs", end); | |
1ed356dc | 1399 | git__free(parts); |
8cddf9b8 VM |
1400 | |
1401 | /* Make sure that the folder with the namespace exists */ | |
1fed6b07 | 1402 | if (git_futils_mkdir_r(git_buf_cstr(path), repo->path_repository, 0777) < 0) |
8cddf9b8 VM |
1403 | return -1; |
1404 | ||
fe372740 | 1405 | /* Return root of the namespaced path, i.e. without the trailing '/refs' */ |
8cddf9b8 VM |
1406 | git_buf_rtruncate_at_char(path, '/'); |
1407 | return 0; | |
1408 | } | |
1409 | ||
b976f3c2 CMN |
1410 | static int reflog_alloc(git_reflog **reflog, const char *name) |
1411 | { | |
1412 | git_reflog *log; | |
1413 | ||
1414 | *reflog = NULL; | |
1415 | ||
1416 | log = git__calloc(1, sizeof(git_reflog)); | |
1417 | GITERR_CHECK_ALLOC(log); | |
1418 | ||
1419 | log->ref_name = git__strdup(name); | |
1420 | GITERR_CHECK_ALLOC(log->ref_name); | |
1421 | ||
1422 | if (git_vector_init(&log->entries, 0, NULL) < 0) { | |
1423 | git__free(log->ref_name); | |
1424 | git__free(log); | |
1425 | return -1; | |
1426 | } | |
1427 | ||
1428 | *reflog = log; | |
1429 | ||
1430 | return 0; | |
1431 | } | |
1432 | ||
1433 | static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) | |
1434 | { | |
1435 | const char *ptr; | |
1436 | git_reflog_entry *entry; | |
1437 | ||
1438 | #define seek_forward(_increase) do { \ | |
1439 | if (_increase >= buf_size) { \ | |
1440 | giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \ | |
1441 | goto fail; \ | |
1442 | } \ | |
1443 | buf += _increase; \ | |
1444 | buf_size -= _increase; \ | |
1445 | } while (0) | |
1446 | ||
1447 | while (buf_size > GIT_REFLOG_SIZE_MIN) { | |
1448 | entry = git__calloc(1, sizeof(git_reflog_entry)); | |
1449 | GITERR_CHECK_ALLOC(entry); | |
1450 | ||
1451 | entry->committer = git__malloc(sizeof(git_signature)); | |
1452 | GITERR_CHECK_ALLOC(entry->committer); | |
1453 | ||
1454 | if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0) | |
1455 | goto fail; | |
1456 | seek_forward(GIT_OID_HEXSZ + 1); | |
1457 | ||
1458 | if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0) | |
1459 | goto fail; | |
1460 | seek_forward(GIT_OID_HEXSZ + 1); | |
1461 | ||
1462 | ptr = buf; | |
1463 | ||
1464 | /* Seek forward to the end of the signature. */ | |
1465 | while (*buf && *buf != '\t' && *buf != '\n') | |
1466 | seek_forward(1); | |
1467 | ||
1468 | if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0) | |
1469 | goto fail; | |
1470 | ||
1471 | if (*buf == '\t') { | |
1472 | /* We got a message. Read everything till we reach LF. */ | |
1473 | seek_forward(1); | |
1474 | ptr = buf; | |
1475 | ||
1476 | while (*buf && *buf != '\n') | |
1477 | seek_forward(1); | |
1478 | ||
1479 | entry->msg = git__strndup(ptr, buf - ptr); | |
1480 | GITERR_CHECK_ALLOC(entry->msg); | |
1481 | } else | |
1482 | entry->msg = NULL; | |
1483 | ||
1484 | while (*buf && *buf == '\n' && buf_size > 1) | |
1485 | seek_forward(1); | |
1486 | ||
1487 | if (git_vector_insert(&log->entries, entry) < 0) | |
1488 | goto fail; | |
1489 | } | |
1490 | ||
1491 | return 0; | |
1492 | ||
1493 | #undef seek_forward | |
1494 | ||
1495 | fail: | |
1496 | if (entry) | |
1497 | git_reflog_entry__free(entry); | |
1498 | ||
1499 | return -1; | |
1500 | } | |
1501 | ||
1502 | static int create_new_reflog_file(const char *filepath) | |
1503 | { | |
1504 | int fd, error; | |
1505 | ||
1506 | if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0) | |
1507 | return error; | |
1508 | ||
1509 | if ((fd = p_open(filepath, | |
8d5ec910 | 1510 | O_WRONLY | O_CREAT, |
b976f3c2 CMN |
1511 | GIT_REFLOG_FILE_MODE)) < 0) |
1512 | return -1; | |
1513 | ||
1514 | return p_close(fd); | |
1515 | } | |
1516 | ||
1517 | GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name) | |
1518 | { | |
18234b14 | 1519 | return git_buf_join3(path, '/', repo->path_repository, GIT_REFLOG_DIR, name); |
b976f3c2 CMN |
1520 | } |
1521 | ||
8d5ec910 CMN |
1522 | static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name) |
1523 | { | |
1524 | refdb_fs_backend *backend; | |
1525 | git_repository *repo; | |
1526 | git_buf path = GIT_BUF_INIT; | |
1527 | int error; | |
1528 | ||
1529 | assert(_backend && name); | |
1530 | ||
1531 | backend = (refdb_fs_backend *) _backend; | |
1532 | repo = backend->repo; | |
1533 | ||
1534 | if ((error = retrieve_reflog_path(&path, repo, name)) < 0) | |
1535 | return error; | |
1536 | ||
ae32c54e CMN |
1537 | error = create_new_reflog_file(git_buf_cstr(&path)); |
1538 | git_buf_free(&path); | |
1539 | ||
1540 | return error; | |
8d5ec910 CMN |
1541 | } |
1542 | ||
6f13a305 CMN |
1543 | static int has_reflog(git_repository *repo, const char *name) |
1544 | { | |
1545 | int ret = 0; | |
1546 | git_buf path = GIT_BUF_INIT; | |
1547 | ||
1548 | if (retrieve_reflog_path(&path, repo, name) < 0) | |
1549 | goto cleanup; | |
1550 | ||
1551 | ret = git_path_isfile(git_buf_cstr(&path)); | |
1552 | ||
1553 | cleanup: | |
1554 | git_buf_free(&path); | |
1555 | return ret; | |
1556 | } | |
1557 | ||
f2105129 CMN |
1558 | static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name) |
1559 | { | |
1560 | refdb_fs_backend *backend; | |
1561 | ||
1562 | assert(_backend && name); | |
1563 | ||
1564 | backend = (refdb_fs_backend *) _backend; | |
1565 | ||
1566 | return has_reflog(backend->repo, name); | |
1567 | } | |
1568 | ||
b976f3c2 CMN |
1569 | static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name) |
1570 | { | |
1571 | int error = -1; | |
1572 | git_buf log_path = GIT_BUF_INIT; | |
1573 | git_buf log_file = GIT_BUF_INIT; | |
1574 | git_reflog *log = NULL; | |
1575 | git_repository *repo; | |
1576 | refdb_fs_backend *backend; | |
1577 | ||
1578 | assert(out && _backend && name); | |
1579 | ||
1580 | backend = (refdb_fs_backend *) _backend; | |
1581 | repo = backend->repo; | |
1582 | ||
1583 | if (reflog_alloc(&log, name) < 0) | |
1584 | return -1; | |
1585 | ||
1586 | if (retrieve_reflog_path(&log_path, repo, name) < 0) | |
1587 | goto cleanup; | |
1588 | ||
1589 | error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path)); | |
1590 | if (error < 0 && error != GIT_ENOTFOUND) | |
1591 | goto cleanup; | |
1592 | ||
1593 | if ((error == GIT_ENOTFOUND) && | |
1594 | ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0)) | |
1595 | goto cleanup; | |
1596 | ||
1597 | if ((error = reflog_parse(log, | |
1598 | git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0) | |
1599 | goto cleanup; | |
1600 | ||
1601 | *out = log; | |
1602 | goto success; | |
1603 | ||
1604 | cleanup: | |
1605 | git_reflog_free(log); | |
1606 | ||
1607 | success: | |
1608 | git_buf_free(&log_file); | |
1609 | git_buf_free(&log_path); | |
1610 | ||
1611 | return error; | |
1612 | } | |
1613 | ||
1614 | static int serialize_reflog_entry( | |
1615 | git_buf *buf, | |
1616 | const git_oid *oid_old, | |
1617 | const git_oid *oid_new, | |
1618 | const git_signature *committer, | |
1619 | const char *msg) | |
1620 | { | |
1621 | char raw_old[GIT_OID_HEXSZ+1]; | |
1622 | char raw_new[GIT_OID_HEXSZ+1]; | |
1623 | ||
1624 | git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old); | |
1625 | git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new); | |
1626 | ||
1627 | git_buf_clear(buf); | |
1628 | ||
1629 | git_buf_puts(buf, raw_old); | |
1630 | git_buf_putc(buf, ' '); | |
1631 | git_buf_puts(buf, raw_new); | |
1632 | ||
1633 | git_signature__writebuf(buf, " ", committer); | |
1634 | ||
1635 | /* drop trailing LF */ | |
1636 | git_buf_rtrim(buf); | |
1637 | ||
1638 | if (msg) { | |
1639 | git_buf_putc(buf, '\t'); | |
1640 | git_buf_puts(buf, msg); | |
1641 | } | |
1642 | ||
1643 | git_buf_putc(buf, '\n'); | |
1644 | ||
1645 | return git_buf_oom(buf); | |
1646 | } | |
1647 | ||
110df893 CMN |
1648 | static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname) |
1649 | { | |
1650 | git_repository *repo; | |
1651 | git_buf log_path = GIT_BUF_INIT; | |
1652 | int error; | |
1653 | ||
1654 | repo = backend->repo; | |
1655 | ||
1656 | if (retrieve_reflog_path(&log_path, repo, refname) < 0) | |
1657 | return -1; | |
1658 | ||
1659 | if (!git_path_isfile(git_buf_cstr(&log_path))) { | |
1660 | giterr_set(GITERR_INVALID, | |
1661 | "Log file for reference '%s' doesn't exist.", refname); | |
1662 | error = -1; | |
1663 | goto cleanup; | |
1664 | } | |
1665 | ||
1666 | error = git_filebuf_open(file, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE); | |
1667 | ||
1668 | cleanup: | |
1669 | git_buf_free(&log_path); | |
1670 | ||
1671 | return error; | |
1672 | } | |
1673 | ||
b976f3c2 CMN |
1674 | static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog) |
1675 | { | |
1676 | int error = -1; | |
1677 | unsigned int i; | |
1678 | git_reflog_entry *entry; | |
b976f3c2 | 1679 | refdb_fs_backend *backend; |
b976f3c2 CMN |
1680 | git_buf log = GIT_BUF_INIT; |
1681 | git_filebuf fbuf = GIT_FILEBUF_INIT; | |
1682 | ||
1683 | assert(_backend && reflog); | |
1684 | ||
1685 | backend = (refdb_fs_backend *) _backend; | |
b976f3c2 | 1686 | |
110df893 | 1687 | if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0) |
b976f3c2 CMN |
1688 | return -1; |
1689 | ||
b976f3c2 CMN |
1690 | git_vector_foreach(&reflog->entries, i, entry) { |
1691 | if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0) | |
1692 | goto cleanup; | |
1693 | ||
1694 | if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0) | |
1695 | goto cleanup; | |
1696 | } | |
1697 | ||
1d3a8aeb | 1698 | error = git_filebuf_commit(&fbuf); |
b976f3c2 CMN |
1699 | goto success; |
1700 | ||
1701 | cleanup: | |
1702 | git_filebuf_cleanup(&fbuf); | |
1703 | ||
1704 | success: | |
1705 | git_buf_free(&log); | |
110df893 | 1706 | |
b976f3c2 CMN |
1707 | return error; |
1708 | } | |
1709 | ||
a57dd3b7 | 1710 | /* Append to the reflog, must be called under reference lock */ |
4b7e1b9e | 1711 | static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *who, const char *message) |
a57dd3b7 | 1712 | { |
afc57eb4 | 1713 | int error, is_symbolic; |
4b7e1b9e | 1714 | git_oid old_id = {{0}}, new_id = {{0}}; |
a57dd3b7 CMN |
1715 | git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; |
1716 | git_repository *repo = backend->repo; | |
1717 | ||
4b7e1b9e CMN |
1718 | is_symbolic = ref->type == GIT_REF_SYMBOLIC; |
1719 | ||
1720 | /* "normal" symbolic updates do not write */ | |
1721 | if (is_symbolic && | |
1722 | strcmp(ref->name, GIT_HEAD_FILE) && | |
1723 | !(old && new)) | |
a57dd3b7 CMN |
1724 | return 0; |
1725 | ||
4b7e1b9e CMN |
1726 | /* From here on is_symoblic also means that it's HEAD */ |
1727 | ||
1728 | if (old) { | |
1729 | git_oid_cpy(&old_id, old); | |
afc57eb4 | 1730 | } else { |
4b7e1b9e | 1731 | error = git_reference_name_to_id(&old_id, repo, ref->name); |
afc57eb4 | 1732 | if (error < 0 && error != GIT_ENOTFOUND) |
4b7e1b9e CMN |
1733 | return error; |
1734 | } | |
1735 | ||
6aaae94a CMN |
1736 | if (new) { |
1737 | git_oid_cpy(&new_id, new); | |
1738 | } else { | |
1739 | if (!is_symbolic) { | |
1740 | git_oid_cpy(&new_id, git_reference_target(ref)); | |
1741 | } else { | |
1742 | error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref)); | |
1743 | if (error < 0 && error != GIT_ENOTFOUND) | |
1744 | return error; | |
1745 | /* detaching HEAD does not create an entry */ | |
1746 | if (error == GIT_ENOTFOUND) | |
1747 | return 0; | |
4b7e1b9e | 1748 | |
6aaae94a CMN |
1749 | giterr_clear(); |
1750 | } | |
7f058b86 | 1751 | } |
4b7e1b9e | 1752 | |
a57dd3b7 CMN |
1753 | if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0) |
1754 | goto cleanup; | |
1755 | ||
1756 | if ((error = retrieve_reflog_path(&path, repo, ref->name)) < 0) | |
1757 | goto cleanup; | |
1758 | ||
1759 | if (((error = git_futils_mkpath2file(git_buf_cstr(&path), 0777)) < 0) && | |
1760 | (error != GIT_EEXISTS)) { | |
1761 | goto cleanup; | |
1762 | } | |
1763 | ||
1764 | error = git_futils_writebuffer(&buf, git_buf_cstr(&path), O_WRONLY|O_CREAT|O_APPEND, GIT_REFLOG_FILE_MODE); | |
1765 | ||
1766 | cleanup: | |
1767 | git_buf_free(&buf); | |
1768 | git_buf_free(&path); | |
1769 | ||
b976f3c2 CMN |
1770 | return error; |
1771 | } | |
1772 | ||
b976f3c2 CMN |
1773 | static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name) |
1774 | { | |
1775 | int error = 0, fd; | |
1776 | git_buf old_path = GIT_BUF_INIT; | |
1777 | git_buf new_path = GIT_BUF_INIT; | |
1778 | git_buf temp_path = GIT_BUF_INIT; | |
1779 | git_buf normalized = GIT_BUF_INIT; | |
1780 | git_repository *repo; | |
1781 | refdb_fs_backend *backend; | |
1782 | ||
1783 | assert(_backend && old_name && new_name); | |
1784 | ||
1785 | backend = (refdb_fs_backend *) _backend; | |
1786 | repo = backend->repo; | |
1787 | ||
1788 | if ((error = git_reference__normalize_name( | |
1789 | &normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0) | |
1790 | return error; | |
1791 | ||
1792 | if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0) | |
1793 | return -1; | |
1794 | ||
1795 | if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0) | |
1796 | return -1; | |
1797 | ||
1798 | if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0) | |
1799 | return -1; | |
1800 | ||
a57dd3b7 CMN |
1801 | if (!git_path_exists(git_buf_cstr(&old_path))) { |
1802 | error = GIT_ENOTFOUND; | |
1803 | goto cleanup; | |
1804 | } | |
1805 | ||
b976f3c2 CMN |
1806 | /* |
1807 | * Move the reflog to a temporary place. This two-phase renaming is required | |
1808 | * in order to cope with funny renaming use cases when one tries to move a reference | |
1809 | * to a partially colliding namespace: | |
1810 | * - a/b -> a/b/c | |
1811 | * - a/b/c/d -> a/b/c | |
1812 | */ | |
1813 | if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0) | |
1814 | return -1; | |
1815 | ||
1d3a8aeb | 1816 | if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) { |
b976f3c2 CMN |
1817 | error = -1; |
1818 | goto cleanup; | |
1819 | } | |
1820 | ||
1821 | p_close(fd); | |
1822 | ||
1823 | if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) { | |
1824 | giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); | |
1825 | error = -1; | |
1826 | goto cleanup; | |
1827 | } | |
1828 | ||
1829 | if (git_path_isdir(git_buf_cstr(&new_path)) && | |
1830 | (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) { | |
1831 | error = -1; | |
1832 | goto cleanup; | |
1833 | } | |
1834 | ||
1835 | if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) { | |
1836 | error = -1; | |
1837 | goto cleanup; | |
1838 | } | |
1839 | ||
1840 | if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) { | |
1841 | giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name); | |
1842 | error = -1; | |
1843 | } | |
1844 | ||
1845 | cleanup: | |
1846 | git_buf_free(&temp_path); | |
1847 | git_buf_free(&old_path); | |
1848 | git_buf_free(&new_path); | |
1849 | git_buf_free(&normalized); | |
1850 | ||
1851 | return error; | |
1852 | } | |
1853 | ||
b976f3c2 CMN |
1854 | static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name) |
1855 | { | |
1856 | int error; | |
1857 | git_buf path = GIT_BUF_INIT; | |
1858 | ||
1859 | git_repository *repo; | |
1860 | refdb_fs_backend *backend; | |
1861 | ||
1862 | assert(_backend && name); | |
1863 | ||
1864 | backend = (refdb_fs_backend *) _backend; | |
1865 | repo = backend->repo; | |
1866 | ||
1867 | error = retrieve_reflog_path(&path, repo, name); | |
1868 | ||
1869 | if (!error && git_path_exists(path.ptr)) | |
1870 | error = p_unlink(path.ptr); | |
1871 | ||
1872 | git_buf_free(&path); | |
1873 | ||
1874 | return error; | |
1875 | ||
1876 | } | |
1877 | ||
d00d5464 ET |
1878 | int git_refdb_backend_fs( |
1879 | git_refdb_backend **backend_out, | |
4e4eab52 | 1880 | git_repository *repository) |
d00d5464 | 1881 | { |
219d3457 | 1882 | int t = 0; |
bade5194 | 1883 | git_buf path = GIT_BUF_INIT; |
d00d5464 ET |
1884 | refdb_fs_backend *backend; |
1885 | ||
1886 | backend = git__calloc(1, sizeof(refdb_fs_backend)); | |
1887 | GITERR_CHECK_ALLOC(backend); | |
1888 | ||
1889 | backend->repo = repository; | |
bade5194 | 1890 | |
fe372740 RB |
1891 | if (setup_namespace(&path, repository) < 0) |
1892 | goto fail; | |
bade5194 VM |
1893 | |
1894 | backend->path = git_buf_detach(&path); | |
d00d5464 | 1895 | |
fe372740 RB |
1896 | if (git_buf_joinpath(&path, backend->path, GIT_PACKEDREFS_FILE) < 0 || |
1897 | git_sortedcache_new( | |
1898 | &backend->refcache, offsetof(struct packref, name), | |
1899 | NULL, NULL, packref_cmp, git_buf_cstr(&path)) < 0) | |
1900 | goto fail; | |
1901 | ||
1902 | git_buf_free(&path); | |
1903 | ||
219d3457 RB |
1904 | if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) { |
1905 | backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE; | |
1906 | backend->direach_flags |= GIT_PATH_DIR_IGNORE_CASE; | |
1907 | } | |
1908 | if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_PRECOMPOSE) && t) { | |
1909 | backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE; | |
1910 | backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE; | |
1911 | } | |
1912 | ||
d00d5464 ET |
1913 | backend->parent.exists = &refdb_fs_backend__exists; |
1914 | backend->parent.lookup = &refdb_fs_backend__lookup; | |
4def7035 | 1915 | backend->parent.iterator = &refdb_fs_backend__iterator; |
d00d5464 | 1916 | backend->parent.write = &refdb_fs_backend__write; |
e3f3868a | 1917 | backend->parent.del = &refdb_fs_backend__delete; |
4e6e2ff2 | 1918 | backend->parent.rename = &refdb_fs_backend__rename; |
d00d5464 | 1919 | backend->parent.compress = &refdb_fs_backend__compress; |
ab8d9242 CMN |
1920 | backend->parent.lock = &refdb_fs_backend__lock; |
1921 | backend->parent.unlock = &refdb_fs_backend__unlock; | |
f2105129 | 1922 | backend->parent.has_log = &refdb_reflog_fs__has_log; |
8d5ec910 | 1923 | backend->parent.ensure_log = &refdb_reflog_fs__ensure_log; |
d00d5464 | 1924 | backend->parent.free = &refdb_fs_backend__free; |
b976f3c2 CMN |
1925 | backend->parent.reflog_read = &refdb_reflog_fs__read; |
1926 | backend->parent.reflog_write = &refdb_reflog_fs__write; | |
b976f3c2 | 1927 | backend->parent.reflog_rename = &refdb_reflog_fs__rename; |
b976f3c2 | 1928 | backend->parent.reflog_delete = &refdb_reflog_fs__delete; |
d00d5464 ET |
1929 | |
1930 | *backend_out = (git_refdb_backend *)backend; | |
1931 | return 0; | |
fe372740 RB |
1932 | |
1933 | fail: | |
1934 | git_buf_free(&path); | |
1935 | git__free(backend->path); | |
1936 | git__free(backend); | |
1937 | return -1; | |
d00d5464 | 1938 | } |