deps/libgit2/src/libgit2/refdb_fs.c

filter: Statementstmt 55.3% 674 / 1217 Branchbran 40.6% 296 / 728 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 49.8% 970 / 1945 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/ignore.c ↑ Summary const-xs-object.inc »
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 "futils.h"
12 #include "filebuf.h"
13 #include "pack.h"
14 #include "parse.h"
15 #include "reflog.h"
16 #include "refdb.h"
17 #include "iterator.h"
18 #include "sortedcache.h"
19 #include "signature.h"
20 #include "wildmatch.h"
21 #include "path.h"
22  
23 #include <git2/tag.h>
24 #include <git2/object.h>
25 #include <git2/refdb.h>
26 #include <git2/branch.h>
27 #include <git2/sys/refdb_backend.h>
28 #include <git2/sys/refs.h>
29 #include <git2/sys/reflog.h>
30  
31 #define DEFAULT_NESTING_LEVEL 5
32 #define MAX_NESTING_LEVEL 10
33  
34 enum {
35 PACKREF_HAS_PEEL = 1,
36 PACKREF_WAS_LOOSE = 2,
37 PACKREF_CANNOT_PEEL = 4,
38 PACKREF_SHADOWED = 8
39 };
40  
41 enum {
42 PEELING_NONE = 0,
43 PEELING_STANDARD,
44 PEELING_FULL
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;
58 /* path to git directory */
59 char *gitpath;
60 /* path to common objects' directory */
61 char *commonpath;
62  
63 git_sortedcache *refcache;
64 int peeling_mode;
65 git_iterator_flag_t iterator_flags;
66 uint32_t direach_flags;
67 int fsync;
68 git_map packed_refs_map;
69 git_mutex prlock; /* protect packed_refs_map */
70 git_futils_filestamp packed_refs_stamp;
71 bool sorted;
72 } refdb_fs_backend;
73  
74 static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name);
75 static char *packed_set_peeling_mode(char *data, size_t data_sz, refdb_fs_backend *backend);
76  
77 1825 GIT_INLINE(int) loose_path(
78 git_str *out,
79 const char *base,
80 const char *refname)
81 {
82 1825 ▶ if (git_str_joinpath(out, base, refname) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(out, base, refname) < 0) 0 1825 1825
83 0 return -1;
84  
85 1825 return git_fs_path_validate_str_length_with_suffix(out,
86 CONST_STRLEN(".lock"));
87 }
88  
89 240 GIT_INLINE(int) reflog_path(
90 git_str *out,
91 git_repository *repo,
92 const char *refname)
93 {
94 const char *base;
95 int error;
96  
97 240 ▶ base = (strcmp(refname, GIT_HEAD_FILE) == 0) ? repo->gitdir :
Branch: 1 branch
BranchTrueFalseTotal
base = (strcmp(refname, GIT_HEAD_FILE) == 0) ? repo->gitdir : 77 163 240
98 repo->commondir;
99  
100 240 ▶ if ((error = git_str_joinpath(out, base, GIT_REFLOG_DIR)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_joinpath(out, base, GIT_REFLOG_DIR)) < 0) 0 240 240
101 0 return error;
102  
103 240 return loose_path(out, out->ptr, refname);
104 }
105  
106 0 static int packref_cmp(const void *a_, const void *b_)
107 {
108 0 const struct packref *a = a_, *b = b_;
109 0 return strcmp(a->name, b->name);
110 }
111  
112 333 static int packed_reload(refdb_fs_backend *backend)
113 {
114 int error;
115 333 git_str packedrefs = GIT_STR_INIT;
116 char *scan, *eof, *eol;
117  
118 333 ▶ if (!backend->gitpath)
Branch: 1 branch
BranchTrueFalseTotal
if (!backend->gitpath) 0 333 333
119 0 return 0;
120  
121 333 error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
122  
123 /*
124 * If we can't find the packed-refs, clear table and return.
125 * Any other error just gets passed through.
126 * If no error, and file wasn't changed, just return.
127 * Anything else means we need to refresh the packed refs.
128 */
129 333 ▶ if (error <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error <= 0) { 333 0 333
130 333 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 333 0 333
131 333 GIT_UNUSED(git_sortedcache_clear(backend->refcache, true));
132 333 git_error_clear();
133 333 error = 0;
134 }
135 333 return error;
136 }
137  
138 /* At this point, refresh the packed refs from the loaded buffer. */
139  
140 0 GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
141  
142 0 scan = packedrefs.ptr;
143 0 eof = scan + packedrefs.size;
144  
145 0 scan = packed_set_peeling_mode(scan, packedrefs.size, backend);
146 0 ▶ if (!scan)
Branch: 1 branch
BranchTrueFalseTotal
if (!scan) 0 0 0
147 0 goto parse_failed;
148  
149 0 ▶ while (scan < eof && *scan == '#') {
Branch: 2 branches
BranchTrueFalseTotal
while (scan < eof && *scan == '#') { 0 0 0
while (scan < eof && *scan == '#') { 0 0 0
150 0 ▶ if (!(eol = strchr(scan, '\n')))
Branch: 1 branch
BranchTrueFalseTotal
if (!(eol = strchr(scan, '\n'))) 0 0 0
151 0 goto parse_failed;
152 0 scan = eol + 1;
153 }
154  
155 0 ▶ while (scan < eof) {
Branch: 1 branch
BranchTrueFalseTotal
while (scan < eof) { 0 0 0
156 struct packref *ref;
157 git_oid oid;
158  
159 /* parse "<OID> <refname>\n" */
160  
161 0 ▶ if (git_oid_fromstr(&oid, scan) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_fromstr(&oid, scan) < 0) 0 0 0
162 0 goto parse_failed;
163 0 scan += GIT_OID_HEXSZ;
164  
165 0 ▶ if (*scan++ != ' ')
Branch: 1 branch
BranchTrueFalseTotal
if (*scan++ != ' ') 0 0 0
166 0 goto parse_failed;
167 0 ▶ if (!(eol = strchr(scan, '\n')))
Branch: 1 branch
BranchTrueFalseTotal
if (!(eol = strchr(scan, '\n'))) 0 0 0
168 0 goto parse_failed;
169 0 *eol = '\0';
170 0 ▶ if (eol[-1] == '\r')
Branch: 1 branch
BranchTrueFalseTotal
if (eol[-1] == '\r') 0 0 0
171 0 eol[-1] = '\0';
172  
173 0 ▶ if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0) 0 0 0
174 0 goto parse_failed;
175 0 scan = eol + 1;
176  
177 0 git_oid_cpy(&ref->oid, &oid);
178  
179 /* look for optional "^<OID>\n" */
180  
181 0 ▶ if (*scan == '^') {
Branch: 1 branch
BranchTrueFalseTotal
if (*scan == '^') { 0 0 0
182 0 ▶ if (git_oid_fromstr(&oid, scan + 1) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_fromstr(&oid, scan + 1) < 0) 0 0 0
183 0 goto parse_failed;
184 0 scan += GIT_OID_HEXSZ + 1;
185  
186 0 ▶ if (scan < eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (scan < eof) { 0 0 0
187 0 ▶ if (!(eol = strchr(scan, '\n')))
Branch: 1 branch
BranchTrueFalseTotal
if (!(eol = strchr(scan, '\n'))) 0 0 0
188 0 goto parse_failed;
189 0 scan = eol + 1;
190 }
191  
192 0 git_oid_cpy(&ref->peel, &oid);
193 0 ref->flags |= PACKREF_HAS_PEEL;
194 }
195 0 ▶ else if (backend->peeling_mode == PEELING_FULL ||
Branch: 1 branch
BranchTrueFalseTotal
else if (backend->peeling_mode == PEELING_FULL || 0 0 0
196 0 (backend->peeling_mode == PEELING_STANDARD &&
197 0 git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0))
198 0 ref->flags |= PACKREF_CANNOT_PEEL;
199 }
200  
201 0 git_sortedcache_wunlock(backend->refcache);
202 0 git_str_dispose(&packedrefs);
203  
204 0 return 0;
205  
206 0 parse_failed:
207 0 git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
208  
209 0 GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
210 0 git_sortedcache_wunlock(backend->refcache);
211 0 git_str_dispose(&packedrefs);
212  
213 0 return -1;
214 }
215  
216 787 static int loose_parse_oid(
217 git_oid *oid, const char *filename, git_str *file_content)
218 {
219 787 const char *str = git_str_cstr(file_content);
220  
221 787 ▶ if (git_str_len(file_content) < GIT_OID_HEXSZ)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_len(file_content) < GIT_OID_HEXSZ) 0 787 787
222 0 goto corrupted;
223  
224 /* we need to get 40 OID characters from the file */
225 787 ▶ if (git_oid_fromstr(oid, str) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_fromstr(oid, str) < 0) 0 787 787
226 0 goto corrupted;
227  
228 /* If the file is longer than 40 chars, the 41st must be a space */
229 787 str += GIT_OID_HEXSZ;
230 787 ▶ if (*str == '\0' || git__isspace(*str))
Branch: 2 branches
BranchTrueFalseTotal
if (*str == '\0' || git__isspace(*str)) 787 0 787
if (*str == '\0' || git__isspace(*str)) 787 0 787
231 787 return 0;
232  
233 0 corrupted:
234 0 git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file: %s", filename);
235 0 return -1;
236 }
237  
238 1437 static int loose_readbuffer(git_str *buf, const char *base, const char *path)
239 {
240 int error;
241  
242 2874 if ((error = loose_path(buf, base, path)) < 0 ||
243 1437 (error = git_futils_readbuffer(buf, buf->ptr)) < 0)
244 166 git_str_dispose(buf);
245  
246 1437 return error;
247 }
248  
249 0 static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
250 {
251 0 int error = 0;
252 0 git_str ref_file = GIT_STR_INIT;
253 0 struct packref *ref = NULL;
254 git_oid oid;
255  
256 /* if we fail to load the loose reference, assume someone changed
257 * the filesystem under us and skip it...
258 */
259 0 ▶ if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) { 0 0 0
260 0 git_error_clear();
261 0 goto done;
262 }
263  
264 /* skip symbolic refs */
265 0 ▶ if (!git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF)) 0 0 0
266 0 goto done;
267  
268 /* parse OID from file */
269 0 ▶ if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0) 0 0 0
270 0 goto done;
271  
272 0 ▶ if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_wlock(backend->refcache)) < 0) 0 0 0
273 0 goto done;
274  
275 0 ▶ if (!(error = git_sortedcache_upsert(
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = git_sortedcache_upsert( 0 0 0
276 (void **)&ref, backend->refcache, name))) {
277  
278 0 git_oid_cpy(&ref->oid, &oid);
279 0 ref->flags = PACKREF_WAS_LOOSE;
280 }
281  
282 0 git_sortedcache_wunlock(backend->refcache);
283  
284 0 done:
285 0 git_str_dispose(&ref_file);
286 0 return error;
287 }
288  
289 0 static int _dirent_loose_load(void *payload, git_str *full_path)
290 {
291 0 refdb_fs_backend *backend = payload;
292 const char *file_path;
293  
294 0 ▶ if (git__suffixcmp(full_path->ptr, ".lock") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git__suffixcmp(full_path->ptr, ".lock") == 0) 0 0 0
295 0 return 0;
296  
297 0 ▶ if (git_fs_path_isdir(full_path->ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_isdir(full_path->ptr)) { 0 0 0
298 0 int error = git_fs_path_direach(
299 full_path, backend->direach_flags, _dirent_loose_load, backend);
300 /* Race with the filesystem, ignore it */
301 0 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 0 0
302 0 git_error_clear();
303 0 return 0;
304 }
305  
306 0 return error;
307 }
308  
309 0 file_path = full_path->ptr + strlen(backend->gitpath);
310  
311 0 return loose_lookup_to_packfile(backend, file_path);
312 }
313  
314 /*
315 * Load all the loose references from the repository
316 * into the in-memory Packfile, and build a vector with
317 * all the references so it can be written back to
318 * disk.
319 */
320 0 static int packed_loadloose(refdb_fs_backend *backend)
321 {
322 int error;
323 0 git_str refs_path = GIT_STR_INIT;
324  
325 0 ▶ if (git_str_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0) 0 0 0
326 0 return -1;
327  
328 /*
329 * Load all the loose files from disk into the Packfile table.
330 * This will overwrite any old packed entries with their
331 * updated loose versions
332 */
333 0 error = git_fs_path_direach(
334 &refs_path, backend->direach_flags, _dirent_loose_load, backend);
335  
336 0 git_str_dispose(&refs_path);
337  
338 0 return error;
339 }
340  
341 30 static int refdb_fs_backend__exists(
342 int *exists,
343 git_refdb_backend *_backend,
344 const char *ref_name)
345 {
346 30 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
347 30 git_str ref_path = GIT_STR_INIT;
348 int error;
349  
350 30 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 30 30
351  
352 30 *exists = 0;
353  
354 30 ▶ if ((error = loose_path(&ref_path, backend->gitpath, ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&ref_path, backend->gitpath, ref_name)) < 0) 0 30 30
355 0 goto out;
356  
357 30 ▶ if (git_fs_path_isfile(ref_path.ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_isfile(ref_path.ptr)) { 2 28 30
358 2 *exists = 1;
359 2 goto out;
360 }
361  
362 28 ▶ if ((error = packed_reload(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_reload(backend)) < 0) 0 28 28
363 0 goto out;
364  
365 28 ▶ if (git_sortedcache_lookup(backend->refcache, ref_name) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_sortedcache_lookup(backend->refcache, ref_name) != NULL) { 28 0 28
366 0 *exists = 1;
367 0 goto out;
368 }
369  
370 28 out:
371 30 git_str_dispose(&ref_path);
372 30 return error;
373 }
374  
375 484 static const char *loose_parse_symbolic(git_str *file_content)
376 {
377 484 const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
378 const char *refname_start;
379  
380 484 refname_start = (const char *)file_content->ptr;
381  
382 484 ▶ if (git_str_len(file_content) < header_len + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_len(file_content) < header_len + 1) { 0 484 484
383 0 git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file");
384 0 return NULL;
385 }
386  
387 /*
388 * Assume we have already checked for the header
389 * before calling this function
390 */
391 484 refname_start += header_len;
392  
393 484 return refname_start;
394 }
395  
396 /*
397 * Returns whether a reference is stored per worktree or not.
398 * Per-worktree references are:
399 *
400 * - all pseudorefs, e.g. HEAD and MERGE_HEAD
401 * - all references stored inside of "refs/bisect/"
402 */
403 1545 static bool is_per_worktree_ref(const char *ref_name)
404 {
405 2533 return git__prefixcmp(ref_name, "refs/") != 0 ||
406 988 git__prefixcmp(ref_name, "refs/bisect/") == 0;
407 }
408  
409 1437 static int loose_lookup(
410 git_reference **out,
411 refdb_fs_backend *backend,
412 const char *ref_name)
413 {
414 1437 git_str ref_file = GIT_STR_INIT;
415 1437 int error = 0;
416 const char *ref_dir;
417  
418 1437 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 1334 103 1437
419 1334 *out = NULL;
420  
421 1437 ▶ if (is_per_worktree_ref(ref_name))
Branch: 1 branch
BranchTrueFalseTotal
if (is_per_worktree_ref(ref_name)) 530 907 1437
422 530 ref_dir = backend->gitpath;
423 else
424 907 ref_dir = backend->commonpath;
425  
426 1437 ▶ if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0) 1271 166 1437
427 /* cannot read loose ref file - gah */;
428 1271 ▶ else if (git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (git__prefixcmp(git_str_cstr(&ref_file), GIT_SYMREF) == 0) { 484 787 1271
429 const char *target;
430  
431 484 git_str_rtrim(&ref_file);
432  
433 484 ▶ if (!(target = loose_parse_symbolic(&ref_file)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(target = loose_parse_symbolic(&ref_file))) 0 484 484
434 0 error = -1;
435 484 ▶ else if (out != NULL)
Branch: 1 branch
BranchTrueFalseTotal
else if (out != NULL) 484 0 484
436 484 *out = git_reference__alloc_symbolic(ref_name, target);
437 } else {
438 git_oid oid;
439  
440 787 ▶ if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) &&
Branch: 2 branches
BranchTrueFalseTotal
if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) && 787 0 787
if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) && 684 103 787
441 out != NULL)
442 684 *out = git_reference__alloc(ref_name, &oid, NULL);
443 }
444  
445 1437 git_str_dispose(&ref_file);
446 1437 return error;
447 }
448  
449 166 static int ref_error_notfound(const char *name)
450 {
451 166 git_error_set(GIT_ERROR_REFERENCE, "reference '%s' not found", name);
452 166 return GIT_ENOTFOUND;
453 }
454  
455 0 static char *packed_set_peeling_mode(
456 char *data,
457 size_t data_sz,
458 refdb_fs_backend *backend)
459 {
460 static const char *traits_header = "# pack-refs with:";
461 char *eol;
462 0 backend->peeling_mode = PEELING_NONE;
463  
464 0 ▶ if (git__prefixncmp(data, data_sz, traits_header) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(data, data_sz, traits_header) == 0) { 0 0 0
465 0 size_t hdr_sz = strlen(traits_header);
466 0 const char *sorted = " sorted ";
467 0 const char *peeled = " peeled ";
468 0 const char *fully_peeled = " fully-peeled ";
469 0 data += hdr_sz;
470 0 data_sz -= hdr_sz;
471  
472 0 eol = memchr(data, '\n', data_sz);
473  
474 0 ▶ if (!eol)
Branch: 1 branch
BranchTrueFalseTotal
if (!eol) 0 0 0
475 0 return NULL;
476  
477 0 ▶ if (git__memmem(data, eol - data, fully_peeled, strlen(fully_peeled)))
Branch: 1 branch
BranchTrueFalseTotal
if (git__memmem(data, eol - data, fully_peeled, strlen(fully_peeled))) 0 0 0
478 0 backend->peeling_mode = PEELING_FULL;
479 0 ▶ else if (git__memmem(data, eol - data, peeled, strlen(peeled)))
Branch: 1 branch
BranchTrueFalseTotal
else if (git__memmem(data, eol - data, peeled, strlen(peeled))) 0 0 0
480 0 backend->peeling_mode = PEELING_STANDARD;
481  
482 0 backend->sorted = NULL != git__memmem(data, eol - data, sorted, strlen(sorted));
483  
484 0 return eol + 1;
485 }
486 0 return data;
487 }
488  
489 215 static void packed_map_free(refdb_fs_backend *backend)
490 {
491 215 ▶ if (backend->packed_refs_map.data) {
Branch: 1 branch
BranchTrueFalseTotal
if (backend->packed_refs_map.data) { 0 215 215
492 #ifdef GIT_WIN32
493 git__free(backend->packed_refs_map.data);
494 #else
495 0 git_futils_mmap_free(&backend->packed_refs_map);
496 #endif
497 0 backend->packed_refs_map.data = NULL;
498 0 backend->packed_refs_map.len = 0;
499 0 git_futils_filestamp_set(&backend->packed_refs_stamp, NULL);
500 }
501 215 }
502  
503 166 static int packed_map_check(refdb_fs_backend *backend)
504 {
505 166 int error = 0;
506 166 git_file fd = -1;
507 struct stat st;
508  
509 166 ▶ if ((error = git_mutex_lock(&backend->prlock)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_mutex_lock(&backend->prlock)) < 0) 0 166 166
510 0 return error;
511  
512 166 if (backend->packed_refs_map.data &&
513 0 !git_futils_filestamp_check(
514 0 &backend->packed_refs_stamp, backend->refcache->path)) {
515 0 git_mutex_unlock(&backend->prlock);
516 0 return error;
517 }
518 166 packed_map_free(backend);
519  
520 166 fd = git_futils_open_ro(backend->refcache->path);
521 166 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 166 0 166
522 166 git_mutex_unlock(&backend->prlock);
523 166 ▶ if (fd == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd == GIT_ENOTFOUND) { 166 0 166
524 166 git_error_clear();
525 166 return 0;
526 }
527 0 return fd;
528 }
529  
530 0 ▶ if (p_fstat(fd, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_fstat(fd, &st) < 0) { 0 0 0
531 0 p_close(fd);
532 0 git_mutex_unlock(&backend->prlock);
533 0 git_error_set(GIT_ERROR_OS, "unable to stat packed-refs '%s'", backend->refcache->path);
534 0 return -1;
535 }
536  
537 0 ▶ if (st.st_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size == 0) { 0 0 0
538 0 p_close(fd);
539 0 git_mutex_unlock(&backend->prlock);
540 0 return 0;
541 }
542  
543 0 git_futils_filestamp_set_from_stat(&backend->packed_refs_stamp, &st);
544  
545 #ifdef GIT_WIN32
546 /* on windows, we copy the entire file into memory rather than using
547 * mmap() because using mmap() on windows also locks the file and this
548 * map is long-lived. */
549 backend->packed_refs_map.len = (size_t)st.st_size;
550 backend->packed_refs_map.data =
551 git__malloc(backend->packed_refs_map.len);
552 GIT_ERROR_CHECK_ALLOC(backend->packed_refs_map.data);
553 {
554 ssize_t bytesread =
555 p_read(fd, backend->packed_refs_map.data,
556 backend->packed_refs_map.len);
557 error = (bytesread == (ssize_t)backend->packed_refs_map.len) ? 0 : -1;
558 }
559 #else
560 0 error = git_futils_mmap_ro(&backend->packed_refs_map, fd, 0, (size_t)st.st_size);
561 #endif
562 0 p_close(fd);
563 0 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 0 0
564 0 git_mutex_unlock(&backend->prlock);
565 0 return error;
566 }
567  
568 0 packed_set_peeling_mode(
569 0 backend->packed_refs_map.data, backend->packed_refs_map.len,
570 backend);
571  
572 0 git_mutex_unlock(&backend->prlock);
573 0 return error;
574 }
575  
576 /*
577 * Find beginning of packed-ref record pointed to by p.
578 * buf - a lower-bound pointer to some memory buffer
579 * p - an upper-bound pointer to the same memory buffer
580 */
581 0 static const char *start_of_record(const char *buf, const char *p)
582 {
583 0 const char *nl = p;
584 while (true) {
585 0 nl = git__memrchr(buf, '\n', nl - buf);
586 0 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 0 0 0
587 0 return buf;
588  
589 0 ▶ if (nl[1] == '^' && nl > buf)
Branch: 2 branches
BranchTrueFalseTotal
if (nl[1] == '^' && nl > buf) 0 0 0
if (nl[1] == '^' && nl > buf) 0 0 0
590 0 --nl;
591 else
592 break;
593 };
594 0 return nl + 1;
595 }
596  
597 /*
598 * Find end of packed-ref record pointed to by p.
599 * end - an upper-bound pointer to some memory buffer
600 * p - a lower-bound pointer to the same memory buffer
601 */
602 0 static const char *end_of_record(const char *p, const char *end)
603 {
604 0 while (1) {
605 0 size_t sz = end - p;
606 0 p = memchr(p, '\n', sz);
607 0 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 0 0
608 0 return end;
609 0 ++p;
610 0 ▶ if (p < end && p[0] == '^')
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && p[0] == '^') 0 0 0
if (p < end && p[0] == '^') 0 0 0
611 0 ++p;
612 else
613 break;
614 }
615 0 return p;
616 }
617  
618 static int
619 0 cmp_record_to_refname(const char *rec, size_t data_end, const char *ref_name)
620 {
621 0 const size_t ref_len = strlen(ref_name);
622 int cmp_val;
623 const char *end;
624  
625 0 rec += GIT_OID_HEXSZ + 1; /* <oid> + space */
626 0 ▶ if (data_end < GIT_OID_HEXSZ + 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_end < GIT_OID_HEXSZ + 3) { 0 0 0
627 /* an incomplete (corrupt) record is treated as less than ref_name */
628 0 return -1;
629 }
630 0 data_end -= GIT_OID_HEXSZ + 1;
631  
632 0 end = memchr(rec, '\n', data_end);
633 0 ▶ if (end)
Branch: 1 branch
BranchTrueFalseTotal
if (end) 0 0 0
634 0 data_end = end - rec;
635  
636 0 cmp_val = memcmp(rec, ref_name, min(ref_len, data_end));
637  
638 0 ▶ if (cmp_val == 0 && data_end != ref_len)
Branch: 2 branches
BranchTrueFalseTotal
if (cmp_val == 0 && data_end != ref_len) 0 0 0
if (cmp_val == 0 && data_end != ref_len) 0 0 0
639 0 ▶ return (data_end > ref_len) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return (data_end > ref_len) ? 1 : -1; 0 0 0
640 0 return cmp_val;
641 }
642  
643 166 static int packed_unsorted_lookup(
644 git_reference **out,
645 refdb_fs_backend *backend,
646 const char *ref_name)
647 {
648 166 int error = 0;
649 struct packref *entry;
650  
651 166 ▶ if ((error = packed_reload(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_reload(backend)) < 0) 0 166 166
652 0 return error;
653  
654 166 ▶ if (git_sortedcache_rlock(backend->refcache) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_sortedcache_rlock(backend->refcache) < 0) 0 166 166
655 0 return -1;
656  
657 166 entry = git_sortedcache_lookup(backend->refcache, ref_name);
658 166 ▶ if (!entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) { 166 0 166
659 166 error = ref_error_notfound(ref_name);
660 } else {
661 0 *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel);
662 0 ▶ if (!*out)
Branch: 1 branch
BranchTrueFalseTotal
if (!*out) 0 0 0
663 0 error = -1;
664 }
665  
666 166 git_sortedcache_runlock(backend->refcache);
667  
668 166 return error;
669 }
670  
671 166 static int packed_lookup(
672 git_reference **out,
673 refdb_fs_backend *backend,
674 const char *ref_name)
675 {
676 166 int error = 0;
677 const char *left, *right, *data_end;
678  
679 166 ▶ if ((error = packed_map_check(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_map_check(backend)) < 0) 0 166 166
680 0 return error;
681  
682 166 ▶ if (!backend->sorted)
Branch: 1 branch
BranchTrueFalseTotal
if (!backend->sorted) 166 0 166
683 166 return packed_unsorted_lookup(out, backend, ref_name);
684  
685 0 left = backend->packed_refs_map.data;
686 0 right = data_end = (const char *) backend->packed_refs_map.data +
687 0 backend->packed_refs_map.len;
688  
689 0 ▶ while (left < right && *left == '#') {
Branch: 2 branches
BranchTrueFalseTotal
while (left < right && *left == '#') { 0 0 0
while (left < right && *left == '#') { 0 0 0
690 0 ▶ if (!(left = memchr(left, '\n', data_end - left)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(left = memchr(left, '\n', data_end - left))) 0 0 0
691 0 goto parse_failed;
692 0 left++;
693 }
694  
695 0 ▶ while (left < right) {
Branch: 1 branch
BranchTrueFalseTotal
while (left < right) { 0 0 0
696 const char *mid, *rec;
697 int compare;
698  
699 0 mid = left + (right - left) / 2;
700 0 rec = start_of_record(left, mid);
701 0 compare = cmp_record_to_refname(rec, data_end - rec, ref_name);
702  
703 0 ▶ if (compare < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (compare < 0) { 0 0 0
704 0 left = end_of_record(mid, right);
705 0 ▶ } else if (compare > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compare > 0) { 0 0 0
706 0 right = rec;
707 } else {
708 const char *eol;
709 0 git_oid oid, peel, *peel_ptr = NULL;
710  
711 0 if (data_end - rec < GIT_OID_HEXSZ ||
712 0 git_oid_fromstr(&oid, rec) < 0) {
713 0 goto parse_failed;
714 }
715 0 rec += GIT_OID_HEXSZ + 1;
716 0 ▶ if (!(eol = memchr(rec, '\n', data_end - rec))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(eol = memchr(rec, '\n', data_end - rec))) { 0 0 0
717 0 goto parse_failed;
718 }
719  
720 /* look for optional "^<OID>\n" */
721  
722 0 ▶ if (eol + 1 < data_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (eol + 1 < data_end) { 0 0 0
723 0 rec = eol + 1;
724  
725 0 ▶ if (*rec == '^') {
Branch: 1 branch
BranchTrueFalseTotal
if (*rec == '^') { 0 0 0
726 0 rec++;
727 0 if (data_end - rec < GIT_OID_HEXSZ ||
728 0 git_oid_fromstr(&peel, rec) < 0) {
729 0 goto parse_failed;
730 }
731 0 peel_ptr = &peel;
732 }
733 }
734  
735 0 *out = git_reference__alloc(ref_name, &oid, peel_ptr);
736 0 ▶ if (!*out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*out) { 0 0 0
737 0 return -1;
738 }
739  
740 0 return 0;
741 }
742 }
743 0 return ref_error_notfound(ref_name);
744  
745 0 parse_failed:
746 0 git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
747 0 return -1;
748 }
749  
750 1323 static int refdb_fs_backend__lookup(
751 git_reference **out,
752 git_refdb_backend *_backend,
753 const char *ref_name)
754 {
755 1323 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
756 int error;
757  
758 1323 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 1323 1323
759  
760 1323 ▶ if (!(error = loose_lookup(out, backend, ref_name)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = loose_lookup(out, backend, ref_name))) 1157 166 1323
761 1157 return 0;
762  
763 /* only try to lookup this reference on the packfile if it
764 * wasn't found on the loose refs; not if there was a critical error */
765 166 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 166 0 166
766 166 git_error_clear();
767 166 error = packed_lookup(out, backend, ref_name);
768 }
769 166 return error;
770 }
771  
772 typedef struct {
773 git_reference_iterator parent;
774  
775 char *glob;
776  
777 git_pool pool;
778 git_vector loose;
779  
780 git_sortedcache *cache;
781 size_t loose_pos;
782 size_t packed_pos;
783 } refdb_fs_iter;
784  
785 31 static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
786 {
787 31 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
788  
789 31 git_vector_free(&iter->loose);
790 31 git_pool_clear(&iter->pool);
791 31 git_sortedcache_free(iter->cache);
792 31 git__free(iter);
793 31 }
794  
795 31 static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
796 {
797 31 int error = 0;
798 31 git_str path = GIT_STR_INIT;
799 31 git_iterator *fsit = NULL;
800 31 git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT;
801 31 const git_index_entry *entry = NULL;
802 31 const char *ref_prefix = GIT_REFS_DIR;
803 31 size_t ref_prefix_len = strlen(ref_prefix);
804  
805 31 ▶ if (!backend->commonpath) /* do nothing if no commonpath for loose refs */
Branch: 1 branch
BranchTrueFalseTotal
if (!backend->commonpath) /* do nothing if no commonpath for loose refs */ 0 31 31
806 0 return 0;
807  
808 31 fsit_opts.flags = backend->iterator_flags;
809  
810 31 ▶ if (iter->glob) {
Branch: 1 branch
BranchTrueFalseTotal
if (iter->glob) { 3 28 31
811 3 const char *last_sep = NULL;
812 const char *pos;
813 57 ▶ for (pos = iter->glob; *pos; ++pos) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = iter->glob; *pos; ++pos) { 57 0 57
814 57 switch (*pos) {
815 3 case '?':
816 case '*':
817 case '[':
818 case '\\':
819 3 break;
820 8 case '/':
821 8 last_sep = pos;
822 /* FALLTHROUGH */
823 54 default:
824 54 continue;
825 }
826 3 break;
827 }
828 3 ▶ if (last_sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_sep) { 3 0 3
829 3 ref_prefix = iter->glob;
830 3 ref_prefix_len = (last_sep - ref_prefix) + 1;
831 }
832 }
833  
834 62 if ((error = git_str_puts(&path, backend->commonpath)) < 0 ||
835 31 (error = git_str_put(&path, ref_prefix, ref_prefix_len)) < 0) {
836 0 git_str_dispose(&path);
837 0 return error;
838 }
839  
840 31 ▶ if ((error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) { 2 29 31
841 2 git_str_dispose(&path);
842 2 ▶ return (iter->glob && error == GIT_ENOTFOUND)? 0 : error;
Branch: 2 branches
BranchTrueFalseTotal
return (iter->glob && error == GIT_ENOTFOUND)? 0 : error; 2 0 2
return (iter->glob && error == GIT_ENOTFOUND)? 0 : error; 0 2 2
843 }
844  
845 29 error = git_str_sets(&path, ref_prefix);
846  
847 143 ▶ while (!error && !git_iterator_advance(&entry, fsit)) {
Branch: 2 branches
BranchTrueFalseTotal
while (!error && !git_iterator_advance(&entry, fsit)) { 143 0 143
while (!error && !git_iterator_advance(&entry, fsit)) { 114 29 143
848 const char *ref_name;
849 char *ref_dup;
850  
851 114 git_str_truncate(&path, ref_prefix_len);
852 114 git_str_puts(&path, entry->path);
853 114 ref_name = git_str_cstr(&path);
854  
855 114 ▶ if (git__suffixcmp(ref_name, ".lock") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (git__suffixcmp(ref_name, ".lock") == 0 || 114 0 114
856 114 ▶ (iter->glob && wildmatch(iter->glob, ref_name, 0) != 0))
Branch: 2 branches
BranchTrueFalseTotal
(iter->glob && wildmatch(iter->glob, ref_name, 0) != 0)) 1 113 114
(iter->glob && wildmatch(iter->glob, ref_name, 0) != 0)) 0 1 1
857 0 continue;
858  
859 114 ref_dup = git_pool_strdup(&iter->pool, ref_name);
860 114 ▶ if (!ref_dup)
Branch: 1 branch
BranchTrueFalseTotal
if (!ref_dup) 0 114 114
861 0 error = -1;
862 else
863 114 error = git_vector_insert(&iter->loose, ref_dup);
864 }
865  
866 29 git_iterator_free(fsit);
867 29 git_str_dispose(&path);
868  
869 29 return error;
870 }
871  
872 19 static int refdb_fs_backend__iterator_next(
873 git_reference **out, git_reference_iterator *_iter)
874 {
875 19 int error = GIT_ITEROVER;
876 19 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
877 19 refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
878 struct packref *ref;
879  
880 19 ▶ while (iter->loose_pos < iter->loose.length) {
Branch: 1 branch
BranchTrueFalseTotal
while (iter->loose_pos < iter->loose.length) { 11 8 19
881 11 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
882  
883 11 ▶ if (loose_lookup(out, backend, path) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (loose_lookup(out, backend, path) == 0) { 11 0 11
884 11 ref = git_sortedcache_lookup(iter->cache, path);
885 11 ▶ if (ref)
Branch: 1 branch
BranchTrueFalseTotal
if (ref) 0 11 11
886 0 ref->flags |= PACKREF_SHADOWED;
887  
888 11 return 0;
889 }
890  
891 0 git_error_clear();
892 }
893  
894 8 error = GIT_ITEROVER;
895 8 ▶ while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
Branch: 1 branch
BranchTrueFalseTotal
while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { 0 8 8
896 0 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
897 0 ▶ if (!ref) /* stop now if another thread deleted refs and we past end */
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) /* stop now if another thread deleted refs and we past end */ 0 0 0
898 0 break;
899  
900 0 ▶ if (ref->flags & PACKREF_SHADOWED)
Branch: 1 branch
BranchTrueFalseTotal
if (ref->flags & PACKREF_SHADOWED) 0 0 0
901 0 continue;
902 0 ▶ if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) 0 0 0
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) 0 0 0
903 0 continue;
904  
905 0 *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
906 0 ▶ error = (*out != NULL) ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
error = (*out != NULL) ? 0 : -1; 0 0 0
907 0 break;
908 }
909  
910 8 return error;
911 }
912  
913 125 static int refdb_fs_backend__iterator_next_name(
914 const char **out, git_reference_iterator *_iter)
915 {
916 125 int error = GIT_ITEROVER;
917 125 refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
918 125 refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
919 struct packref *ref;
920  
921 125 ▶ while (iter->loose_pos < iter->loose.length) {
Branch: 1 branch
BranchTrueFalseTotal
while (iter->loose_pos < iter->loose.length) { 103 22 125
922 103 const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
923 struct packref *ref;
924  
925 103 ▶ if (loose_lookup(NULL, backend, path) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (loose_lookup(NULL, backend, path) == 0) { 103 0 103
926 103 ref = git_sortedcache_lookup(iter->cache, path);
927 103 ▶ if (ref)
Branch: 1 branch
BranchTrueFalseTotal
if (ref) 0 103 103
928 0 ref->flags |= PACKREF_SHADOWED;
929  
930 103 *out = path;
931 103 return 0;
932 }
933  
934 0 git_error_clear();
935 }
936  
937 22 error = GIT_ITEROVER;
938 22 ▶ while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
Branch: 1 branch
BranchTrueFalseTotal
while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) { 0 22 22
939 0 ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
940 0 ▶ if (!ref) /* stop now if another thread deleted refs and we past end */
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) /* stop now if another thread deleted refs and we past end */ 0 0 0
941 0 break;
942  
943 0 ▶ if (ref->flags & PACKREF_SHADOWED)
Branch: 1 branch
BranchTrueFalseTotal
if (ref->flags & PACKREF_SHADOWED) 0 0 0
944 0 continue;
945 0 ▶ if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) 0 0 0
if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0) 0 0 0
946 0 continue;
947  
948 0 *out = ref->name;
949 0 error = 0;
950 0 break;
951 }
952  
953 22 return error;
954 }
955  
956 31 static int refdb_fs_backend__iterator(
957 git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
958 {
959 31 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
960 31 refdb_fs_iter *iter = NULL;
961 int error;
962  
963 31 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 31 31
964  
965 31 iter = git__calloc(1, sizeof(refdb_fs_iter));
966 31 ▶ GIT_ERROR_CHECK_ALLOC(iter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(iter); 0 31 31
967  
968 31 ▶ if ((error = git_pool_init(&iter->pool, 1)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_pool_init(&iter->pool, 1)) < 0) 0 31 31
969 0 goto out;
970  
971 31 ▶ if ((error = git_vector_init(&iter->loose, 8, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_init(&iter->loose, 8, NULL)) < 0) 0 31 31
972 0 goto out;
973  
974 31 ▶ if (glob != NULL &&
Branch: 1 branch
BranchTrueFalseTotal
if (glob != NULL && 3 28 31
975 3 ▶ (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
(iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) { 0 3 3
976 0 error = GIT_ERROR_NOMEMORY;
977 0 goto out;
978 }
979  
980 31 ▶ if ((error = iter_load_loose_paths(backend, iter)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = iter_load_loose_paths(backend, iter)) < 0) 0 31 31
981 0 goto out;
982  
983 31 ▶ if ((error = packed_reload(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_reload(backend)) < 0) 0 31 31
984 0 goto out;
985  
986 31 ▶ if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0) 0 31 31
987 0 goto out;
988  
989 31 iter->parent.next = refdb_fs_backend__iterator_next;
990 31 iter->parent.next_name = refdb_fs_backend__iterator_next_name;
991 31 iter->parent.free = refdb_fs_backend__iterator_free;
992  
993 31 *out = (git_reference_iterator *)iter;
994 31 out:
995 31 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 31 31
996 0 refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
997 31 return error;
998 }
999  
1000 0 static bool ref_is_available(
1001 const char *old_ref, const char *new_ref, const char *this_ref)
1002 {
1003 0 ▶ if (old_ref == NULL || strcmp(old_ref, this_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (old_ref == NULL || strcmp(old_ref, this_ref)) { 0 0 0
if (old_ref == NULL || strcmp(old_ref, this_ref)) { 0 0 0
1004 0 size_t reflen = strlen(this_ref);
1005 0 size_t newlen = strlen(new_ref);
1006 0 size_t cmplen = reflen < newlen ? reflen : newlen;
1007 0 ▶ const char *lead = reflen < newlen ? new_ref : this_ref;
Branch: 1 branch
BranchTrueFalseTotal
const char *lead = reflen < newlen ? new_ref : this_ref; 0 0 0
1008  
1009 0 ▶ if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') { 0 0 0
if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') { 0 0 0
1010 0 return false;
1011 }
1012 }
1013  
1014 0 return true;
1015 }
1016  
1017 100 static int reference_path_available(
1018 refdb_fs_backend *backend,
1019 const char *new_ref,
1020 const char *old_ref,
1021 int force)
1022 {
1023 size_t i;
1024 int error;
1025  
1026 100 ▶ if ((error = packed_reload(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_reload(backend)) < 0) 0 100 100
1027 0 return error;
1028  
1029 100 ▶ if (!force) {
Branch: 1 branch
BranchTrueFalseTotal
if (!force) { 30 70 100
1030 int exists;
1031  
1032 30 ▶ if ((error = refdb_fs_backend__exists(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = refdb_fs_backend__exists( 0 30 30
1033 &exists, (git_refdb_backend *)backend, new_ref)) < 0) {
1034 2 return error;
1035 }
1036  
1037 30 ▶ if (exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists) { 2 28 30
1038 2 git_error_set(GIT_ERROR_REFERENCE,
1039 "failed to write reference '%s': a reference with "
1040 "that name already exists.", new_ref);
1041 2 return GIT_EEXISTS;
1042 }
1043 }
1044  
1045 98 ▶ if ((error = git_sortedcache_rlock(backend->refcache)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_rlock(backend->refcache)) < 0) 0 98 98
1046 0 return error;
1047  
1048 98 ▶ for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { 0 98 98
1049 0 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
1050  
1051 0 ▶ if (ref && !ref_is_available(old_ref, new_ref, ref->name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { 0 0 0
if (ref && !ref_is_available(old_ref, new_ref, ref->name)) { 0 0 0
1052 0 git_sortedcache_runlock(backend->refcache);
1053 0 git_error_set(GIT_ERROR_REFERENCE,
1054 "path to reference '%s' collides with existing one", new_ref);
1055 0 return -1;
1056 }
1057 }
1058  
1059 98 git_sortedcache_runlock(backend->refcache);
1060 98 return 0;
1061 }
1062  
1063 108 static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
1064 {
1065 int error, filebuf_flags;
1066 108 git_str ref_path = GIT_STR_INIT;
1067 const char *basedir;
1068  
1069 108 ▶ GIT_ASSERT_ARG(file);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(file); 0 108 108
1070 108 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 108 108
1071 108 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 108 108
1072  
1073 108 ▶ if (!git_path_is_valid(backend->repo, name, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_path_is_valid(backend->repo, name, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) { 0 108 108
1074 0 git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", name);
1075 0 return GIT_EINVALIDSPEC;
1076 }
1077  
1078 108 ▶ if (is_per_worktree_ref(name))
Branch: 1 branch
BranchTrueFalseTotal
if (is_per_worktree_ref(name)) 27 81 108
1079 27 basedir = backend->gitpath;
1080 else
1081 81 basedir = backend->commonpath;
1082  
1083 /* Remove a possibly existing empty directory hierarchy
1084 * which name would collide with the reference name
1085 */
1086 108 ▶ if ((error = git_futils_rmdir_r(name, basedir, GIT_RMDIR_SKIP_NONEMPTY)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_futils_rmdir_r(name, basedir, GIT_RMDIR_SKIP_NONEMPTY)) < 0) 0 108 108
1087 0 return error;
1088  
1089 108 ▶ if ((error = loose_path(&ref_path, basedir, name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&ref_path, basedir, name)) < 0) 0 108 108
1090 0 return error;
1091  
1092 108 filebuf_flags = GIT_FILEBUF_CREATE_LEADING_DIRS;
1093 108 ▶ if (backend->fsync)
Branch: 1 branch
BranchTrueFalseTotal
if (backend->fsync) 0 108 108
1094 0 filebuf_flags |= GIT_FILEBUF_FSYNC;
1095  
1096 108 error = git_filebuf_open(file, ref_path.ptr, filebuf_flags, GIT_REFS_FILE_MODE);
1097  
1098 108 ▶ if (error == GIT_EDIRECTORY)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EDIRECTORY) 0 108 108
1099 0 git_error_set(GIT_ERROR_REFERENCE, "cannot lock ref '%s', there are refs beneath that folder", name);
1100  
1101 108 git_str_dispose(&ref_path);
1102 108 return error;
1103 }
1104  
1105 93 static int loose_commit(git_filebuf *file, const git_reference *ref)
1106 {
1107 93 ▶ GIT_ASSERT_ARG(file);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(file); 0 93 93
1108 93 ▶ GIT_ASSERT_ARG(ref);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ref); 0 93 93
1109  
1110 93 ▶ if (ref->type == GIT_REFERENCE_DIRECT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref->type == GIT_REFERENCE_DIRECT) { 73 20 93
1111 char oid[GIT_OID_HEXSZ + 1];
1112 73 git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
1113  
1114 73 git_filebuf_printf(file, "%s\n", oid);
1115 20 ▶ } else if (ref->type == GIT_REFERENCE_SYMBOLIC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ref->type == GIT_REFERENCE_SYMBOLIC) { 20 0 20
1116 20 git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
1117 } else {
1118 0 GIT_ASSERT(0);
1119 }
1120  
1121 93 return git_filebuf_commit(file);
1122 }
1123  
1124 3 static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname)
1125 {
1126 int error;
1127 git_filebuf *lock;
1128 3 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1129  
1130 3 lock = git__calloc(1, sizeof(git_filebuf));
1131 3 ▶ GIT_ERROR_CHECK_ALLOC(lock);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(lock); 0 3 3
1132  
1133 3 ▶ if ((error = loose_lock(lock, backend, refname)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_lock(lock, backend, refname)) < 0) { 0 3 3
1134 0 git__free(lock);
1135 0 return error;
1136 }
1137  
1138 3 *out = lock;
1139 3 return 0;
1140 }
1141  
1142 static int refdb_fs_backend__write_tail(
1143 git_refdb_backend *_backend,
1144 const git_reference *ref,
1145 git_filebuf *file,
1146 int update_reflog,
1147 const git_oid *old_id,
1148 const char *old_target,
1149 const git_signature *who,
1150 const char *message);
1151  
1152 static int refdb_fs_backend__delete_tail(
1153 git_refdb_backend *_backend,
1154 git_filebuf *file,
1155 const char *ref_name,
1156 const git_oid *old_id,
1157 const char *old_target);
1158  
1159 3 static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog,
1160 const git_reference *ref, const git_signature *sig, const char *message)
1161 {
1162 3 git_filebuf *lock = (git_filebuf *) payload;
1163 3 int error = 0;
1164  
1165 3 ▶ if (success == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (success == 2) 1 2 3
1166 1 error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL);
1167 2 ▶ else if (success)
Branch: 1 branch
BranchTrueFalseTotal
else if (success) 2 0 2
1168 2 error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, NULL, NULL, sig, message);
1169 else
1170 0 git_filebuf_cleanup(lock);
1171  
1172 3 git__free(lock);
1173 3 return error;
1174 }
1175  
1176 /*
1177 * Find out what object this reference resolves to.
1178 *
1179 * For references that point to a 'big' tag (e.g. an
1180 * actual tag object on the repository), we need to
1181 * cache on the packfile the OID of the object to
1182 * which that 'big tag' is pointing to.
1183 */
1184 0 static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
1185 {
1186 git_object *object;
1187  
1188 0 ▶ if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL)
Branch: 2 branches
BranchTrueFalseTotal
if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL) 0 0 0
if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL) 0 0 0
1189 0 return 0;
1190  
1191 /*
1192 * Find the tagged object in the repository
1193 */
1194 0 ▶ if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJECT_ANY) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJECT_ANY) < 0) 0 0 0
1195 0 return -1;
1196  
1197 /*
1198 * If the tagged object is a Tag object, we need to resolve it;
1199 * if the ref is actually a 'weak' ref, we don't need to resolve
1200 * anything.
1201 */
1202 0 ▶ if (git_object_type(object) == GIT_OBJECT_TAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_object_type(object) == GIT_OBJECT_TAG) { 0 0 0
1203 0 git_tag *tag = (git_tag *)object;
1204  
1205 /*
1206 * Find the object pointed at by this tag
1207 */
1208 0 git_oid_cpy(&ref->peel, git_tag_target_id(tag));
1209 0 ref->flags |= PACKREF_HAS_PEEL;
1210  
1211 /*
1212 * The reference has now cached the resolved OID, and is
1213 * marked at such. When written to the packfile, it'll be
1214 * accompanied by this resolved oid
1215 */
1216 }
1217  
1218 0 git_object_free(object);
1219 0 return 0;
1220 }
1221  
1222 /*
1223 * Write a single reference into a packfile
1224 */
1225 0 static int packed_write_ref(struct packref *ref, git_filebuf *file)
1226 {
1227 char oid[GIT_OID_HEXSZ + 1];
1228 0 git_oid_nfmt(oid, sizeof(oid), &ref->oid);
1229  
1230 /*
1231 * For references that peel to an object in the repo, we must
1232 * write the resulting peel on a separate line, e.g.
1233 *
1234 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
1235 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
1236 *
1237 * This obviously only applies to tags.
1238 * The required peels have already been loaded into `ref->peel_target`.
1239 */
1240 0 ▶ if (ref->flags & PACKREF_HAS_PEEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref->flags & PACKREF_HAS_PEEL) { 0 0 0
1241 char peel[GIT_OID_HEXSZ + 1];
1242 0 git_oid_nfmt(peel, sizeof(peel), &ref->peel);
1243  
1244 0 ▶ if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0) 0 0 0
1245 0 return -1;
1246 } else {
1247 0 ▶ if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0) 0 0 0
1248 0 return -1;
1249 }
1250  
1251 0 return 0;
1252 }
1253  
1254 /*
1255 * Remove all loose references
1256 *
1257 * Once we have successfully written a packfile,
1258 * all the loose references that were packed must be
1259 * removed from disk.
1260 *
1261 * This is a dangerous method; make sure the packfile
1262 * is well-written, because we are destructing references
1263 * here otherwise.
1264 */
1265 0 static int packed_remove_loose(refdb_fs_backend *backend)
1266 {
1267 size_t i;
1268 0 git_filebuf lock = GIT_FILEBUF_INIT;
1269 0 git_str ref_content = GIT_STR_INIT;
1270 0 int error = 0;
1271  
1272 /* backend->refcache is already locked when this is called */
1273  
1274 0 ▶ for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) { 0 0 0
1275 0 struct packref *ref = git_sortedcache_entry(backend->refcache, i);
1276 git_oid current_id;
1277  
1278 0 ▶ if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
Branch: 2 branches
BranchTrueFalseTotal
if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) 0 0 0
if (!ref || !(ref->flags & PACKREF_WAS_LOOSE)) 0 0 0
1279 0 continue;
1280  
1281 0 git_filebuf_cleanup(&lock);
1282  
1283 /* We need to stop anybody from updating the ref while we try to do a safe delete */
1284 0 error = loose_lock(&lock, backend, ref->name);
1285 /* If someone else is updating it, let them do it */
1286 0 ▶ if (error == GIT_EEXISTS || error == GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error == GIT_EEXISTS || error == GIT_ENOTFOUND) 0 0 0
if (error == GIT_EEXISTS || error == GIT_ENOTFOUND) 0 0 0
1287 0 continue;
1288  
1289 0 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 0 0
1290 0 git_str_dispose(&ref_content);
1291 0 git_error_set(GIT_ERROR_REFERENCE, "failed to lock loose reference '%s'", ref->name);
1292 0 return error;
1293 }
1294  
1295 0 error = git_futils_readbuffer(&ref_content, lock.path_original);
1296 /* Someone else beat us to cleaning up the ref, let's simply continue */
1297 0 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 0 0
1298 0 continue;
1299  
1300 /* This became a symref between us packing and trying to delete it, so ignore it */
1301 0 ▶ if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) 0 0 0
1302 0 continue;
1303  
1304 /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
1305 0 ▶ if (loose_parse_oid(&current_id, lock.path_original, &ref_content) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (loose_parse_oid(&current_id, lock.path_original, &ref_content) < 0) 0 0 0
1306 0 continue;
1307  
1308 /* If the ref moved since we packed it, we must not delete it */
1309 0 ▶ if (!git_oid_equal(&current_id, &ref->oid))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_oid_equal(&current_id, &ref->oid)) 0 0 0
1310 0 continue;
1311  
1312 /*
1313 * if we fail to remove a single file, this is *not* good,
1314 * but we should keep going and remove as many as possible.
1315 * If we fail to remove, the ref is still in the old state, so
1316 * we haven't lost information.
1317 */
1318 0 p_unlink(lock.path_original);
1319 }
1320  
1321 0 git_str_dispose(&ref_content);
1322 0 git_filebuf_cleanup(&lock);
1323 0 return 0;
1324 }
1325  
1326 /*
1327 * Write all the contents in the in-memory packfile to disk.
1328 */
1329 0 static int packed_write(refdb_fs_backend *backend)
1330 {
1331 0 git_sortedcache *refcache = backend->refcache;
1332 0 git_filebuf pack_file = GIT_FILEBUF_INIT;
1333 0 int error, open_flags = 0;
1334 size_t i;
1335  
1336 /* take lock and close up packed-refs mmap if open */
1337 0 ▶ if ((error = git_mutex_lock(&backend->prlock)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_mutex_lock(&backend->prlock)) < 0) { 0 0 0
1338 0 return error;
1339 }
1340  
1341 0 packed_map_free(backend);
1342  
1343 0 git_mutex_unlock(&backend->prlock);
1344  
1345 /* lock the cache to updates while we do this */
1346 0 ▶ if ((error = git_sortedcache_wlock(refcache)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_wlock(refcache)) < 0) 0 0 0
1347 0 return error;
1348  
1349 0 ▶ if (backend->fsync)
Branch: 1 branch
BranchTrueFalseTotal
if (backend->fsync) 0 0 0
1350 0 open_flags = GIT_FILEBUF_FSYNC;
1351  
1352 /* Open the file! */
1353 0 ▶ if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0) 0 0 0
1354 0 goto fail;
1355  
1356 /* Packfiles have a header... apparently
1357 * This is in fact not required, but we might as well print it
1358 * just for kicks */
1359 0 ▶ if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0) 0 0 0
1360 0 goto fail;
1361  
1362 0 ▶ for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) { 0 0 0
1363 0 struct packref *ref = git_sortedcache_entry(refcache, i);
1364  
1365 0 ▶ GIT_ASSERT_WITH_CLEANUP(ref, {
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_WITH_CLEANUP(ref, { 0 0 0
1366 error = -1;
1367 goto fail;
1368 });
1369  
1370 0 ▶ if ((error = packed_find_peel(backend, ref)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_find_peel(backend, ref)) < 0) 0 0 0
1371 0 goto fail;
1372  
1373 0 ▶ if ((error = packed_write_ref(ref, &pack_file)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_write_ref(ref, &pack_file)) < 0) 0 0 0
1374 0 goto fail;
1375 }
1376  
1377 /* if we've written all the references properly, we can commit
1378 * the packfile to make the changes effective */
1379 0 ▶ if ((error = git_filebuf_commit(&pack_file)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_commit(&pack_file)) < 0) 0 0 0
1380 0 goto fail;
1381  
1382 /* when and only when the packfile has been properly written,
1383 * we can go ahead and remove the loose refs */
1384 0 ▶ if ((error = packed_remove_loose(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_remove_loose(backend)) < 0) 0 0 0
1385 0 goto fail;
1386  
1387 0 git_sortedcache_updated(refcache);
1388 0 git_sortedcache_wunlock(refcache);
1389  
1390 /* we're good now */
1391 0 return 0;
1392  
1393 0 fail:
1394 0 git_filebuf_cleanup(&pack_file);
1395 0 git_sortedcache_wunlock(refcache);
1396  
1397 0 return error;
1398 }
1399  
1400 8 static int packed_delete(refdb_fs_backend *backend, const char *ref_name)
1401 {
1402 size_t pack_pos;
1403 8 int error, found = 0;
1404  
1405 8 ▶ if ((error = packed_reload(backend)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = packed_reload(backend)) < 0) 0 8 8
1406 0 goto cleanup;
1407  
1408 8 ▶ if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sortedcache_wlock(backend->refcache)) < 0) 0 8 8
1409 0 goto cleanup;
1410  
1411 /* If a packed reference exists, remove it from the packfile and repack if necessary */
1412 8 error = git_sortedcache_lookup_index(&pack_pos, backend->refcache, ref_name);
1413 8 ▶ if (error == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == 0) { 0 8 8
1414 0 error = git_sortedcache_remove(backend->refcache, pack_pos);
1415 0 found = 1;
1416 }
1417 8 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 8 0 8
1418 8 error = 0;
1419  
1420 8 git_sortedcache_wunlock(backend->refcache);
1421  
1422 8 ▶ if (found)
Branch: 1 branch
BranchTrueFalseTotal
if (found) 8 0 8
1423 0 error = packed_write(backend);
1424  
1425 8 cleanup:
1426 8 return error;
1427 }
1428  
1429 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);
1430  
1431 206 static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
1432 const git_oid *old_id, const char *old_target)
1433 {
1434 206 int error = 0;
1435 206 git_reference *old_ref = NULL;
1436  
1437 206 *cmp = 0;
1438 /* It "matches" if there is no old value to compare against */
1439 206 ▶ if (!old_id && !old_target)
Branch: 2 branches
BranchTrueFalseTotal
if (!old_id && !old_target) 93 113 206
if (!old_id && !old_target) 69 24 93
1440 69 return 0;
1441  
1442 137 ▶ if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0) { 31 106 137
1443 31 ▶ if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id))
Branch: 3 branches
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id)) 31 0 31
if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id)) 29 2 31
if (error == GIT_ENOTFOUND && old_id && git_oid_is_zero(old_id)) 0 29 29
1444 0 return 0;
1445 31 goto out;
1446 }
1447  
1448 /* If the types don't match, there's no way the values do */
1449 106 ▶ if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) {
Branch: 2 branches
BranchTrueFalseTotal
if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) { 84 22 106
if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) { 2 82 84
1450 2 *cmp = -1;
1451 2 goto out;
1452 }
1453 104 ▶ if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) { 22 82 104
if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) { 1 21 22
1454 1 *cmp = 1;
1455 1 goto out;
1456 }
1457  
1458 103 ▶ if (old_id && old_ref->type == GIT_REFERENCE_DIRECT)
Branch: 2 branches
BranchTrueFalseTotal
if (old_id && old_ref->type == GIT_REFERENCE_DIRECT) 82 21 103
if (old_id && old_ref->type == GIT_REFERENCE_DIRECT) 82 0 82
1459 82 *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
1460  
1461 103 ▶ if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC)
Branch: 2 branches
BranchTrueFalseTotal
if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC) 82 21 103
if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC) 0 21 21
1462 21 *cmp = git__strcmp(old_target, old_ref->target.symbolic);
1463  
1464 82 out:
1465 137 git_reference_free(old_ref);
1466  
1467 137 return error;
1468 }
1469  
1470 /*
1471 * The git.git comment regarding this, for your viewing pleasure:
1472 *
1473 * Special hack: If a branch is updated directly and HEAD
1474 * points to it (may happen on the remote side of a push
1475 * for example) then logically the HEAD reflog should be
1476 * updated too.
1477 * A generic solution implies reverse symref information,
1478 * but finding all symrefs pointing to the given branch
1479 * would be rather costly for this rare event (the direct
1480 * update of a branch) to be worth it. So let's cheat and
1481 * check with HEAD only which should cover 99% of all usage
1482 * scenarios (even 100% of the default ones).
1483 */
1484 80 static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
1485 {
1486 80 git_reference *head = NULL;
1487 80 git_refdb *refdb = NULL;
1488 int error, write_reflog;
1489 git_oid old_id;
1490  
1491 160 if ((error = git_repository_refdb(&refdb, backend->repo)) < 0 ||
1492 80 (error = git_refdb_should_write_head_reflog(&write_reflog, refdb, ref)) < 0)
1493 0 goto out;
1494 80 ▶ if (!write_reflog)
Branch: 1 branch
BranchTrueFalseTotal
if (!write_reflog) 50 30 80
1495 50 goto out;
1496  
1497 /* if we can't resolve, we use {0}*40 as old id */
1498 30 ▶ if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0) 3 27 30
1499 3 memset(&old_id, 0, sizeof(old_id));
1500  
1501 60 if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0 ||
1502 30 (error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message)) < 0)
1503 0 goto out;
1504  
1505 30 out:
1506 80 git_reference_free(head);
1507 80 git_refdb_free(refdb);
1508 80 return error;
1509 }
1510  
1511 99 static int refdb_fs_backend__write(
1512 git_refdb_backend *_backend,
1513 const git_reference *ref,
1514 int force,
1515 const git_signature *who,
1516 const char *message,
1517 const git_oid *old_id,
1518 const char *old_target)
1519 {
1520 99 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1521 99 git_filebuf file = GIT_FILEBUF_INIT;
1522 99 int error = 0;
1523  
1524 99 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 99 99
1525  
1526 99 ▶ if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0) 2 97 99
1527 2 return error;
1528  
1529 /* We need to perform the reflog append and old value check under the ref's lock */
1530 97 ▶ if ((error = loose_lock(&file, backend, ref->name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_lock(&file, backend, ref->name)) < 0) 0 97 97
1531 0 return error;
1532  
1533 97 return refdb_fs_backend__write_tail(_backend, ref, &file, true, old_id, old_target, who, message);
1534 }
1535  
1536 99 static int refdb_fs_backend__write_tail(
1537 git_refdb_backend *_backend,
1538 const git_reference *ref,
1539 git_filebuf *file,
1540 int update_reflog,
1541 const git_oid *old_id,
1542 const char *old_target,
1543 const git_signature *who,
1544 const char *message)
1545 {
1546 99 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1547 99 int error = 0, cmp = 0, should_write;
1548 99 const char *new_target = NULL;
1549 99 const git_oid *new_id = NULL;
1550  
1551 99 ▶ if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0) 0 99 99
1552 0 goto on_error;
1553  
1554 99 ▶ if (cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) { 0 99 99
1555 0 git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
1556 0 error = GIT_EMODIFIED;
1557 0 goto on_error;
1558 }
1559  
1560 99 ▶ if (ref->type == GIT_REFERENCE_SYMBOLIC)
Branch: 1 branch
BranchTrueFalseTotal
if (ref->type == GIT_REFERENCE_SYMBOLIC) 22 77 99
1561 22 new_target = ref->target.symbolic;
1562 else
1563 77 new_id = &ref->target.oid;
1564  
1565 99 error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
1566 99 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 31 68 99
if (error < 0 && error != GIT_ENOTFOUND) 0 31 31
1567 0 goto on_error;
1568  
1569 /* Don't update if we have the same value */
1570 99 ▶ if (!error && !cmp) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && !cmp) { 68 31 99
if (!error && !cmp) { 7 61 68
1571 7 error = 0;
1572 7 goto on_error; /* not really error */
1573 }
1574  
1575 92 ▶ if (update_reflog) {
Branch: 1 branch
BranchTrueFalseTotal
if (update_reflog) { 90 2 92
1576 git_refdb *refdb;
1577  
1578 180 if ((error = git_repository_refdb__weakptr(&refdb, backend->repo)) < 0 ||
1579 90 (error = git_refdb_should_write_reflog(&should_write, refdb, ref)) < 0)
1580 0 goto on_error;
1581  
1582 90 ▶ if (should_write) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_write) { 80 10 90
1583 80 ▶ if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0) 0 80 80
1584 0 goto on_error;
1585 80 ▶ if ((error = maybe_append_head(backend, ref, who, message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = maybe_append_head(backend, ref, who, message)) < 0) 0 80 80
1586 0 goto on_error;
1587 }
1588 }
1589  
1590 92 return loose_commit(file, ref);
1591  
1592 7 on_error:
1593 7 git_filebuf_cleanup(file);
1594 7 return error;
1595 }
1596  
1597 11 static int refdb_fs_backend__prune_refs(
1598 refdb_fs_backend *backend,
1599 const char *ref_name,
1600 const char *prefix)
1601 {
1602 11 git_str relative_path = GIT_STR_INIT;
1603 11 git_str base_path = GIT_STR_INIT;
1604 size_t commonlen;
1605 int error;
1606  
1607 11 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 11 11
1608 11 ▶ GIT_ASSERT_ARG(ref_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ref_name); 0 11 11
1609  
1610 11 ▶ if ((error = git_str_sets(&relative_path, ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_sets(&relative_path, ref_name)) < 0) 0 11 11
1611 0 goto cleanup;
1612  
1613 11 git_fs_path_squash_slashes(&relative_path);
1614 17 if ((commonlen = git_fs_path_common_dirlen("refs/heads/", git_str_cstr(&relative_path))) == strlen("refs/heads/") ||
1615 10 ▶ (commonlen = git_fs_path_common_dirlen("refs/tags/", git_str_cstr(&relative_path))) == strlen("refs/tags/") ||
Branch: 1 branch
BranchTrueFalseTotal
(commonlen = git_fs_path_common_dirlen("refs/tags/", git_str_cstr(&relative_path))) == strlen("refs/tags/") || 4 0 4
1616 4 (commonlen = git_fs_path_common_dirlen("refs/remotes/", git_str_cstr(&relative_path))) == strlen("refs/remotes/")) {
1617  
1618 7 git_str_truncate(&relative_path, commonlen);
1619  
1620 7 ▶ if (prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (prefix) 7 0 7
1621 7 error = git_str_join3(&base_path, '/',
1622 7 backend->commonpath, prefix,
1623 git_str_cstr(&relative_path));
1624 else
1625 0 error = git_str_joinpath(&base_path,
1626 0 backend->commonpath,
1627 git_str_cstr(&relative_path));
1628  
1629 7 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 7 0 7
1630 7 error = git_path_validate_str_length(NULL, &base_path);
1631  
1632 7 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 7 7
1633 0 goto cleanup;
1634  
1635 7 error = git_futils_rmdir_r(ref_name + commonlen,
1636 git_str_cstr(&base_path),
1637 GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT);
1638  
1639 7 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 7 0 7
1640 0 error = 0;
1641 }
1642  
1643 11 cleanup:
1644 11 git_str_dispose(&relative_path);
1645 11 git_str_dispose(&base_path);
1646 11 return error;
1647 }
1648  
1649 7 static int refdb_fs_backend__delete(
1650 git_refdb_backend *_backend,
1651 const char *ref_name,
1652 const git_oid *old_id, const char *old_target)
1653 {
1654 7 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1655 7 git_filebuf file = GIT_FILEBUF_INIT;
1656 7 int error = 0;
1657  
1658 7 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 7 7
1659 7 ▶ GIT_ASSERT_ARG(ref_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ref_name); 0 7 7
1660  
1661 7 ▶ if ((error = loose_lock(&file, backend, ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_lock(&file, backend, ref_name)) < 0) 0 7 7
1662 0 return error;
1663  
1664 7 ▶ if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) { 0 7 7
1665 0 git_filebuf_cleanup(&file);
1666 0 return error;
1667 }
1668  
1669 7 return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target);
1670 }
1671  
1672 8 static int loose_delete(refdb_fs_backend *backend, const char *ref_name)
1673 {
1674 8 git_str path = GIT_STR_INIT;
1675 8 int error = 0;
1676  
1677 8 ▶ if ((error = loose_path(&path, backend->commonpath, ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&path, backend->commonpath, ref_name)) < 0) 0 8 8
1678 0 return error;
1679  
1680 8 error = p_unlink(path.ptr);
1681 8 ▶ if (error < 0 && errno == ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && errno == ENOENT) 0 8 8
if (error < 0 && errno == ENOENT) 0 0 0
1682 0 error = GIT_ENOTFOUND;
1683 8 ▶ else if (error != 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (error != 0) 0 8 8
1684 0 error = -1;
1685  
1686 8 git_str_dispose(&path);
1687  
1688 8 return error;
1689 }
1690  
1691 8 static int refdb_fs_backend__delete_tail(
1692 git_refdb_backend *_backend,
1693 git_filebuf *file,
1694 const char *ref_name,
1695 const git_oid *old_id, const char *old_target)
1696 {
1697 8 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1698 8 int error = 0, cmp = 0;
1699 8 bool packed_deleted = 0;
1700  
1701 8 error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
1702 8 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 8 8
1703 0 goto cleanup;
1704  
1705 8 ▶ if (cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) { 0 8 8
1706 0 git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
1707 0 error = GIT_EMODIFIED;
1708 0 goto cleanup;
1709 }
1710  
1711 /*
1712 * To ensure that an external observer will see either the current ref value
1713 * (because the loose ref still exists), or a missing ref (after the packed-file is
1714 * unlocked, there will be nothing left), we must ensure things happen in the
1715 * following order:
1716 *
1717 * - the packed-ref file is locked and loaded, as well as a loose one, if it exists
1718 * - we optimistically delete a packed ref, keeping track of whether it existed
1719 * - we delete the loose ref, note that we have its .lock
1720 * - the loose ref is "unlocked", then the packed-ref file is rewritten and unlocked
1721 * - we should prune the path components if a loose ref was deleted
1722 *
1723 * Note that, because our packed backend doesn't expose its filesystem lock,
1724 * we might not be able to guarantee that this is what actually happens (ie.
1725 * as our current code never write packed-refs.lock, nothing stops observers
1726 * from grabbing a "stale" value from there).
1727 */
1728 8 ▶ if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) 0 8 8
if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) 0 0 0
1729 0 goto cleanup;
1730  
1731 8 ▶ if (error == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error == 0) 8 0 8
1732 8 packed_deleted = 1;
1733  
1734 8 ▶ if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) 0 8 8
if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND) 0 0 0
1735 0 goto cleanup;
1736  
1737 8 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 8 0 8
1738 0 ▶ error = packed_deleted ? 0 : ref_error_notfound(ref_name);
Branch: 1 branch
BranchTrueFalseTotal
error = packed_deleted ? 0 : ref_error_notfound(ref_name); 0 0 0
1739 0 goto cleanup;
1740 }
1741  
1742 8 cleanup:
1743 8 git_filebuf_cleanup(file);
1744 8 ▶ if (error == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error == 0) 8 0 8
1745 8 error = refdb_fs_backend__prune_refs(backend, ref_name, "");
1746 8 return error;
1747 }
1748  
1749 static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
1750  
1751 1 static int refdb_fs_backend__rename(
1752 git_reference **out,
1753 git_refdb_backend *_backend,
1754 const char *old_name,
1755 const char *new_name,
1756 int force,
1757 const git_signature *who,
1758 const char *message)
1759 {
1760 1 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1761 1 git_reference *old, *new = NULL;
1762 1 git_filebuf file = GIT_FILEBUF_INIT;
1763 int error;
1764  
1765 1 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 1 1
1766  
1767 1 ▶ if ((error = reference_path_available(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reference_path_available( 1 0 1
1768 1 ▶ backend, new_name, old_name, force)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
backend, new_name, old_name, force)) < 0 || 0 1 1
1769 1 (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
1770 0 return error;
1771  
1772 1 ▶ if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) { 0 1 1
1773 0 git_reference_free(old);
1774 0 return error;
1775 }
1776  
1777 1 new = git_reference__realloc(&old, new_name);
1778 1 ▶ if (!new) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new) { 0 1 1
1779 0 git_reference_free(old);
1780 0 return -1;
1781 }
1782  
1783 1 ▶ if ((error = loose_lock(&file, backend, new->name)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_lock(&file, backend, new->name)) < 0) { 0 1 1
1784 0 git_reference_free(new);
1785 0 return error;
1786 }
1787  
1788 /* Try to rename the refog; it's ok if the old doesn't exist */
1789 1 error = refdb_reflog_fs__rename(_backend, old_name, new_name);
1790 2 ▶ if (((error == 0) || (error == GIT_ENOTFOUND)) &&
Branch: 1 branch
BranchTrueFalseTotal
if (((error == 0) || (error == GIT_ENOTFOUND)) && 1 0 1
1791 1 ((error = reflog_append(backend, new, git_reference_target(new), NULL, who, message)) < 0)) {
1792 0 git_reference_free(new);
1793 0 git_filebuf_cleanup(&file);
1794 0 return error;
1795 }
1796  
1797 1 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 1 1
1798 0 git_reference_free(new);
1799 0 git_filebuf_cleanup(&file);
1800 0 return error;
1801 }
1802  
1803  
1804 1 ▶ if ((error = loose_commit(&file, new)) < 0 || out == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if ((error = loose_commit(&file, new)) < 0 || out == NULL) { 1 0 1
if ((error = loose_commit(&file, new)) < 0 || out == NULL) { 0 1 1
1805 0 git_reference_free(new);
1806 0 return error;
1807 }
1808  
1809 1 *out = new;
1810 1 return 0;
1811 }
1812  
1813 0 static int refdb_fs_backend__compress(git_refdb_backend *_backend)
1814 {
1815 int error;
1816 0 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1817  
1818 0 ▶ GIT_ASSERT_ARG(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(backend); 0 0 0
1819  
1820 0 if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */
1821 0 ▶ (error = packed_loadloose(backend)) < 0 || /* add all the loose refs */
Branch: 1 branch
BranchTrueFalseTotal
(error = packed_loadloose(backend)) < 0 || /* add all the loose refs */ 0 0 0
1822 0 (error = packed_write(backend)) < 0) /* write back to disk */
1823 0 return error;
1824  
1825 0 return 0;
1826 }
1827  
1828 49 static void refdb_fs_backend__free(git_refdb_backend *_backend)
1829 {
1830 49 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1831  
1832 49 ▶ if (!backend)
Branch: 1 branch
BranchTrueFalseTotal
if (!backend) 0 49 49
1833 0 return;
1834  
1835 49 git_sortedcache_free(backend->refcache);
1836  
1837 49 git_mutex_lock(&backend->prlock);
1838 49 packed_map_free(backend);
1839 49 git_mutex_unlock(&backend->prlock);
1840 49 git_mutex_free(&backend->prlock);
1841  
1842 49 git__free(backend->gitpath);
1843 49 git__free(backend->commonpath);
1844 49 git__free(backend);
1845 }
1846  
1847 108 static char *setup_namespace(git_repository *repo, const char *in)
1848 {
1849 108 git_str path = GIT_STR_INIT;
1850 108 char *parts, *start, *end, *out = NULL;
1851  
1852 108 ▶ if (!in)
Branch: 1 branch
BranchTrueFalseTotal
if (!in) 0 108 108
1853 0 goto done;
1854  
1855 108 git_str_puts(&path, in);
1856  
1857 /* if the repo is not namespaced, nothing else to do */
1858 108 ▶ if (repo->namespace == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (repo->namespace == NULL) { 108 0 108
1859 108 out = git_str_detach(&path);
1860 108 goto done;
1861 }
1862  
1863 0 parts = end = git__strdup(repo->namespace);
1864 0 ▶ if (parts == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (parts == NULL) 0 0 0
1865 0 goto done;
1866  
1867 /*
1868 * From `man gitnamespaces`:
1869 * namespaces which include a / will expand to a hierarchy
1870 * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
1871 * refs under refs/namespaces/foo/refs/namespaces/bar/
1872 */
1873 0 ▶ while ((start = git__strsep(&end, "/")) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
while ((start = git__strsep(&end, "/")) != NULL) 0 0 0
1874 0 git_str_printf(&path, "refs/namespaces/%s/", start);
1875  
1876 0 git_str_printf(&path, "refs/namespaces/%s/refs", end);
1877 0 git__free(parts);
1878  
1879 /* Make sure that the folder with the namespace exists */
1880 0 ▶ if (git_futils_mkdir_relative(git_str_cstr(&path), in, 0777,
Branch: 1 branch
BranchTrueFalseTotal
if (git_futils_mkdir_relative(git_str_cstr(&path), in, 0777, 0 0 0
1881 GIT_MKDIR_PATH, NULL) < 0)
1882 0 goto done;
1883  
1884 /* Return root of the namespaced gitpath, i.e. without the trailing 'refs' */
1885 0 git_str_rtruncate_at_char(&path, '/');
1886 0 git_str_putc(&path, '/');
1887 0 out = git_str_detach(&path);
1888  
1889 108 done:
1890 108 git_str_dispose(&path);
1891 108 return out;
1892 }
1893  
1894 20 static int reflog_alloc(git_reflog **reflog, const char *name)
1895 {
1896 git_reflog *log;
1897  
1898 20 *reflog = NULL;
1899  
1900 20 log = git__calloc(1, sizeof(git_reflog));
1901 20 ▶ GIT_ERROR_CHECK_ALLOC(log);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(log); 0 20 20
1902  
1903 20 log->ref_name = git__strdup(name);
1904 20 ▶ GIT_ERROR_CHECK_ALLOC(log->ref_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(log->ref_name); 0 20 20
1905  
1906 20 ▶ if (git_vector_init(&log->entries, 0, NULL) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_init(&log->entries, 0, NULL) < 0) { 0 20 20
1907 0 git__free(log->ref_name);
1908 0 git__free(log);
1909 0 return -1;
1910 }
1911  
1912 20 *reflog = log;
1913  
1914 20 return 0;
1915 }
1916  
1917 20 static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
1918 {
1919 20 git_parse_ctx parser = GIT_PARSE_CTX_INIT;
1920  
1921 20 ▶ if ((git_parse_ctx_init(&parser, buf, buf_size)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((git_parse_ctx_init(&parser, buf, buf_size)) < 0) 0 20 20
1922 0 return -1;
1923  
1924 59 ▶ for (; parser.remain_len; git_parse_advance_line(&parser)) {
Branch: 1 branch
BranchTrueFalseTotal
for (; parser.remain_len; git_parse_advance_line(&parser)) { 39 20 59
1925 git_reflog_entry *entry;
1926 const char *sig;
1927 char c;
1928  
1929 39 entry = git__calloc(1, sizeof(*entry));
1930 39 ▶ GIT_ERROR_CHECK_ALLOC(entry);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(entry); 0 39 39
1931 39 entry->committer = git__calloc(1, sizeof(*entry->committer));
1932 39 ▶ GIT_ERROR_CHECK_ALLOC(entry->committer);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(entry->committer); 0 39 39
1933  
1934 78 if (git_parse_advance_oid(&entry->oid_old, &parser) < 0 ||
1935 78 ▶ git_parse_advance_expected(&parser, " ", 1) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_parse_advance_expected(&parser, " ", 1) < 0 || 0 39 39
1936 39 git_parse_advance_oid(&entry->oid_cur, &parser) < 0)
1937 0 goto next;
1938  
1939 39 sig = parser.line;
1940 2145 ▶ while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n')
Branch: 3 branches
BranchTrueFalseTotal
while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n') 2145 0 2145
while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n') 2106 39 2145
while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n') 2106 0 2106
1941 2106 git_parse_advance_chars(&parser, 1);
1942  
1943 39 ▶ if (git_signature__parse(entry->committer, &sig, parser.line, NULL, 0) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_signature__parse(entry->committer, &sig, parser.line, NULL, 0) < 0) 0 39 39
1944 0 goto next;
1945  
1946 39 ▶ if (c == '\t') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\t') { 39 0 39
1947 size_t len;
1948 39 git_parse_advance_chars(&parser, 1);
1949  
1950 39 len = parser.line_len;
1951 39 ▶ if (parser.line[len - 1] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (parser.line[len - 1] == '\n') 39 0 39
1952 39 len--;
1953  
1954 39 entry->msg = git__strndup(parser.line, len);
1955 39 ▶ GIT_ERROR_CHECK_ALLOC(entry->msg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(entry->msg); 0 39 39
1956 }
1957  
1958 39 ▶ if ((git_vector_insert(&log->entries, entry)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((git_vector_insert(&log->entries, entry)) < 0) { 0 39 39
1959 0 git_reflog_entry__free(entry);
1960 0 return -1;
1961 }
1962  
1963 39 continue;
1964  
1965 0 next:
1966 0 git_reflog_entry__free(entry);
1967 }
1968  
1969 20 return 0;
1970 }
1971  
1972 7 static int create_new_reflog_file(const char *filepath)
1973 {
1974 int fd, error;
1975  
1976 7 ▶ if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0) 0 7 7
1977 0 return error;
1978  
1979 7 ▶ if ((fd = p_open(filepath,
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = p_open(filepath, 0 7 7
1980 O_WRONLY | O_CREAT,
1981 GIT_REFLOG_FILE_MODE)) < 0)
1982 0 return -1;
1983  
1984 7 return p_close(fd);
1985 }
1986  
1987 6 static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
1988 {
1989 refdb_fs_backend *backend;
1990 git_repository *repo;
1991 6 git_str path = GIT_STR_INIT;
1992 int error;
1993  
1994 6 ▶ GIT_ASSERT_ARG(_backend && name);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend && name); 6 0 6
GIT_ASSERT_ARG(_backend && name); 0 6 6
1995  
1996 6 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1997 6 repo = backend->repo;
1998  
1999 6 ▶ if ((error = reflog_path(&path, repo, name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reflog_path(&path, repo, name)) < 0) 0 6 6
2000 0 return error;
2001  
2002 6 error = create_new_reflog_file(git_str_cstr(&path));
2003 6 git_str_dispose(&path);
2004  
2005 6 return error;
2006 }
2007  
2008 88 static int has_reflog(git_repository *repo, const char *name)
2009 {
2010 88 int ret = 0;
2011 88 git_str path = GIT_STR_INIT;
2012  
2013 88 ▶ if (reflog_path(&path, repo, name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (reflog_path(&path, repo, name) < 0) 0 88 88
2014 0 goto cleanup;
2015  
2016 88 ret = git_fs_path_isfile(git_str_cstr(&path));
2017  
2018 88 cleanup:
2019 88 git_str_dispose(&path);
2020 88 return ret;
2021 }
2022  
2023 88 static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name)
2024 {
2025 refdb_fs_backend *backend;
2026  
2027 88 ▶ GIT_ASSERT_ARG(_backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend); 0 88 88
2028 88 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 88 88
2029  
2030 88 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2031  
2032 88 return has_reflog(backend->repo, name);
2033 }
2034  
2035 20 static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
2036 {
2037 20 int error = -1;
2038 20 git_str log_path = GIT_STR_INIT;
2039 20 git_str log_file = GIT_STR_INIT;
2040 20 git_reflog *log = NULL;
2041 git_repository *repo;
2042 refdb_fs_backend *backend;
2043  
2044 20 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 20 20
2045 20 ▶ GIT_ASSERT_ARG(_backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend); 0 20 20
2046 20 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 20 20
2047  
2048 20 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2049 20 repo = backend->repo;
2050  
2051 20 ▶ if (reflog_alloc(&log, name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (reflog_alloc(&log, name) < 0) 0 20 20
2052 0 return -1;
2053  
2054 20 ▶ if (reflog_path(&log_path, repo, name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (reflog_path(&log_path, repo, name) < 0) 0 20 20
2055 0 goto cleanup;
2056  
2057 20 error = git_futils_readbuffer(&log_file, git_str_cstr(&log_path));
2058 20 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 1 19 20
if (error < 0 && error != GIT_ENOTFOUND) 0 1 1
2059 0 goto cleanup;
2060  
2061 21 if ((error == GIT_ENOTFOUND) &&
2062 1 ((error = create_new_reflog_file(git_str_cstr(&log_path))) < 0))
2063 0 goto cleanup;
2064  
2065 20 ▶ if ((error = reflog_parse(log,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reflog_parse(log, 0 20 20
2066 git_str_cstr(&log_file), git_str_len(&log_file))) < 0)
2067 0 goto cleanup;
2068  
2069 20 *out = log;
2070 20 goto success;
2071  
2072 0 cleanup:
2073 0 git_reflog_free(log);
2074  
2075 20 success:
2076 20 git_str_dispose(&log_file);
2077 20 git_str_dispose(&log_path);
2078  
2079 20 return error;
2080 }
2081  
2082 121 static int serialize_reflog_entry(
2083 git_str *buf,
2084 const git_oid *oid_old,
2085 const git_oid *oid_new,
2086 const git_signature *committer,
2087 const char *msg)
2088 {
2089 char raw_old[GIT_OID_HEXSZ+1];
2090 char raw_new[GIT_OID_HEXSZ+1];
2091  
2092 121 git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
2093 121 git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
2094  
2095 121 git_str_clear(buf);
2096  
2097 121 git_str_puts(buf, raw_old);
2098 121 git_str_putc(buf, ' ');
2099 121 git_str_puts(buf, raw_new);
2100  
2101 121 git_signature__writebuf(buf, " ", committer);
2102  
2103 /* drop trailing LF */
2104 121 git_str_rtrim(buf);
2105  
2106 121 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 114 7 121
2107 size_t i;
2108  
2109 114 git_str_putc(buf, '\t');
2110 114 git_str_puts(buf, msg);
2111  
2112 19259 ▶ for (i = 0; i < buf->size - 2; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < buf->size - 2; i++) 19145 114 19259
2113 19145 ▶ if (buf->ptr[i] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (buf->ptr[i] == '\n') 0 19145 19145
2114 0 buf->ptr[i] = ' ';
2115 114 git_str_rtrim(buf);
2116 }
2117  
2118 121 git_str_putc(buf, '\n');
2119  
2120 121 return git_str_oom(buf);
2121 }
2122  
2123 7 static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname)
2124 {
2125 git_repository *repo;
2126 7 git_str log_path = GIT_STR_INIT;
2127 int error;
2128  
2129 7 repo = backend->repo;
2130  
2131 7 ▶ if (!git_path_is_valid(backend->repo, refname, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_path_is_valid(backend->repo, refname, 0, GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS)) { 0 7 7
2132 0 git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", refname);
2133 0 return GIT_EINVALIDSPEC;
2134 }
2135  
2136 7 ▶ if (reflog_path(&log_path, repo, refname) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (reflog_path(&log_path, repo, refname) < 0) 0 7 7
2137 0 return -1;
2138  
2139 7 ▶ if (!git_fs_path_isfile(git_str_cstr(&log_path))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_fs_path_isfile(git_str_cstr(&log_path))) { 0 7 7
2140 0 git_error_set(GIT_ERROR_INVALID,
2141 "log file for reference '%s' doesn't exist", refname);
2142 0 error = -1;
2143 0 goto cleanup;
2144 }
2145  
2146 7 error = git_filebuf_open(file, git_str_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE);
2147  
2148 7 cleanup:
2149 7 git_str_dispose(&log_path);
2150  
2151 7 return error;
2152 }
2153  
2154 7 static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
2155 {
2156 7 int error = -1;
2157 unsigned int i;
2158 git_reflog_entry *entry;
2159 refdb_fs_backend *backend;
2160 7 git_str log = GIT_STR_INIT;
2161 7 git_filebuf fbuf = GIT_FILEBUF_INIT;
2162  
2163 7 ▶ GIT_ASSERT_ARG(_backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend); 0 7 7
2164 7 ▶ GIT_ASSERT_ARG(reflog);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(reflog); 0 7 7
2165  
2166 7 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2167  
2168 7 ▶ if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0) 0 7 7
2169 0 return -1;
2170  
2171 17 ▶ git_vector_foreach(&reflog->entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&reflog->entries, i, entry) { 10 7 17
2172 10 ▶ if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0) 0 10 10
2173 0 goto cleanup;
2174  
2175 10 ▶ if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0) 0 10 10
2176 0 goto cleanup;
2177 }
2178  
2179 7 error = git_filebuf_commit(&fbuf);
2180 7 goto success;
2181  
2182 0 cleanup:
2183 0 git_filebuf_cleanup(&fbuf);
2184  
2185 7 success:
2186 7 git_str_dispose(&log);
2187  
2188 7 return error;
2189 }
2190  
2191 /* Append to the reflog, must be called under reference lock */
2192 111 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)
2193 {
2194 int error, is_symbolic, open_flags;
2195 111 git_oid old_id = {{0}}, new_id = {{0}};
2196 111 git_str buf = GIT_STR_INIT, path = GIT_STR_INIT;
2197 111 git_repository *repo = backend->repo;
2198  
2199 111 is_symbolic = ref->type == GIT_REFERENCE_SYMBOLIC;
2200  
2201 /* "normal" symbolic updates do not write */
2202 111 ▶ if (is_symbolic &&
Branch: 1 branch
BranchTrueFalseTotal
if (is_symbolic && 48 63 111
2203 48 ▶ strcmp(ref->name, GIT_HEAD_FILE) &&
Branch: 2 branches
BranchTrueFalseTotal
strcmp(ref->name, GIT_HEAD_FILE) && 0 48 48
strcmp(ref->name, GIT_HEAD_FILE) && 0 0 0
2204 0 ▶ !(old && new))
Branch: 1 branch
BranchTrueFalseTotal
!(old && new)) 0 0 0
2205 0 return 0;
2206  
2207 /* From here on is_symbolic also means that it's HEAD */
2208  
2209 111 ▶ if (old) {
Branch: 1 branch
BranchTrueFalseTotal
if (old) { 31 80 111
2210 31 git_oid_cpy(&old_id, old);
2211 } else {
2212 80 error = git_reference_name_to_id(&old_id, repo, ref->name);
2213 80 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 23 57 80
if (error < 0 && error != GIT_ENOTFOUND) 0 23 23
2214 0 return error;
2215 }
2216  
2217 111 ▶ if (new) {
Branch: 1 branch
BranchTrueFalseTotal
if (new) { 30 81 111
2218 30 git_oid_cpy(&new_id, new);
2219 } else {
2220 81 ▶ if (!is_symbolic) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_symbolic) { 63 18 81
2221 63 git_oid_cpy(&new_id, git_reference_target(ref));
2222 } else {
2223 18 error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
2224 18 ▶ if (error < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if (error < 0 && error != GIT_ENOTFOUND) 0 18 18
if (error < 0 && error != GIT_ENOTFOUND) 0 0 0
2225 0 return error;
2226 /* detaching HEAD does not create an entry */
2227 18 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 18 18
2228 0 return 0;
2229  
2230 18 git_error_clear();
2231 }
2232 }
2233  
2234 111 ▶ if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0) 0 111 111
2235 0 goto cleanup;
2236  
2237 111 ▶ if ((error = reflog_path(&path, repo, ref->name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reflog_path(&path, repo, ref->name)) < 0) 0 111 111
2238 0 goto cleanup;
2239  
2240 111 ▶ if (((error = git_futils_mkpath2file(git_str_cstr(&path), 0777)) < 0) &&
Branch: 2 branches
BranchTrueFalseTotal
if (((error = git_futils_mkpath2file(git_str_cstr(&path), 0777)) < 0) && 0 111 111
if (((error = git_futils_mkpath2file(git_str_cstr(&path), 0777)) < 0) && 0 0 0
2241 (error != GIT_EEXISTS)) {
2242 0 goto cleanup;
2243 }
2244  
2245 /* If the new branch matches part of the namespace of a previously deleted branch,
2246 * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
2247 */
2248 111 ▶ if (git_fs_path_isdir(git_str_cstr(&path))) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_isdir(git_str_cstr(&path))) { 0 111 111
2249 0 ▶ if ((error = git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) { 0 0 0
2250 0 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 0 0 0
2251 0 error = 0;
2252 0 ▶ } else if (git_fs_path_isdir(git_str_cstr(&path))) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (git_fs_path_isdir(git_str_cstr(&path))) { 0 0 0
2253 0 git_error_set(GIT_ERROR_REFERENCE, "cannot create reflog at '%s', there are reflogs beneath that folder",
2254 0 ref->name);
2255 0 error = GIT_EDIRECTORY;
2256 }
2257  
2258 0 ▶ if (error != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error != 0) 0 0 0
2259 0 goto cleanup;
2260 }
2261  
2262 111 open_flags = O_WRONLY | O_CREAT | O_APPEND;
2263  
2264 111 ▶ if (backend->fsync)
Branch: 1 branch
BranchTrueFalseTotal
if (backend->fsync) 0 111 111
2265 0 open_flags |= O_FSYNC;
2266  
2267 111 error = git_futils_writebuffer(&buf, git_str_cstr(&path), open_flags, GIT_REFLOG_FILE_MODE);
2268  
2269 111 cleanup:
2270 111 git_str_dispose(&buf);
2271 111 git_str_dispose(&path);
2272  
2273 111 return error;
2274 }
2275  
2276 1 static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
2277 {
2278 1 int error = 0, fd;
2279 1 git_str old_path = GIT_STR_INIT;
2280 1 git_str new_path = GIT_STR_INIT;
2281 1 git_str temp_path = GIT_STR_INIT;
2282 1 git_str normalized = GIT_STR_INIT;
2283 git_repository *repo;
2284 refdb_fs_backend *backend;
2285  
2286 1 ▶ GIT_ASSERT_ARG(_backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend); 0 1 1
2287 1 ▶ GIT_ASSERT_ARG(old_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(old_name); 0 1 1
2288 1 ▶ GIT_ASSERT_ARG(new_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(new_name); 0 1 1
2289  
2290 1 backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2291 1 repo = backend->repo;
2292  
2293 1 ▶ if ((error = git_reference__normalize_name(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_reference__normalize_name( 0 1 1
2294 &normalized, new_name, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL)) < 0)
2295 0 return error;
2296  
2297 1 ▶ if (git_str_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0) 0 1 1
2298 0 return -1;
2299  
2300 1 ▶ if ((error = loose_path(&old_path, git_str_cstr(&temp_path), old_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&old_path, git_str_cstr(&temp_path), old_name)) < 0) 0 1 1
2301 0 return error;
2302  
2303 1 ▶ if ((error = loose_path(&new_path, git_str_cstr(&temp_path), git_str_cstr(&normalized))) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&new_path, git_str_cstr(&temp_path), git_str_cstr(&normalized))) < 0) 0 1 1
2304 0 return error;
2305  
2306 1 ▶ if (!git_fs_path_exists(git_str_cstr(&old_path))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_fs_path_exists(git_str_cstr(&old_path))) { 1 0 1
2307 1 error = GIT_ENOTFOUND;
2308 1 goto cleanup;
2309 }
2310  
2311 /*
2312 * Move the reflog to a temporary place. This two-phase renaming is required
2313 * in order to cope with funny renaming use cases when one tries to move a reference
2314 * to a partially colliding namespace:
2315 * - a/b -> a/b/c
2316 * - a/b/c/d -> a/b/c
2317 */
2318 0 ▶ if ((error = loose_path(&temp_path, git_str_cstr(&temp_path), "temp_reflog")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = loose_path(&temp_path, git_str_cstr(&temp_path), "temp_reflog")) < 0) 0 0 0
2319 0 return error;
2320  
2321 0 ▶ if ((fd = git_futils_mktmp(&temp_path, git_str_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = git_futils_mktmp(&temp_path, git_str_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) { 0 0 0
2322 0 error = -1;
2323 0 goto cleanup;
2324 }
2325  
2326 0 p_close(fd);
2327  
2328 0 ▶ if (p_rename(git_str_cstr(&old_path), git_str_cstr(&temp_path)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_rename(git_str_cstr(&old_path), git_str_cstr(&temp_path)) < 0) { 0 0 0
2329 0 git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
2330 0 error = -1;
2331 0 goto cleanup;
2332 }
2333  
2334 0 if (git_fs_path_isdir(git_str_cstr(&new_path)) &&
2335 0 (git_futils_rmdir_r(git_str_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
2336 0 error = -1;
2337 0 goto cleanup;
2338 }
2339  
2340 0 ▶ if (git_futils_mkpath2file(git_str_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_futils_mkpath2file(git_str_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) { 0 0 0
2341 0 error = -1;
2342 0 goto cleanup;
2343 }
2344  
2345 0 ▶ if (p_rename(git_str_cstr(&temp_path), git_str_cstr(&new_path)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_rename(git_str_cstr(&temp_path), git_str_cstr(&new_path)) < 0) { 0 0 0
2346 0 git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
2347 0 error = -1;
2348 }
2349  
2350 0 cleanup:
2351 1 git_str_dispose(&temp_path);
2352 1 git_str_dispose(&old_path);
2353 1 git_str_dispose(&new_path);
2354 1 git_str_dispose(&normalized);
2355  
2356 1 return error;
2357 }
2358  
2359 8 static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
2360 {
2361 8 refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
2362 8 git_str path = GIT_STR_INIT;
2363 int error;
2364  
2365 8 ▶ GIT_ASSERT_ARG(_backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(_backend); 0 8 8
2366 8 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 8 8
2367  
2368 8 ▶ if ((error = reflog_path(&path, backend->repo, name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = reflog_path(&path, backend->repo, name)) < 0) 0 8 8
2369 0 goto out;
2370  
2371 8 ▶ if (!git_fs_path_exists(path.ptr))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_fs_path_exists(path.ptr)) 5 3 8
2372 5 goto out;
2373  
2374 3 ▶ if ((error = p_unlink(path.ptr)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = p_unlink(path.ptr)) < 0) 0 3 3
2375 0 goto out;
2376  
2377 3 error = refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR);
2378  
2379 8 out:
2380 8 git_str_dispose(&path);
2381  
2382 8 return error;
2383 }
2384  
2385 54 int git_refdb_backend_fs(
2386 git_refdb_backend **backend_out,
2387 git_repository *repository)
2388 {
2389 54 int t = 0;
2390 54 git_str gitpath = GIT_STR_INIT;
2391 refdb_fs_backend *backend;
2392  
2393 54 backend = git__calloc(1, sizeof(refdb_fs_backend));
2394 54 ▶ GIT_ERROR_CHECK_ALLOC(backend);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(backend); 0 54 54
2395 54 ▶ if (git_mutex_init(&backend->prlock) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_mutex_init(&backend->prlock) < 0) { 0 54 54
2396 0 git__free(backend);
2397 0 return -1;
2398 }
2399  
2400  
2401 54 ▶ if (git_refdb_init_backend(&backend->parent, GIT_REFDB_BACKEND_VERSION) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_refdb_init_backend(&backend->parent, GIT_REFDB_BACKEND_VERSION) < 0) 0 54 54
2402 0 goto fail;
2403  
2404 54 backend->repo = repository;
2405  
2406 54 ▶ if (repository->gitdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (repository->gitdir) { 54 0 54
2407 54 backend->gitpath = setup_namespace(repository, repository->gitdir);
2408  
2409 54 ▶ if (backend->gitpath == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (backend->gitpath == NULL) 0 54 54
2410 0 goto fail;
2411 }
2412  
2413 54 ▶ if (repository->commondir) {
Branch: 1 branch
BranchTrueFalseTotal
if (repository->commondir) { 54 0 54
2414 54 backend->commonpath = setup_namespace(repository, repository->commondir);
2415  
2416 54 ▶ if (backend->commonpath == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (backend->commonpath == NULL) 0 54 54
2417 0 goto fail;
2418 }
2419  
2420 108 if (git_str_joinpath(&gitpath, backend->commonpath, GIT_PACKEDREFS_FILE) < 0 ||
2421 54 git_sortedcache_new(
2422 &backend->refcache, offsetof(struct packref, name),
2423 NULL, NULL, packref_cmp, git_str_cstr(&gitpath)) < 0)
2424 0 goto fail;
2425  
2426 54 git_str_dispose(&gitpath);
2427  
2428 54 ▶ if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) {
Branch: 2 branches
BranchTrueFalseTotal
if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) { 54 0 54
if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) { 0 54 54
2429 0 backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
2430 0 backend->direach_flags |= GIT_FS_PATH_DIR_IGNORE_CASE;
2431 }
2432 54 ▶ if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) {
Branch: 2 branches
BranchTrueFalseTotal
if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) { 54 0 54
if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) { 0 54 54
2433 0 backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
2434 0 backend->direach_flags |= GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE;
2435 }
2436 54 ▶ if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) || 54 0 54
if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) || 54 0 54
if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) || 0 54 54
2437 git_repository__fsync_gitdir)
2438 0 backend->fsync = 1;
2439 54 backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS;
2440  
2441 54 backend->parent.exists = &refdb_fs_backend__exists;
2442 54 backend->parent.lookup = &refdb_fs_backend__lookup;
2443 54 backend->parent.iterator = &refdb_fs_backend__iterator;
2444 54 backend->parent.write = &refdb_fs_backend__write;
2445 54 backend->parent.del = &refdb_fs_backend__delete;
2446 54 backend->parent.rename = &refdb_fs_backend__rename;
2447 54 backend->parent.compress = &refdb_fs_backend__compress;
2448 54 backend->parent.lock = &refdb_fs_backend__lock;
2449 54 backend->parent.unlock = &refdb_fs_backend__unlock;
2450 54 backend->parent.has_log = &refdb_reflog_fs__has_log;
2451 54 backend->parent.ensure_log = &refdb_reflog_fs__ensure_log;
2452 54 backend->parent.free = &refdb_fs_backend__free;
2453 54 backend->parent.reflog_read = &refdb_reflog_fs__read;
2454 54 backend->parent.reflog_write = &refdb_reflog_fs__write;
2455 54 backend->parent.reflog_rename = &refdb_reflog_fs__rename;
2456 54 backend->parent.reflog_delete = &refdb_reflog_fs__delete;
2457  
2458 54 *backend_out = (git_refdb_backend *)backend;
2459 54 return 0;
2460  
2461 0 fail:
2462 0 git_mutex_free(&backend->prlock);
2463 0 git_str_dispose(&gitpath);
2464 0 git__free(backend->gitpath);
2465 0 git__free(backend->commonpath);
2466 0 git__free(backend);
2467 0 return -1;
2468 }
« deps/libgit2/src/libgit2/ignore.c ↑ Summary const-xs-object.inc »